peer destroy with new operations handling
[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    * The id of the task which calls OperationStart for this operation
120    */
121   GNUNET_SCHEDULER_TaskIdentifier start_task_id;
122
123   /**
124    * Number of queues in the operation queues array
125    */
126   unsigned int nqueues;
127
128   /**
129    * The state of the operation
130    */
131   enum OperationState state;  
132   
133 };
134
135
136 /**
137  * Task for calling OperationStart on operation
138  *
139  * @param cls the Operation
140  * @param tc the TaskContext from scheduler
141  */
142 static void
143 call_start (void *cls, const struct GNUNET_SCHEDULER_TaskContext *tc)
144 {  
145   struct GNUNET_TESTBED_Operation *op = cls;
146   
147   op->start_task_id = GNUNET_SCHEDULER_NO_TASK;
148   op->state = OP_STATE_STARTED;
149   if (NULL != op->start)
150   {
151     op->start (op->cb_cls);
152   }  
153 }
154
155
156 /**
157  * Checks for the readiness of an operation and schedules a operation start task
158  *
159  * @param op the operation
160  */
161 static void
162 check_readiness (struct GNUNET_TESTBED_Operation *op)
163 {   
164   unsigned int i;
165   
166   for (i = 0; i < op->nqueues; i++)
167   {
168     if (0 == op->queues[i]->active)
169       return;
170   }
171   for (i = 0; i < op->nqueues; i++)
172   {
173     op->queues[i]->active--;
174   }
175   GNUNET_assert (GNUNET_SCHEDULER_NO_TASK == op->start_task_id);
176   op->start_task_id = GNUNET_SCHEDULER_add_now (&call_start, op);  
177 }
178
179
180 /**
181  * Create an 'operation' to be performed.
182  *
183  * @param cls closure for the callbacks
184  * @param start function to call to start the operation
185  * @param release function to call to close down the operation
186  * @return handle to the operation
187  */
188 struct GNUNET_TESTBED_Operation *
189 GNUNET_TESTBED_operation_create_ (void *cls,
190                                   OperationStart start,
191                                   OperationRelease release)
192 {
193   struct GNUNET_TESTBED_Operation *op;
194
195   op = GNUNET_malloc (sizeof (struct GNUNET_TESTBED_Operation));
196   op->start = start;
197   op->release = release;
198   op->cb_cls = cls;
199   return op;  
200 }
201
202
203 /**
204  * Create an operation queue.
205  *
206  * @param max_active maximum number of operations in this
207  *        queue that can be active in parallel at the same time
208  * @return handle to the queue
209  */
210 struct OperationQueue *
211 GNUNET_TESTBED_operation_queue_create_ (unsigned int max_active)
212 {
213   struct OperationQueue *queue;
214
215   queue = GNUNET_malloc (sizeof (struct OperationQueue));
216   queue->active = max_active;
217   return queue;
218 }
219
220
221 /**
222  * Destroy an operation queue.  The queue MUST be empty
223  * at this time.
224  *
225  * @param queue queue to destroy
226  */
227 void
228 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue)
229 {
230   GNUNET_break (NULL == queue->head);
231   GNUNET_break (NULL == queue->tail);
232   GNUNET_free (queue);
233 }
234
235
236 /**
237  * Add an operation to a queue.  An operation can be in multiple
238  * queues at once.  Once all queues permit the operation to become
239  * active, the operation will be activated.  The actual activation
240  * will occur in a separate task (thus allowing multiple queue 
241  * insertions to be made without having the first one instantly
242  * trigger the operation if the first queue has sufficient 
243  * resources).
244  *
245  * @param queue queue to add the operation to
246  * @param operation operation to add to the queue
247  */
248 void
249 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
250                                         struct GNUNET_TESTBED_Operation *operation)
251 {
252   struct QueueEntry *entry;
253
254   entry = GNUNET_malloc (sizeof (struct QueueEntry));
255   entry->op = operation;
256   GNUNET_CONTAINER_DLL_insert_tail (queue->head, queue->tail, entry);
257   operation->queues =
258     GNUNET_realloc (operation->queues,
259                     sizeof (struct OperationQueue *) * (++operation->nqueues));
260   operation->queues[operation->nqueues - 1] = queue;
261   check_readiness (operation);
262 }
263
264
265 /**
266  * Remove an operation from a queue.  This can be because the
267  * oeration was active and has completed (and the resources have
268  * been released), or because the operation was cancelled and
269  * thus scheduling the operation is no longer required.
270  *
271  * @param queue queue to add the operation to
272  * @param operation operation to add to the queue
273  */
274 void
275 GNUNET_TESTBED_operation_queue_remove_ (struct OperationQueue *queue,
276                                         struct GNUNET_TESTBED_Operation *operation)
277 {
278   struct QueueEntry *entry;
279   struct QueueEntry *entry2;
280   
281   for (entry = queue->head; NULL != entry; entry = entry->next)
282     if (entry->op == operation)
283       break;
284   GNUNET_assert (NULL != entry);
285   if (OP_STATE_STARTED == operation->state)
286     queue->active++;
287   entry2 = entry->next;
288   GNUNET_CONTAINER_DLL_remove (queue->head, queue->tail, entry);
289   GNUNET_free (entry);
290   for (; NULL != entry2; entry2 = entry2->next)
291     if (OP_STATE_STARTED != entry2->op->state)
292       break;
293   if (NULL == entry2)
294     return;
295   check_readiness (entry2->op);
296 }
297
298
299 /**
300  * An operation is 'done' (was cancelled or finished); remove
301  * it from the queues and release associated resources.
302  *
303  * @param operation operation that finished
304  */
305 void
306 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *operation)
307 {
308   unsigned int i;
309     
310   if (GNUNET_SCHEDULER_NO_TASK != operation->start_task_id)
311   {
312     GNUNET_SCHEDULER_cancel (operation->start_task_id);
313     operation->start_task_id = GNUNET_SCHEDULER_NO_TASK;
314   }
315   for (i = 0; i < operation->nqueues; i++)
316     GNUNET_TESTBED_operation_queue_remove_ (operation->queues[i], operation);
317   GNUNET_free (operation->queues);
318   if (NULL != operation->release)
319     operation->release (operation->cb_cls);
320   GNUNET_free (operation);
321 }
322
323
324 /* end of testbed_api_operations.c */