2 This file is part of GNUnet
3 (C) 2008--2012 Christian Grothoff (and other contributing authors)
5 GNUnet is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published
7 by the Free Software Foundation; either version 3, or (at your
8 option) any later version.
10 GNUnet is distributed in the hope that it will be useful, but
11 WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNUnet; see the file COPYING. If not, write to the
17 Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
22 * @file testbed/testbed_api_operations.c
23 * @brief functions to manage operation queues
24 * @author Christian Grothoff
27 #include "testbed_api_operations.h"
31 * An entry in the operation queue
36 * The next DLL pointer
38 struct QueueEntry *next;
41 * The prev DLL pointer
43 struct QueueEntry *prev;
46 * The operation this entry holds
48 struct GNUNET_TESTBED_Operation *op;
53 * Queue of operations where we can only support a certain
54 * number of concurrent operations of a particular type.
59 * The head of the operation queue
61 struct QueueEntry *head;
64 * The tail of the operation queue
66 struct QueueEntry *tail;
69 * Number of operations that are currently active in this queue.
74 * Max number of operations which can be active at any time in this queue
76 unsigned int max_active;
87 * The operation is just created and is in initial state
92 * The operation is currently waiting for resources
97 * The operation is ready to be started
102 * The operation has started
109 * Opaque handle to an abstract operation to be executed by the testing framework.
111 struct GNUNET_TESTBED_Operation
114 * Function to call when we have the resources to begin the operation.
116 OperationStart start;
119 * Function to call to clean up after the operation (which may or may
120 * not have been started yet).
122 OperationRelease release;
125 * Closure for callbacks.
130 * Array of operation queues this Operation belongs to.
132 struct OperationQueue **queues;
135 * The id of the task which calls OperationStart for this operation
137 GNUNET_SCHEDULER_TaskIdentifier start_task_id;
140 * Number of queues in the operation queues array
142 unsigned int nqueues;
145 * The state of the operation
147 enum OperationState state;
153 * Task for calling OperationStart on operation
155 * @param cls the Operation
156 * @param tc the TaskContext from scheduler
159 call_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
161 struct GNUNET_TESTBED_Operation *op = cls;
163 op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
164 op->state = OP_STATE_STARTED;
165 if (NULL != op->start)
166 op->start (op->cb_cls);
171 * Checks for the readiness of an operation and schedules a operation start task
173 * @param op the operation
176 check_readiness (struct GNUNET_TESTBED_Operation *op)
180 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == op->start_task_id);
181 for (i = 0; i < op->nqueues; i++)
182 if (op->queues[i]->active >= op->queues[i]->max_active)
184 for (i = 0; i < op->nqueues; i++)
185 op->queues[i]->active++;
186 op->state = OP_STATE_READY;
187 op->start_task_id = GNUNET_SCHEDULER_add_now (&call_start, op);
192 * Create an 'operation' to be performed.
194 * @param cls closure for the callbacks
195 * @param start function to call to start the operation
196 * @param release function to call to close down the operation
197 * @return handle to the operation
199 struct GNUNET_TESTBED_Operation *
200 GNUNET_TESTBED_operation_create_ (void *cls, OperationStart start,
201 OperationRelease release)
203 struct GNUNET_TESTBED_Operation *op;
205 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
207 op->state = OP_STATE_INIT;
208 op->release = release;
210 op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
216 * Create an operation queue.
218 * @param max_active maximum number of operations in this
219 * queue that can be active in parallel at the same time
220 * @return handle to the queue
222 struct OperationQueue *
223 GNUNET_TESTBED_operation_queue_create_ (unsigned int max_active)
225 struct OperationQueue *queue;
227 queue = GNUNET_malloc (sizeof (struct OperationQueue));
228 queue->max_active = max_active;
234 * Destroy an operation queue. The queue MUST be empty
237 * @param queue queue to destroy
240 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
242 GNUNET_break (NULL == queue->head);
243 GNUNET_break (NULL == queue->tail);
249 * Function to reset the maximum number of operations in the given queue. If
250 * max_active is lesser than the number of currently active operations, the
251 * active operations are not stopped immediately.
253 * @param queue the operation queue which has to be modified
254 * @param max_active the new maximum number of active operations
257 GNUNET_TESTBED_operation_queue_reset_max_active_ (struct OperationQueue *queue,
258 unsigned int max_active)
260 struct QueueEntry *entry;
262 queue->max_active = max_active;
263 if (queue->active >= queue->max_active)
266 while ( (NULL != entry) &&
267 (queue->active < queue->max_active) )
269 if (OP_STATE_WAITING == entry->op->state)
270 check_readiness (entry->op);
277 * Add an operation to a queue. An operation can be in multiple queues at
278 * once. Once the operation is inserted into all the queues
279 * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
280 * waiting for the operation to become active.
282 * @param queue queue to add the operation to
283 * @param operation operation to add to the queue
286 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
287 struct GNUNET_TESTBED_Operation
290 struct QueueEntry *entry;
292 entry = GNUNET_malloc (sizeof (struct QueueEntry));
293 entry->op = operation;
294 GNUNET_CONTAINER_DLL_insert_tail (queue->head, queue->tail, entry);
295 GNUNET_array_append (operation->queues, operation->nqueues, queue);
300 * Marks the given operation as waiting on the queues. Once all queues permit
301 * the operation to become active, the operation will be activated. The actual
302 * activation will occur in a separate task (thus allowing multiple queue
303 * insertions to be made without having the first one instantly trigger the
304 * operation if the first queue has sufficient resources).
306 * @param operation the operation to marks as waiting
309 GNUNET_TESTBED_operation_begin_wait_ (struct GNUNET_TESTBED_Operation
312 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == operation->start_task_id);
313 operation->state = OP_STATE_WAITING;
314 check_readiness (operation);
319 * Remove an operation from a queue. This can be because the
320 * oeration was active and has completed (and the resources have
321 * been released), or because the operation was cancelled and
322 * thus scheduling the operation is no longer required.
324 * @param queue queue to add the operation to
325 * @param operation operation to add to the queue
328 GNUNET_TESTBED_operation_queue_remove_ (struct OperationQueue *queue,
329 struct GNUNET_TESTBED_Operation
332 struct QueueEntry *entry;
333 struct QueueEntry *entry2;
335 for (entry = queue->head; NULL != entry; entry = entry->next)
336 if (entry->op == operation)
338 GNUNET_assert (NULL != entry);
339 if (OP_STATE_STARTED == operation->state)
341 GNUNET_assert (0 != queue->active);
344 entry2 = entry->next;
345 GNUNET_CONTAINER_DLL_remove (queue->head, queue->tail, entry);
347 for (; NULL != entry2; entry2 = entry2->next)
348 if (OP_STATE_WAITING == entry2->op->state)
352 check_readiness (entry2->op);
357 * An operation is 'done' (was cancelled or finished); remove
358 * it from the queues and release associated resources.
360 * @param operation operation that finished
363 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *operation)
367 if (GNUNET_SCHEDULER_NO_TASK != operation->start_task_id)
369 GNUNET_SCHEDULER_cancel (operation->start_task_id);
370 operation->start_task_id = GNUNET_SCHEDULER_NO_TASK;
372 for (i = 0; i < operation->nqueues; i++)
373 GNUNET_TESTBED_operation_queue_remove_ (operation->queues[i], operation);
374 GNUNET_free (operation->queues);
375 if (NULL != operation->release)
376 operation->release (operation->cb_cls);
377 GNUNET_free (operation);
381 /* end of testbed_api_operations.c */