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 * Defers a ready to be executed operation back to waiting
194 * @param op the operation to defer
197 defer (struct GNUNET_TESTBED_Operation *op)
201 GNUNET_assert (OP_STATE_READY == op->state);
202 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK != op->start_task_id);
203 GNUNET_SCHEDULER_cancel (op->start_task_id);
204 op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
205 for (i = 0; i < op->nqueues; i++)
206 op->queues[i]->active--;
207 op->state = OP_STATE_WAITING;
212 * Create an 'operation' to be performed.
214 * @param cls closure for the callbacks
215 * @param start function to call to start the operation
216 * @param release function to call to close down the operation
217 * @return handle to the operation
219 struct GNUNET_TESTBED_Operation *
220 GNUNET_TESTBED_operation_create_ (void *cls, OperationStart start,
221 OperationRelease release)
223 struct GNUNET_TESTBED_Operation *op;
225 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
227 op->state = OP_STATE_INIT;
228 op->release = release;
230 op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
236 * Create an operation queue.
238 * @param max_active maximum number of operations in this
239 * queue that can be active in parallel at the same time
240 * @return handle to the queue
242 struct OperationQueue *
243 GNUNET_TESTBED_operation_queue_create_ (unsigned int max_active)
245 struct OperationQueue *queue;
247 queue = GNUNET_malloc (sizeof (struct OperationQueue));
248 queue->max_active = max_active;
254 * Destroy an operation queue. The queue MUST be empty
257 * @param queue queue to destroy
260 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
262 GNUNET_break (NULL == queue->head);
263 GNUNET_break (NULL == queue->tail);
269 * Function to reset the maximum number of operations in the given queue. If
270 * max_active is lesser than the number of currently active operations, the
271 * active operations are not stopped immediately.
273 * @param queue the operation queue which has to be modified
274 * @param max_active the new maximum number of active operations
277 GNUNET_TESTBED_operation_queue_reset_max_active_ (struct OperationQueue *queue,
278 unsigned int max_active)
280 struct QueueEntry *entry;
282 queue->max_active = max_active;
283 /* if (queue->active >= queue->max_active) */
287 while ( (queue->active > queue->max_active) &&
290 if (entry->op->state == OP_STATE_READY)
296 while ( (NULL != entry) &&
297 (queue->active < queue->max_active) )
299 if (OP_STATE_WAITING == entry->op->state)
300 check_readiness (entry->op);
307 * Add an operation to a queue. An operation can be in multiple queues at
308 * once. Once the operation is inserted into all the queues
309 * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
310 * waiting for the operation to become active.
312 * @param queue queue to add the operation to
313 * @param operation operation to add to the queue
316 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
317 struct GNUNET_TESTBED_Operation
320 struct QueueEntry *entry;
322 entry = GNUNET_malloc (sizeof (struct QueueEntry));
323 entry->op = operation;
324 GNUNET_CONTAINER_DLL_insert_tail (queue->head, queue->tail, entry);
325 GNUNET_array_append (operation->queues, operation->nqueues, queue);
330 * Marks the given operation as waiting on the queues. Once all queues permit
331 * the operation to become active, the operation will be activated. The actual
332 * activation will occur in a separate task (thus allowing multiple queue
333 * insertions to be made without having the first one instantly trigger the
334 * operation if the first queue has sufficient resources).
336 * @param operation the operation to marks as waiting
339 GNUNET_TESTBED_operation_begin_wait_ (struct GNUNET_TESTBED_Operation
342 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == operation->start_task_id);
343 operation->state = OP_STATE_WAITING;
344 check_readiness (operation);
349 * Remove an operation from a queue. This can be because the
350 * oeration was active and has completed (and the resources have
351 * been released), or because the operation was cancelled and
352 * thus scheduling the operation is no longer required.
354 * @param queue queue to add the operation to
355 * @param operation operation to add to the queue
358 GNUNET_TESTBED_operation_queue_remove_ (struct OperationQueue *queue,
359 struct GNUNET_TESTBED_Operation
362 struct QueueEntry *entry;
363 struct QueueEntry *entry2;
365 for (entry = queue->head; NULL != entry; entry = entry->next)
366 if (entry->op == operation)
368 GNUNET_assert (NULL != entry);
369 switch (operation->state)
372 case OP_STATE_WAITING:
375 case OP_STATE_STARTED:
376 GNUNET_assert (0 != queue->active);
380 entry2 = entry->next;
381 GNUNET_CONTAINER_DLL_remove (queue->head, queue->tail, entry);
383 for (; NULL != entry2; entry2 = entry2->next)
384 if (OP_STATE_WAITING == entry2->op->state)
388 check_readiness (entry2->op);
393 * An operation is 'done' (was cancelled or finished); remove
394 * it from the queues and release associated resources.
396 * @param operation operation that finished
399 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *operation)
403 if (GNUNET_SCHEDULER_NO_TASK != operation->start_task_id)
405 GNUNET_SCHEDULER_cancel (operation->start_task_id);
406 operation->start_task_id = GNUNET_SCHEDULER_NO_TASK;
408 for (i = 0; i < operation->nqueues; i++)
409 GNUNET_TESTBED_operation_queue_remove_ (operation->queues[i], operation);
410 GNUNET_free (operation->queues);
411 if (NULL != operation->release)
412 operation->release (operation->cb_cls);
413 GNUNET_free (operation);
417 /* end of testbed_api_operations.c */