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;
119 * Pointer to operation's data
129 * The id of the task which calls OperationStart for this operation
131 GNUNET_SCHEDULER_TaskIdentifier start_task_id;
134 * Number of queues in the operation queues array
136 unsigned int nqueues;
139 * The state of the operation
141 enum OperationState state;
144 * The type of the operation
146 enum OperationType type;
152 * Task for calling OperationStart on operation
154 * @param cls the Operation
155 * @param tc the TaskContext from scheduler
158 call_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 struct GNUNET_TESTBED_Operation *op = cls;
162 op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
163 op->state = OP_STATE_STARTED;
164 if (NULL != op->start)
166 op->start (op->cb_cls);
172 * Checks for the readiness of an operation and schedules a operation start task
174 * @param op the operation
177 check_readiness (struct GNUNET_TESTBED_Operation *op)
181 for (i = 0; i < op->nqueues; i++)
183 if (0 == op->queues[i]->active)
186 for (i = 0; i < op->nqueues; i++)
188 op->queues[i]->active--;
190 GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == op->start_task_id);
191 op->start_task_id = GNUNET_SCHEDULER_add_now (&call_start, op);
196 * Create an 'operation' to be performed.
198 * @param cls closure for the callbacks
199 * @param start function to call to start the operation
200 * @param release function to call to close down the operation
201 * @param type the type of the operation
202 * @param data operation's relavant data
203 * @return handle to the operation
205 struct GNUNET_TESTBED_Operation *
206 GNUNET_TESTBED_operation_create_ (void *cls,
207 OperationStart start,
208 OperationRelease release,
209 enum OperationType type,
212 struct GNUNET_TESTBED_Operation *op;
214 op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
216 op->release = release;
225 * Create an operation queue.
227 * @param max_active maximum number of operations in this
228 * queue that can be active in parallel at the same time
229 * @return handle to the queue
231 struct OperationQueue *
232 GNUNET_TESTBED_operation_queue_create_ (unsigned int max_active)
234 struct OperationQueue *queue;
236 queue = GNUNET_malloc (sizeof (struct OperationQueue));
237 queue->active = max_active;
243 * Destroy an operation queue. The queue MUST be empty
246 * @param queue queue to destroy
249 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
251 GNUNET_assert (NULL == queue->head);
252 GNUNET_assert (NULL == queue->tail);
258 * Add an operation to a queue. An operation can be in multiple
259 * queues at once. Once all queues permit the operation to become
260 * active, the operation will be activated. The actual activation
261 * will occur in a separate task (thus allowing multiple queue
262 * insertions to be made without having the first one instantly
263 * trigger the operation if the first queue has sufficient
266 * @param queue queue to add the operation to
267 * @param operation operation to add to the queue
270 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
271 struct GNUNET_TESTBED_Operation *operation)
273 struct QueueEntry *entry;
275 entry = GNUNET_malloc (sizeof (struct QueueEntry));
276 entry->op = operation;
277 GNUNET_CONTAINER_DLL_insert_tail (queue->head, queue->tail, entry);
279 GNUNET_realloc (operation->queues,
280 sizeof (struct OperationQueue *) * (++operation->nqueues));
281 operation->queues[operation->nqueues - 1] = queue;
282 check_readiness (operation);
287 * Remove an operation from a queue. This can be because the
288 * oeration was active and has completed (and the resources have
289 * been released), or because the operation was cancelled and
290 * thus scheduling the operation is no longer required.
292 * @param queue queue to add the operation to
293 * @param operation operation to add to the queue
296 GNUNET_TESTBED_operation_queue_remove_ (struct OperationQueue *queue,
297 struct GNUNET_TESTBED_Operation *operation)
299 struct QueueEntry *entry;
300 struct QueueEntry *entry2;
302 for (entry = queue->head; NULL != entry; entry = entry->next)
303 if (entry->op == operation)
305 GNUNET_assert (NULL != entry);
306 if (OP_STATE_STARTED == operation->state)
308 entry2 = entry->next;
309 GNUNET_CONTAINER_DLL_remove (queue->head, queue->tail, entry);
311 for (; NULL != entry2; entry2 = entry2->next)
312 if (OP_STATE_STARTED != entry2->op->state)
316 check_readiness (entry2->op);
321 * An operation is 'done' (was cancelled or finished); remove
322 * it from the queues and release associated resources.
324 * @param operation operation that finished
327 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *operation)
331 if (GNUNET_SCHEDULER_NO_TASK != operation->start_task_id)
333 GNUNET_SCHEDULER_cancel (operation->start_task_id);
334 operation->start_task_id = GNUNET_SCHEDULER_NO_TASK;
336 for (i = 0; i < operation->nqueues; i++)
337 GNUNET_TESTBED_operation_queue_remove_ (operation->queues[i], operation);
338 GNUNET_free (operation->queues);
339 if (NULL != operation->release)
340 operation->release (operation->cb_cls);
341 GNUNET_free (operation);
345 /* end of testbed_api_operations.c */