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 * DLL head for the wait queue. Operations which are waiting for this
75 * operation queue are put here
77 struct QueueEntry *wq_head;
80 * DLL tail for the wait queue.
82 struct QueueEntry *wq_tail;
85 * Number of operations that are currently active in this queue.
90 * Max number of operations which can be active at any time in this queue
92 unsigned int max_active;
103 * The operation is just created and is in initial state
108 * The operation is currently waiting for resources
113 * The operation is ready to be started
118 * The operation has started
125 * An entry in the ready queue (implemented as DLL)
127 struct ReadyQueueEntry
132 struct ReadyQueueEntry *next;
137 struct ReadyQueueEntry *prev;
140 * The operation associated with this entry
142 struct GNUNET_TESTBED_Operation *op;
147 * Opaque handle to an abstract operation to be executed by the testing framework.
149 struct GNUNET_TESTBED_Operation
152 * Function to call when we have the resources to begin the operation.
154 OperationStart start;
157 * Function to call to clean up after the operation (which may or may
158 * not have been started yet).
160 OperationRelease release;
163 * Closure for callbacks.
168 * Array of operation queues this Operation belongs to.
170 struct OperationQueue **queues;
173 * Array of operation queue entries corresponding to this operation in
174 * operation queues for this operation
176 struct QueueEntry **qentries;
179 * Array of number of resources an operation need from each queue. The numbers
180 * in this array should correspond to the queues array
185 * Entry corresponding to this operation in ready queue. Will be NULL if the
186 * operation is not marked as READY
188 struct ReadyQueueEntry *rq_entry;
191 * Number of queues in the operation queues array
193 unsigned int nqueues;
196 * The state of the operation
198 enum OperationState state;
203 * DLL head for the ready queue
205 struct ReadyQueueEntry *rq_head;
208 * DLL tail for the ready queue
210 struct ReadyQueueEntry *rq_tail;
213 * The id of the task to process the ready queue
215 GNUNET_SCHEDULER_TaskIdentifier process_rq_task_id;
219 * Removes an operation from the ready queue. Also stops the 'process_rq_task'
220 * if the given operation is the last one in the queue.
222 * @param op the operation to be removed
225 rq_remove (struct GNUNET_TESTBED_Operation *op)
227 GNUNET_assert (NULL != op->rq_entry);
228 GNUNET_CONTAINER_DLL_remove (rq_head, rq_tail, op->rq_entry);
229 GNUNET_free (op->rq_entry);
231 if ( (NULL == rq_head) && (GNUNET_SCHEDULER_NO_TASK != process_rq_task_id) )
233 GNUNET_SCHEDULER_cancel (process_rq_task_id);
234 process_rq_task_id = GNUNET_SCHEDULER_NO_TASK;
240 * Processes the ready queue by calling the operation start callback of the
241 * operation at the head. The operation is then removed from the queue. The
242 * task is scheduled to run again immediately until no more operations are in
246 * @param tc scheduler task context. Not used.
249 process_rq_task (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
251 struct GNUNET_TESTBED_Operation *op;
253 process_rq_task_id = GNUNET_SCHEDULER_NO_TASK;
254 GNUNET_assert (NULL != rq_head);
255 GNUNET_assert (NULL != (op = rq_head->op));
258 process_rq_task_id = GNUNET_SCHEDULER_add_now (&process_rq_task, NULL);
259 op->state = OP_STATE_STARTED;
260 if (NULL != op->start)
261 op->start (op->cb_cls);
266 * Adds the operation to the ready queue and starts the 'process_rq_task'
268 * @param op the operation to be queued
271 rq_add (struct GNUNET_TESTBED_Operation *op)
273 struct ReadyQueueEntry *rq_entry;
275 GNUNET_assert (NULL == op->rq_entry);
276 rq_entry = GNUNET_malloc (sizeof (struct ReadyQueueEntry));
278 GNUNET_CONTAINER_DLL_insert_tail (rq_head, rq_tail, rq_entry);
279 op->rq_entry = rq_entry;
280 if (GNUNET_SCHEDULER_NO_TASK == process_rq_task_id)
281 process_rq_task_id = GNUNET_SCHEDULER_add_now (&process_rq_task, NULL);
286 wq_add (struct GNUNET_TESTBED_Operation *op)
288 struct QueueEntry *entry;
289 struct OperationQueue *opq;
292 GNUNET_assert (OP_STATE_WAITING == op->state);
293 GNUNET_assert (NULL == op->qentries);
294 for (cnt = 0; cnt < op->nqueues;)
296 opq = op->queues[cnt];
297 entry = GNUNET_malloc (sizeof (struct QueueEntry));
299 entry->nres = op->nres[cnt];
300 GNUNET_CONTAINER_DLL_insert_tail (opq->wq_head, opq->wq_tail, entry);
301 GNUNET_array_append (op->qentries, cnt, entry); /* increments cnt */
307 wq_remove (struct GNUNET_TESTBED_Operation *op)
309 struct QueueEntry *entry;
310 struct OperationQueue *opq;
313 GNUNET_assert (OP_STATE_WAITING == op->state);
314 GNUNET_assert (NULL != op->qentries);
315 for (cnt = 0; cnt < op->nqueues; cnt ++)
317 opq = op->queues[cnt];
318 entry = op->qentries[cnt];
319 GNUNET_CONTAINER_DLL_remove (opq->wq_head, opq->wq_tail, entry);
322 GNUNET_free (op->qentries);
328 * Checks for the readiness of an operation and schedules a operation start task
330 * @param op the operation
333 check_readiness (struct GNUNET_TESTBED_Operation *op)
337 GNUNET_assert (NULL == op->rq_entry);
338 GNUNET_assert (OP_STATE_WAITING == op->state);
339 for (i = 0; i < op->nqueues; i++)
341 GNUNET_assert (0 < op->nres[i]);
342 if ((op->queues[i]->active + op->nres[i]) > op->queues[i]->max_active)
346 for (i = 0; i < op->nqueues; i++)
347 op->queues[i]->active += op->nres[i];
348 op->state = OP_STATE_READY;
354 * Defers a ready to be executed operation back to waiting
356 * @param op the operation to defer
359 defer (struct GNUNET_TESTBED_Operation *op)
363 GNUNET_assert (OP_STATE_READY == op->state);
365 for (i = 0; i < op->nqueues; i++)
366 op->queues[i]->active--;
367 op->state = OP_STATE_WAITING;
373 * Create an 'operation' to be performed.
375 * @param cls closure for the callbacks
376 * @param start function to call to start the operation
377 * @param release function to call to close down the operation
378 * @return handle to the operation
380 struct GNUNET_TESTBED_Operation *
381 GNUNET_TESTBED_operation_create_ (void *cls, OperationStart start,
382 OperationRelease release)
384 struct GNUNET_TESTBED_Operation *op;
386 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
388 op->state = OP_STATE_INIT;
389 op->release = release;
396 * Create an operation queue.
398 * @param max_active maximum number of operations in this
399 * queue that can be active in parallel at the same time
400 * @return handle to the queue
402 struct OperationQueue *
403 GNUNET_TESTBED_operation_queue_create_ (unsigned int max_active)
405 struct OperationQueue *queue;
407 queue = GNUNET_malloc (sizeof (struct OperationQueue));
408 queue->max_active = max_active;
414 * Destroy an operation queue. The queue MUST be empty
417 * @param queue queue to destroy
420 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
422 GNUNET_break (NULL == queue->head);
423 GNUNET_break (NULL == queue->tail);
429 * Destroys the operation queue if it is empty. If not empty return GNUNET_NO.
431 * @param queue the queue to destroy if empty
432 * @return GNUNET_YES if the queue is destroyed. GNUNET_NO if not (because it
436 GNUNET_TESTBED_operation_queue_destroy_empty_ (struct OperationQueue *queue)
438 if (NULL != queue->head)
440 GNUNET_TESTBED_operation_queue_destroy_ (queue);
446 * Function to reset the maximum number of operations in the given queue. If
447 * max_active is lesser than the number of currently active operations, the
448 * active operations are not stopped immediately.
450 * @param queue the operation queue which has to be modified
451 * @param max_active the new maximum number of active operations
454 GNUNET_TESTBED_operation_queue_reset_max_active_ (struct OperationQueue *queue,
455 unsigned int max_active)
457 struct QueueEntry *entry;
459 queue->max_active = max_active;
461 while ((queue->active > queue->max_active) && (NULL != entry))
463 if (entry->op->state == OP_STATE_READY)
469 while ((NULL != entry) && (queue->active < queue->max_active))
471 if (OP_STATE_WAITING == entry->op->state)
472 check_readiness (entry->op);
479 * Add an operation to a queue. An operation can be in multiple queues at
480 * once. Once the operation is inserted into all the queues
481 * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
482 * waiting for the operation to become active.
484 * @param queue queue to add the operation to
485 * @param op operation to add to the queue
486 * @param nres the number of units of the resources of queue needed by the
487 * operation. Should be greater than 0.
490 GNUNET_TESTBED_operation_queue_insert2_ (struct OperationQueue *queue,
491 struct GNUNET_TESTBED_Operation *op,
494 struct QueueEntry *entry;
497 GNUNET_assert (0 < nres);
498 entry = GNUNET_malloc (sizeof (struct QueueEntry));
501 GNUNET_CONTAINER_DLL_insert_tail (queue->head, queue->tail, entry);
503 GNUNET_array_append (op->queues, op->nqueues, queue);
504 GNUNET_array_append (op->nres, qsize, nres);
505 GNUNET_assert (qsize == op->nqueues);
510 * Add an operation to a queue. An operation can be in multiple queues at
511 * once. Once the operation is inserted into all the queues
512 * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
513 * waiting for the operation to become active. The operation is assumed to take
514 * 1 queue resource. Use GNUNET_TESTBED_operation_queue_insert2_() if it
515 * requires more than 1
517 * @param queue queue to add the operation to
518 * @param op operation to add to the queue
521 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
522 struct GNUNET_TESTBED_Operation *op)
524 return GNUNET_TESTBED_operation_queue_insert2_ (queue, op, 1);
529 * Marks the given operation as waiting on the queues. Once all queues permit
530 * the operation to become active, the operation will be activated. The actual
531 * activation will occur in a separate task (thus allowing multiple queue
532 * insertions to be made without having the first one instantly trigger the
533 * operation if the first queue has sufficient resources).
535 * @param op the operation to marks as waiting
538 GNUNET_TESTBED_operation_begin_wait_ (struct GNUNET_TESTBED_Operation *op)
540 GNUNET_assert (NULL == op->rq_entry);
541 op->state = OP_STATE_WAITING;
543 check_readiness (op);
548 * Remove an operation from a queue. This can be because the
549 * oeration was active and has completed (and the resources have
550 * been released), or because the operation was cancelled and
551 * thus scheduling the operation is no longer required.
553 * @param queue queue to add the operation to
554 * @param op operation to add to the queue
557 GNUNET_TESTBED_operation_queue_remove_ (struct OperationQueue *queue,
558 struct GNUNET_TESTBED_Operation
561 struct QueueEntry *entry;
563 for (entry = queue->head; NULL != entry; entry = entry->next)
566 GNUNET_assert (NULL != entry);
567 GNUNET_assert (0 < entry->nres);
571 case OP_STATE_WAITING:
574 case OP_STATE_STARTED:
575 GNUNET_assert (0 != queue->active);
576 GNUNET_assert (queue->active >= entry->nres);
577 queue->active -= entry->nres;
580 GNUNET_CONTAINER_DLL_remove (queue->head, queue->tail, entry);
582 entry = queue->wq_head;
585 check_readiness (entry->op);
590 * An operation is 'done' (was cancelled or finished); remove
591 * it from the queues and release associated resources.
593 * @param op operation that finished
596 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *op)
605 case OP_STATE_WAITING:
608 case OP_STATE_STARTED:
612 for (i = 0; i < op->nqueues; i++)
613 GNUNET_TESTBED_operation_queue_remove_ (op->queues[i], op);
614 GNUNET_free (op->queues);
615 GNUNET_free (op->nres);
616 if (NULL != op->release)
617 op->release (op->cb_cls);
622 /* end of testbed_api_operations.c */