1692171e3da4f0e631c24342a3099b6e4ff6b40c
[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 enum OperationState
77   {
78     /**
79      * The operation is currently waiting for resources
80      */
81     OP_STATE_WAITING,
82
83     /**
84      * The operation has started
85      */
86     OP_STATE_STARTED,
87   };
88
89   
90
91
92 /**
93  * Opaque handle to an abstract operation to be executed by the testing framework.
94  */
95 struct GNUNET_TESTBED_Operation
96 {
97   /**
98    * Function to call when we have the resources to begin the operation.
99    */
100   OperationStart start;
101
102   /**
103    * Function to call to clean up after the operation (which may or may
104    * not have been started yet).
105    */
106   OperationRelease release;
107                                  
108   /**
109    * Closure for callbacks.
110    */
111   void *cb_cls;
112
113   /**
114    * Array of operation queues this Operation belongs to.
115    */
116   struct OperationQueue **queues;
117
118   /**
119    * Pointer to operation's data
120    */
121   void *data;
122   
123   /**
124    * The Operation ID
125    */
126   uint64_t id;  
127
128   /**
129    * The id of the task which calls OperationStart for this operation
130    */
131   GNUNET_SCHEDULER_TaskIdentifier start_task_id;
132
133   /**
134    * Number of queues in the operation queues array
135    */
136   unsigned int nqueues;
137
138   /**
139    * The state of the operation
140    */
141   enum OperationState state;  
142   
143   /**
144    * The type of the operation
145    */
146   enum OperationType type;
147   
148 };
149
150
151 /**
152  * Task for calling OperationStart on operation
153  *
154  * @param cls the Operation
155  * @param tc the TaskContext from scheduler
156  */
157 static void
158 call_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
159 {  
160   struct GNUNET_TESTBED_Operation *op = cls;
161   
162   op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
163   op->state = OP_STATE_STARTED;
164   if (NULL != op->start)
165   {
166     op->start (op->cb_cls);
167   }  
168 }
169
170
171 /**
172  * Checks for the readiness of an operation and schedules a operation start task
173  *
174  * @param op the operation
175  */
176 static void
177 check_readiness (struct GNUNET_TESTBED_Operation *op)
178 {   
179   unsigned int i;
180   
181   for (i = 0; i < op->nqueues; i++)
182   {
183     if (0 == op->queues[i]->active)
184       return;
185   }
186   for (i = 0; i < op->nqueues; i++)
187   {
188     op->queues[i]->active--;
189   }
190   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == op->start_task_id);
191   op->start_task_id = GNUNET_SCHEDULER_add_now (&call_start, op);  
192 }
193
194
195 /**
196  * Create an 'operation' to be performed.
197  *
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
204  */
205 struct GNUNET_TESTBED_Operation *
206 GNUNET_TESTBED_operation_create_ (void *cls,
207                                   OperationStart start,
208                                   OperationRelease release,
209                                   enum OperationType type, 
210                                   void *data)
211 {
212   struct GNUNET_TESTBED_Operation *op;
213
214   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
215   op->start = start;
216   op->release = release;
217   op->cb_cls = cls;
218   op->type = type;
219   op->data = data;
220   return op;  
221 }
222
223
224 /**
225  * Create an operation queue.
226  *
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
230  */
231 struct OperationQueue *
232 GNUNET_TESTBED_operation_queue_create_ (unsigned int max_active)
233 {
234   struct OperationQueue *queue;
235
236   queue = GNUNET_malloc (sizeof (struct OperationQueue));
237   queue->active = max_active;
238   return queue;
239 }
240
241
242 /**
243  * Destroy an operation queue.  The queue MUST be empty
244  * at this time.
245  *
246  * @param queue queue to destroy
247  */
248 void
249 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
250 {
251   GNUNET_assert (NULL == queue->head);
252   GNUNET_assert (NULL == queue->tail);
253   GNUNET_free (queue);
254 }
255
256
257 /**
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 
264  * resources).
265  *
266  * @param queue queue to add the operation to
267  * @param operation operation to add to the queue
268  */
269 void
270 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
271                                         struct GNUNET_TESTBED_Operation *operation)
272 {
273   struct QueueEntry *entry;
274
275   entry = GNUNET_malloc (sizeof (struct QueueEntry));
276   entry->op = operation;
277   GNUNET_CONTAINER_DLL_insert_tail (queue->head, queue->tail, entry);
278   operation->queues =
279     GNUNET_realloc (operation->queues,
280                     sizeof (struct OperationQueue *) * (++operation->nqueues));
281   operation->queues[operation->nqueues - 1] = queue;
282   check_readiness (operation);
283 }
284
285
286 /**
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.
291  *
292  * @param queue queue to add the operation to
293  * @param operation operation to add to the queue
294  */
295 void
296 GNUNET_TESTBED_operation_queue_remove_ (struct OperationQueue *queue,
297                                         struct GNUNET_TESTBED_Operation *operation)
298 {
299   struct QueueEntry *entry;
300   struct QueueEntry *entry2;
301   
302   for (entry = queue->head; NULL != entry; entry = entry->next)
303     if (entry->op == operation)
304       break;
305   GNUNET_assert (NULL != entry);
306   if (OP_STATE_STARTED == operation->state)
307     queue->active++;
308   entry2 = entry->next;
309   GNUNET_CONTAINER_DLL_remove (queue->head, queue->tail, entry);
310   GNUNET_free (entry);
311   for (; NULL != entry2; entry2 = entry2->next)
312     if (OP_STATE_STARTED != entry2->op->state)
313       break;
314   if (NULL == entry2)
315     return;
316   check_readiness (entry2->op);
317 }
318
319
320 /**
321  * An operation is 'done' (was cancelled or finished); remove
322  * it from the queues and release associated resources.
323  *
324  * @param operation operation that finished
325  */
326 void
327 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *operation)
328 {
329   unsigned int i;
330     
331   if (GNUNET_SCHEDULER_NO_TASK != operation->start_task_id)
332   {
333     GNUNET_SCHEDULER_cancel (operation->start_task_id);
334     operation->start_task_id = GNUNET_SCHEDULER_NO_TASK;
335   }
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);
342 }
343
344
345 /* end of testbed_api_operations.c */