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;
51 * How many units of resources does the operation need
58 * Queue of operations where we can only support a certain
59 * number of concurrent operations of a particular type.
64 * The head of the operation queue
66 struct QueueEntry *head;
69 * The tail of the operation queue
71 struct QueueEntry *tail;
74 * Number of operations that are currently active in this queue.
79 * Max number of operations which can be active at any time in this queue
81 unsigned int max_active;
92 * The operation is just created and is in initial state
97 * The operation is currently waiting for resources
102 * The operation is ready to be started
107 * The operation has started
114 * Opaque handle to an abstract operation to be executed by the testing framework.
116 struct GNUNET_TESTBED_Operation
119 * Function to call when we have the resources to begin the operation.
121 OperationStart start;
124 * Function to call to clean up after the operation (which may or may
125 * not have been started yet).
127 OperationRelease release;
130 * Closure for callbacks.
135 * Array of operation queues this Operation belongs to.
137 struct OperationQueue **queues;
140 * Array of number resources an operation need from each queue. This numbers
141 * in this array should correspond to the queues array
146 * The id of the task which calls OperationStart for this operation
148 GNUNET_SCHEDULER_TaskIdentifier start_task_id;
151 * Number of queues in the operation queues array
153 unsigned int nqueues;
156 * The state of the operation
158 enum OperationState state;
164 * Task for calling OperationStart on operation
166 * @param cls the Operation
167 * @param tc the TaskContext from scheduler
170 call_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
172 struct GNUNET_TESTBED_Operation *op = cls;
174 op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
175 op->state = OP_STATE_STARTED;
176 if (NULL != op->start)
177 op->start (op->cb_cls);
182 * Checks for the readiness of an operation and schedules a operation start task
184 * @param op the operation
187 check_readiness (struct GNUNET_TESTBED_Operation *op)
191 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == op->start_task_id);
192 for (i = 0; i < op->nqueues; i++)
194 GNUNET_assert (0 < op->nres[i]);
195 if ((op->queues[i]->active + op->nres[i]) > op->queues[i]->max_active)
198 for (i = 0; i < op->nqueues; i++)
199 op->queues[i]->active += op->nres[i];
200 op->state = OP_STATE_READY;
201 op->start_task_id = GNUNET_SCHEDULER_add_now (&call_start, op);
206 * Defers a ready to be executed operation back to waiting
208 * @param op the operation to defer
211 defer (struct GNUNET_TESTBED_Operation *op)
215 GNUNET_assert (OP_STATE_READY == op->state);
216 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != op->start_task_id);
217 GNUNET_SCHEDULER_cancel (op->start_task_id);
218 op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
219 for (i = 0; i < op->nqueues; i++)
220 op->queues[i]->active--;
221 op->state = OP_STATE_WAITING;
226 * Create an 'operation' to be performed.
228 * @param cls closure for the callbacks
229 * @param start function to call to start the operation
230 * @param release function to call to close down the operation
231 * @return handle to the operation
233 struct GNUNET_TESTBED_Operation *
234 GNUNET_TESTBED_operation_create_ (void *cls, OperationStart start,
235 OperationRelease release)
237 struct GNUNET_TESTBED_Operation *op;
239 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
241 op->state = OP_STATE_INIT;
242 op->release = release;
244 op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
250 * Create an operation queue.
252 * @param max_active maximum number of operations in this
253 * queue that can be active in parallel at the same time
254 * @return handle to the queue
256 struct OperationQueue *
257 GNUNET_TESTBED_operation_queue_create_ (unsigned int max_active)
259 struct OperationQueue *queue;
261 queue = GNUNET_malloc (sizeof (struct OperationQueue));
262 queue->max_active = max_active;
268 * Destroy an operation queue. The queue MUST be empty
271 * @param queue queue to destroy
274 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
276 GNUNET_break (NULL == queue->head);
277 GNUNET_break (NULL == queue->tail);
283 * Function to reset the maximum number of operations in the given queue. If
284 * max_active is lesser than the number of currently active operations, the
285 * active operations are not stopped immediately.
287 * @param queue the operation queue which has to be modified
288 * @param max_active the new maximum number of active operations
291 GNUNET_TESTBED_operation_queue_reset_max_active_ (struct OperationQueue *queue,
292 unsigned int max_active)
294 struct QueueEntry *entry;
296 queue->max_active = max_active;
297 /* if (queue->active >= queue->max_active) */
301 while ((queue->active > queue->max_active) && (NULL != entry))
303 if (entry->op->state == OP_STATE_READY)
309 while ((NULL != entry) && (queue->active < queue->max_active))
311 if (OP_STATE_WAITING == entry->op->state)
312 check_readiness (entry->op);
319 * Add an operation to a queue. An operation can be in multiple queues at
320 * once. Once the operation is inserted into all the queues
321 * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
322 * waiting for the operation to become active.
324 * @param queue queue to add the operation to
325 * @param operation operation to add to the queue
326 * @param nres the number of units of the resources of queue needed by the
327 * operation. Should be greater than 0.
330 GNUNET_TESTBED_operation_queue_insert2_ (struct OperationQueue *queue,
331 struct GNUNET_TESTBED_Operation
332 *operation, unsigned int nres)
334 struct QueueEntry *entry;
337 GNUNET_assert (0 < nres);
338 entry = GNUNET_malloc (sizeof (struct QueueEntry));
339 entry->op = operation;
341 GNUNET_CONTAINER_DLL_insert_tail (queue->head, queue->tail, entry);
342 qsize = operation->nqueues;
343 GNUNET_array_append (operation->queues, operation->nqueues, queue);
344 GNUNET_array_append (operation->nres, qsize, nres);
345 GNUNET_assert (qsize == operation->nqueues);
350 * Add an operation to a queue. An operation can be in multiple queues at
351 * once. Once the operation is inserted into all the queues
352 * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
353 * waiting for the operation to become active. The operation is assumed to take
354 * 1 queue resource. Use GNUNET_TESTBED_operation_queue_insert2_() if it
355 * requires more than 1
357 * @param queue queue to add the operation to
358 * @param operation operation to add to the queue
361 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
362 struct GNUNET_TESTBED_Operation
365 return GNUNET_TESTBED_operation_queue_insert2_ (queue, operation, 1);
370 * Marks the given operation as waiting on the queues. Once all queues permit
371 * the operation to become active, the operation will be activated. The actual
372 * activation will occur in a separate task (thus allowing multiple queue
373 * insertions to be made without having the first one instantly trigger the
374 * operation if the first queue has sufficient resources).
376 * @param operation the operation to marks as waiting
379 GNUNET_TESTBED_operation_begin_wait_ (struct GNUNET_TESTBED_Operation
382 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == operation->start_task_id);
383 operation->state = OP_STATE_WAITING;
384 check_readiness (operation);
389 * Remove an operation from a queue. This can be because the
390 * oeration was active and has completed (and the resources have
391 * been released), or because the operation was cancelled and
392 * thus scheduling the operation is no longer required.
394 * @param queue queue to add the operation to
395 * @param operation operation to add to the queue
398 GNUNET_TESTBED_operation_queue_remove_ (struct OperationQueue *queue,
399 struct GNUNET_TESTBED_Operation
402 struct QueueEntry *entry;
403 struct QueueEntry *entry2;
405 for (entry = queue->head; NULL != entry; entry = entry->next)
406 if (entry->op == operation)
408 GNUNET_assert (NULL != entry);
409 GNUNET_assert (0 < entry->nres);
410 switch (operation->state)
413 case OP_STATE_WAITING:
416 case OP_STATE_STARTED:
417 GNUNET_assert (0 != queue->active);
418 GNUNET_assert (queue->active >= entry->nres);
419 queue->active -= entry->nres;
422 entry2 = entry->next;
423 GNUNET_CONTAINER_DLL_remove (queue->head, queue->tail, entry);
425 for (; NULL != entry2; entry2 = entry2->next)
426 if (OP_STATE_WAITING == entry2->op->state)
430 check_readiness (entry2->op);
435 * An operation is 'done' (was cancelled or finished); remove
436 * it from the queues and release associated resources.
438 * @param operation operation that finished
441 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *operation)
445 if (GNUNET_SCHEDULER_NO_TASK != operation->start_task_id)
447 GNUNET_SCHEDULER_cancel (operation->start_task_id);
448 operation->start_task_id = GNUNET_SCHEDULER_NO_TASK;
450 for (i = 0; i < operation->nqueues; i++)
451 GNUNET_TESTBED_operation_queue_remove_ (operation->queues[i], operation);
452 GNUNET_free (operation->queues);
453 GNUNET_free (operation->nres);
454 if (NULL != operation->release)
455 operation->release (operation->cb_cls);
456 GNUNET_free (operation);
460 /* end of testbed_api_operations.c */