- test config fixes for adaptive overlay connects
[oweals/gnunet.git] / src / testbed / testbed_api_operations.c
1 /*
2       This file is part of GNUnet
3       (C) 2008--2012 Christian Grothoff (and other contributing authors)
4
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.
9
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.
14
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.
19  */
20
21 /**
22  * @file testbed/testbed_api_operations.c
23  * @brief functions to manage operation queues
24  * @author Christian Grothoff
25  */
26 #include "platform.h"
27 #include "testbed_api_operations.h"
28
29
30 /**
31  * An entry in the operation queue
32  */
33 struct QueueEntry
34 {
35   /**
36    * The next DLL pointer
37    */
38   struct QueueEntry *next;
39
40   /**
41    * The prev DLL pointer
42    */
43   struct QueueEntry *prev;
44
45   /**
46    * The operation this entry holds
47    */
48   struct GNUNET_TESTBED_Operation *op;
49 };
50
51
52 /**
53  * Queue of operations where we can only support a certain
54  * number of concurrent operations of a particular type.
55  */
56 struct OperationQueue
57 {
58  /**
59    * The head of the operation queue
60    */
61   struct QueueEntry *head;
62
63   /**
64    * The tail of the operation queue
65    */
66   struct QueueEntry *tail;
67
68   /**
69    * Number of operations that are currently active in this queue.
70    */
71   unsigned int active;
72
73   /**
74    * Max number of operations which can be active at any time in this queue
75    */
76   unsigned int max_active;
77
78 };
79
80
81 /**
82  * Operation state
83  */
84 enum OperationState
85 {
86   /**
87    * The operation is just created and is in initial state
88    */
89   OP_STATE_INIT,
90   
91   /**
92    * The operation is currently waiting for resources
93    */
94   OP_STATE_WAITING,
95   
96   /**
97    * The operation is ready to be started
98    */
99   OP_STATE_READY,
100   
101   /**
102    * The operation has started
103    */
104   OP_STATE_STARTED
105 };
106
107
108 /**
109  * Opaque handle to an abstract operation to be executed by the testing framework.
110  */
111 struct GNUNET_TESTBED_Operation
112 {
113   /**
114    * Function to call when we have the resources to begin the operation.
115    */
116   OperationStart start;
117
118   /**
119    * Function to call to clean up after the operation (which may or may
120    * not have been started yet).
121    */
122   OperationRelease release;
123
124   /**
125    * Closure for callbacks.
126    */
127   void *cb_cls;
128
129   /**
130    * Array of operation queues this Operation belongs to.
131    */
132   struct OperationQueue **queues;
133
134   /**
135    * The id of the task which calls OperationStart for this operation
136    */
137   GNUNET_SCHEDULER_TaskIdentifier start_task_id;
138
139   /**
140    * Number of queues in the operation queues array
141    */
142   unsigned int nqueues;
143
144   /**
145    * The state of the operation
146    */
147   enum OperationState state;
148
149 };
150
151
152 /**
153  * Task for calling OperationStart on operation
154  *
155  * @param cls the Operation
156  * @param tc the TaskContext from scheduler
157  */
158 static void
159 call_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
160 {
161   struct GNUNET_TESTBED_Operation *op = cls;
162
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);
167 }
168
169
170 /**
171  * Checks for the readiness of an operation and schedules a operation start task
172  *
173  * @param op the operation
174  */
175 static void
176 check_readiness (struct GNUNET_TESTBED_Operation *op)
177 {
178   unsigned int i;
179
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)
183       return;
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);
188 }
189
190
191 /**
192  * Create an 'operation' to be performed.
193  *
194  * @param cls closure for the callbacks
195  * @param start function to call to start the operation
196  * @param release function to call to close down the operation
197  * @return handle to the operation
198  */
199 struct GNUNET_TESTBED_Operation *
200 GNUNET_TESTBED_operation_create_ (void *cls, OperationStart start,
201                                   OperationRelease release)
202 {
203   struct GNUNET_TESTBED_Operation *op;
204
205   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
206   op->start = start;
207   op->state = OP_STATE_INIT;
208   op->release = release;
209   op->cb_cls = cls;
210   op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
211   return op;
212 }
213
214
215 /**
216  * Create an operation queue.
217  *
218  * @param max_active maximum number of operations in this
219  *        queue that can be active in parallel at the same time
220  * @return handle to the queue
221  */
222 struct OperationQueue *
223 GNUNET_TESTBED_operation_queue_create_ (unsigned int max_active)
224 {
225   struct OperationQueue *queue;
226
227   queue = GNUNET_malloc (sizeof (struct OperationQueue));
228   queue->max_active = max_active;
229   return queue;
230 }
231
232
233 /**
234  * Destroy an operation queue.  The queue MUST be empty
235  * at this time.
236  *
237  * @param queue queue to destroy
238  */
239 void
240 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
241 {
242   GNUNET_break (NULL == queue->head);
243   GNUNET_break (NULL == queue->tail);
244   GNUNET_free (queue);
245 }
246
247
248 /**
249  * Function to reset the maximum number of operations in the given queue. If
250  * max_active is lesser than the number of currently active operations, the
251  * active operations are not stopped immediately.
252  *
253  * @param queue the operation queue which has to be modified
254  * @param max_active the new maximum number of active operations
255  */
256 void
257 GNUNET_TESTBED_operation_queue_reset_max_active_ (struct OperationQueue *queue,
258                                                   unsigned int max_active)
259 {
260   struct QueueEntry *entry;
261   
262   queue->max_active = max_active;
263   if (queue->active >= queue->max_active)
264     return;
265   entry = queue->head;
266   while ( (NULL != entry) &&
267           (queue->active < queue->max_active) )
268   {
269     if (OP_STATE_WAITING == entry->op->state)
270       check_readiness (entry->op);
271     entry = entry->next;
272   }
273 }
274
275
276 /**
277  * Add an operation to a queue.  An operation can be in multiple queues at
278  * once. Once the operation is inserted into all the queues
279  * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
280  * waiting for the operation to become active.
281  *
282  * @param queue queue to add the operation to
283  * @param operation operation to add to the queue
284  */
285 void
286 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
287                                         struct GNUNET_TESTBED_Operation
288                                         *operation)
289 {
290   struct QueueEntry *entry;
291
292   entry = GNUNET_malloc (sizeof (struct QueueEntry));
293   entry->op = operation;
294   GNUNET_CONTAINER_DLL_insert_tail (queue->head, queue->tail, entry);
295   operation->queues =
296       GNUNET_realloc (operation->queues,
297                       sizeof (struct OperationQueue *) *
298                       (++operation->nqueues));
299   operation->queues[operation->nqueues - 1] = queue;
300 }
301
302
303 /**
304  * Marks the given operation as waiting on the queues.  Once all queues permit
305  * the operation to become active, the operation will be activated.  The actual
306  * activation will occur in a separate task (thus allowing multiple queue
307  * insertions to be made without having the first one instantly trigger the
308  * operation if the first queue has sufficient resources).
309  *
310  * @param operation the operation to marks as waiting
311  */
312 void
313 GNUNET_TESTBED_operation_begin_wait_ (struct GNUNET_TESTBED_Operation
314                                       *operation)
315 {
316   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == operation->start_task_id);
317   operation->state = OP_STATE_WAITING;
318   check_readiness (operation);
319 }
320
321
322 /**
323  * Remove an operation from a queue.  This can be because the
324  * oeration was active and has completed (and the resources have
325  * been released), or because the operation was cancelled and
326  * thus scheduling the operation is no longer required.
327  *
328  * @param queue queue to add the operation to
329  * @param operation operation to add to the queue
330  */
331 void
332 GNUNET_TESTBED_operation_queue_remove_ (struct OperationQueue *queue,
333                                         struct GNUNET_TESTBED_Operation
334                                         *operation)
335 {
336   struct QueueEntry *entry;
337   struct QueueEntry *entry2;
338
339   for (entry = queue->head; NULL != entry; entry = entry->next)
340     if (entry->op == operation)
341       break;
342   GNUNET_assert (NULL != entry);
343   if (OP_STATE_STARTED == operation->state)
344   {
345     GNUNET_assert (0 != queue->active);
346     queue->active--;
347   }
348   entry2 = entry->next;
349   GNUNET_CONTAINER_DLL_remove (queue->head, queue->tail, entry);
350   GNUNET_free (entry);
351   for (; NULL != entry2; entry2 = entry2->next)
352     if (OP_STATE_WAITING == entry2->op->state)
353       break;
354   if (NULL == entry2)
355     return;
356   check_readiness (entry2->op);
357 }
358
359
360 /**
361  * An operation is 'done' (was cancelled or finished); remove
362  * it from the queues and release associated resources.
363  *
364  * @param operation operation that finished
365  */
366 void
367 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *operation)
368 {
369   unsigned int i;
370
371   if (GNUNET_SCHEDULER_NO_TASK != operation->start_task_id)
372   {
373     GNUNET_SCHEDULER_cancel (operation->start_task_id);
374     operation->start_task_id = GNUNET_SCHEDULER_NO_TASK;
375   }
376   for (i = 0; i < operation->nqueues; i++)
377     GNUNET_TESTBED_operation_queue_remove_ (operation->queues[i], operation);
378   GNUNET_free (operation->queues);
379   if (NULL != operation->release)
380     operation->release (operation->cb_cls);
381   GNUNET_free (operation);
382 }
383
384
385 /* end of testbed_api_operations.c */