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 can be concurrently
70 * active in this queue.
79 * The operation is currently waiting for resources
84 * The operation has started
93 * Opaque handle to an abstract operation to be executed by the testing framework.
95 struct GNUNET_TESTBED_Operation
98 * Function to call when we have the resources to begin the operation.
100 OperationStart start;
103 * Function to call to clean up after the operation (which may or may
104 * not have been started yet).
106 OperationRelease release;
109 * Closure for callbacks.
114 * Array of operation queues this Operation belongs to.
116 struct OperationQueue **queues;
124 * The id of the task which calls OperationStart for this operation
126 GNUNET_SCHEDULER_TaskIdentifier start_task_id;
129 * Number of queues in the operation queues array
131 unsigned int nqueues;
134 * The state of the operation
136 enum OperationState state;
139 * The type of the operation
141 enum OperationType type;
147 * Task for calling OperationStart on operation
149 * @param cls the Operation
150 * @param tc the TaskContext from scheduler
153 call_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
155 struct GNUNET_TESTBED_Operation *op = cls;
157 op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
158 op->state = OP_STATE_STARTED;
159 if (NULL != op->start)
161 op->start (op->cb_cls);
167 * Checks for the readiness of an operation and schedules a operation start task
169 * @param op the operation
172 check_readiness (struct GNUNET_TESTBED_Operation *op)
176 for (i = 0; i < op->nqueues; i++)
178 if (0 == op->queues[i]->active)
181 for (i = 0; i < op->nqueues; i++)
183 op->queues[i]->active--;
185 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == op->start_task_id);
186 op->start_task_id = GNUNET_SCHEDULER_add_now (&call_start, op);
191 * Create an 'operation' to be performed.
193 * @param cls closure for the callbacks
194 * @param start function to call to start the operation
195 * @param release function to call to close down the operation
196 * @param type the type of the operation
197 * @return handle to the operation
199 struct GNUNET_TESTBED_Operation *
200 GNUNET_TESTBED_operation_create_ (void *cls,
201 OperationStart start,
202 OperationRelease release,
203 enum OperationType type)
205 struct GNUNET_TESTBED_Operation *op;
207 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
209 op->release = release;
217 * Create an operation queue.
219 * @param max_active maximum number of operations in this
220 * queue that can be active in parallel at the same time
221 * @return handle to the queue
223 struct OperationQueue *
224 GNUNET_TESTBED_operation_queue_create_ (unsigned int max_active)
226 struct OperationQueue *queue;
228 queue = GNUNET_malloc (sizeof (struct OperationQueue));
229 queue->active = max_active;
235 * Destroy an operation queue. The queue MUST be empty
238 * @param queue queue to destroy
241 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
243 GNUNET_assert (NULL == queue->head);
244 GNUNET_assert (NULL == queue->tail);
250 * Add an operation to a queue. An operation can be in multiple
251 * queues at once. Once all queues permit the operation to become
252 * active, the operation will be activated. The actual activation
253 * will occur in a separate task (thus allowing multiple queue
254 * insertions to be made without having the first one instantly
255 * trigger the operation if the first queue has sufficient
258 * @param queue queue to add the operation to
259 * @param operation operation to add to the queue
262 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
263 struct GNUNET_TESTBED_Operation *operation)
265 struct QueueEntry *entry;
267 entry = GNUNET_malloc (sizeof (struct QueueEntry));
268 entry->op = operation;
269 GNUNET_CONTAINER_DLL_insert_tail (queue->head, queue->tail, entry);
271 GNUNET_realloc (operation->queues,
272 sizeof (struct OperationQueue *) * (++operation->nqueues));
273 operation->queues[operation->nqueues - 1] = queue;
274 check_readiness (operation);
279 * Remove an operation from a queue. This can be because the
280 * oeration was active and has completed (and the resources have
281 * been released), or because the operation was cancelled and
282 * thus scheduling the operation is no longer required.
284 * @param queue queue to add the operation to
285 * @param operation operation to add to the queue
288 GNUNET_TESTBED_operation_queue_remove_ (struct OperationQueue *queue,
289 struct GNUNET_TESTBED_Operation *operation)
291 struct QueueEntry *entry;
292 struct QueueEntry *entry2;
294 for (entry = queue->head; NULL != entry; entry = entry->next)
295 if (entry->op == operation)
297 GNUNET_assert (NULL != entry);
298 if (OP_STATE_STARTED == operation->state)
300 entry2 = entry->next;
301 GNUNET_CONTAINER_DLL_remove (queue->head, queue->tail, entry);
303 for (; NULL != entry2; entry2 = entry2->next)
304 if (OP_STATE_STARTED != entry2->op->state)
308 check_readiness (entry2->op);
313 * An operation is 'done' (was cancelled or finished); remove
314 * it from the queues and release associated resources.
316 * @param operation operation that finished
319 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *operation)
323 if (GNUNET_SCHEDULER_NO_TASK != operation->start_task_id)
325 GNUNET_SCHEDULER_cancel (operation->start_task_id);
326 operation->start_task_id = GNUNET_SCHEDULER_NO_TASK;
328 for (i = 0; i < operation->nqueues; i++)
329 GNUNET_TESTBED_operation_queue_remove_ (operation->queues[i], operation);
330 GNUNET_free (operation->queues);
331 if (NULL != operation->release)
332 operation->release (operation->cb_cls);
333 GNUNET_free (operation);
337 /* end of testbed_api_operations.c */