fix memleak
[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 can be concurrently
70    * active in this queue.
71    */
72   unsigned int active;
73 };
74
75
76 /**
77  * Operation state
78  */
79 enum OperationState
80 {
81   /**
82    * The operation is just created and is in initial state
83    */
84   OP_STATE_INIT,
85   
86   /**
87    * The operation is currently waiting for resources
88    */
89   OP_STATE_WAITING,
90   
91   /**
92    * The operation is ready to be started
93    */
94   OP_STATE_READY,
95   
96   /**
97    * The operation has started
98    */
99   OP_STATE_STARTED
100 };
101
102
103 /**
104  * Opaque handle to an abstract operation to be executed by the testing framework.
105  */
106 struct GNUNET_TESTBED_Operation
107 {
108   /**
109    * Function to call when we have the resources to begin the operation.
110    */
111   OperationStart start;
112
113   /**
114    * Function to call to clean up after the operation (which may or may
115    * not have been started yet).
116    */
117   OperationRelease release;
118
119   /**
120    * Closure for callbacks.
121    */
122   void *cb_cls;
123
124   /**
125    * Array of operation queues this Operation belongs to.
126    */
127   struct OperationQueue **queues;
128
129   /**
130    * The id of the task which calls OperationStart for this operation
131    */
132   GNUNET_SCHEDULER_TaskIdentifier start_task_id;
133
134   /**
135    * Number of queues in the operation queues array
136    */
137   unsigned int nqueues;
138
139   /**
140    * The state of the operation
141    */
142   enum OperationState state;
143
144 };
145
146
147 /**
148  * Task for calling OperationStart on operation
149  *
150  * @param cls the Operation
151  * @param tc the TaskContext from scheduler
152  */
153 static void
154 call_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
155 {
156   struct GNUNET_TESTBED_Operation *op = cls;
157
158   op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
159   op->state = OP_STATE_STARTED;
160   if (NULL != op->start)
161   {
162     op->start (op->cb_cls);
163   }
164 }
165
166
167 /**
168  * Checks for the readiness of an operation and schedules a operation start task
169  *
170  * @param op the operation
171  */
172 static void
173 check_readiness (struct GNUNET_TESTBED_Operation *op)
174 {
175   unsigned int i;
176
177   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == op->start_task_id);
178   for (i = 0; i < op->nqueues; i++)
179   {
180     if (0 == op->queues[i]->active)
181       return;
182   }
183   for (i = 0; i < op->nqueues; i++)
184   {
185     op->queues[i]->active--;
186   }
187   op->state = OP_STATE_READY;
188   op->start_task_id = GNUNET_SCHEDULER_add_now (&call_start, op);
189 }
190
191
192 /**
193  * Create an 'operation' to be performed.
194  *
195  * @param cls closure for the callbacks
196  * @param start function to call to start the operation
197  * @param release function to call to close down the operation
198  * @return handle to the operation
199  */
200 struct GNUNET_TESTBED_Operation *
201 GNUNET_TESTBED_operation_create_ (void *cls, OperationStart start,
202                                   OperationRelease release)
203 {
204   struct GNUNET_TESTBED_Operation *op;
205
206   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
207   op->start = start;
208   op->state = OP_STATE_INIT;
209   op->release = release;
210   op->cb_cls = cls;
211   op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
212   return op;
213 }
214
215
216 /**
217  * Create an operation queue.
218  *
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
222  */
223 struct OperationQueue *
224 GNUNET_TESTBED_operation_queue_create_ (unsigned int max_active)
225 {
226   struct OperationQueue *queue;
227
228   queue = GNUNET_malloc (sizeof (struct OperationQueue));
229   queue->active = max_active;
230   return queue;
231 }
232
233
234 /**
235  * Destroy an operation queue.  The queue MUST be empty
236  * at this time.
237  *
238  * @param queue queue to destroy
239  */
240 void
241 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
242 {
243   GNUNET_break (NULL == queue->head);
244   GNUNET_break (NULL == queue->tail);
245   GNUNET_free (queue);
246 }
247
248
249 /**
250  * Add an operation to a queue.  An operation can be in multiple queues at
251  * once. Once the operation is inserted into all the queues
252  * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
253  * waiting for the operation to become active.
254  *
255  * @param queue queue to add the operation to
256  * @param operation operation to add to the queue
257  */
258 void
259 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
260                                         struct GNUNET_TESTBED_Operation
261                                         *operation)
262 {
263   struct QueueEntry *entry;
264
265   entry = GNUNET_malloc (sizeof (struct QueueEntry));
266   entry->op = operation;
267   GNUNET_CONTAINER_DLL_insert_tail (queue->head, queue->tail, entry);
268   operation->queues =
269       GNUNET_realloc (operation->queues,
270                       sizeof (struct OperationQueue *) *
271                       (++operation->nqueues));
272   operation->queues[operation->nqueues - 1] = queue;
273 }
274
275
276 /**
277  * Marks the given operation as waiting on the queues.  Once all queues permit
278  * the operation to become active, the operation will be activated.  The actual
279  * activation will occur in a separate task (thus allowing multiple queue
280  * insertions to be made without having the first one instantly trigger the
281  * operation if the first queue has sufficient resources).
282  *
283  * @param operation the operation to marks as waiting
284  */
285 void
286 GNUNET_TESTBED_operation_begin_wait_ (struct GNUNET_TESTBED_Operation
287                                       *operation)
288 {
289   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == operation->start_task_id);
290   operation->state = OP_STATE_WAITING;
291   check_readiness (operation);
292 }
293
294
295 /**
296  * Remove an operation from a queue.  This can be because the
297  * oeration was active and has completed (and the resources have
298  * been released), or because the operation was cancelled and
299  * thus scheduling the operation is no longer required.
300  *
301  * @param queue queue to add the operation to
302  * @param operation operation to add to the queue
303  */
304 void
305 GNUNET_TESTBED_operation_queue_remove_ (struct OperationQueue *queue,
306                                         struct GNUNET_TESTBED_Operation
307                                         *operation)
308 {
309   struct QueueEntry *entry;
310   struct QueueEntry *entry2;
311
312   for (entry = queue->head; NULL != entry; entry = entry->next)
313     if (entry->op == operation)
314       break;
315   GNUNET_assert (NULL != entry);
316   if (OP_STATE_STARTED == operation->state)
317     queue->active++;
318   entry2 = entry->next;
319   GNUNET_CONTAINER_DLL_remove (queue->head, queue->tail, entry);
320   GNUNET_free (entry);
321   for (; NULL != entry2; entry2 = entry2->next)
322     if (OP_STATE_WAITING == entry2->op->state)
323       break;
324   if (NULL == entry2)
325     return;
326   check_readiness (entry2->op);
327 }
328
329
330 /**
331  * An operation is 'done' (was cancelled or finished); remove
332  * it from the queues and release associated resources.
333  *
334  * @param operation operation that finished
335  */
336 void
337 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *operation)
338 {
339   unsigned int i;
340
341   if (GNUNET_SCHEDULER_NO_TASK != operation->start_task_id)
342   {
343     GNUNET_SCHEDULER_cancel (operation->start_task_id);
344     operation->start_task_id = GNUNET_SCHEDULER_NO_TASK;
345   }
346   for (i = 0; i < operation->nqueues; i++)
347     GNUNET_TESTBED_operation_queue_remove_ (operation->queues[i], operation);
348   GNUNET_free (operation->queues);
349   if (NULL != operation->release)
350     operation->release (operation->cb_cls);
351   GNUNET_free (operation);
352 }
353
354
355 /* end of testbed_api_operations.c */