Merge branch 'master' of gnunet.org:gnunet
[oweals/gnunet.git] / src / testbed / testbed_api_operations.h
1 /*
2       This file is part of GNUnet
3       Copyright (C) 2008--2013 GNUnet e.V.
4
5       GNUnet is free software: you can redistribute it and/or modify it
6       under the terms of the GNU Affero General Public License as published
7       by the Free Software Foundation, either version 3 of the License,
8       or (at your 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       Affero General Public License for more details.
14      
15       You should have received a copy of the GNU Affero General Public License
16       along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 /**
20  * @file testbed/testbed_api_operations.h
21  * @brief internal API to access the 'operations' subsystem
22  * @author Christian Grothoff
23  */
24 #ifndef NEW_TESTING_API_OPERATIONS_H
25 #define NEW_TESTING_API_OPERATIONS_H
26
27 #include "gnunet_testbed_service.h"
28 #include "gnunet_helper_lib.h"
29
30
31 /**
32  * Queue of operations where we can only support a certain
33  * number of concurrent operations of a particular type.
34  */
35 struct OperationQueue;
36
37
38 /**
39  * The type of operation queue
40  */
41 enum OperationQueueType
42 {
43   /**
44    * Operation queue which permits a fixed maximum number of operations to be
45    * active at any time
46    */
47   OPERATION_QUEUE_TYPE_FIXED,
48
49   /**
50    * Operation queue which adapts the number of operations to be active based on
51    * the operation completion times of previously executed operation in it
52    */
53   OPERATION_QUEUE_TYPE_ADAPTIVE
54 };
55
56
57 /**
58  * Create an operation queue.
59  *
60  * @param type the type of operation queue
61  * @param max_active maximum number of operations in this queue that can be
62  *   active in parallel at the same time.
63  * @return handle to the queue
64  */
65 struct OperationQueue *
66 GNUNET_TESTBED_operation_queue_create_ (enum OperationQueueType type,
67                                         unsigned int max_active);
68
69
70 /**
71  * Destroy an operation queue.  The queue MUST be empty
72  * at this time.
73  *
74  * @param queue queue to destroy
75  */
76 void
77 GNUNET_TESTBED_operation_queue_destroy_ (struct OperationQueue *queue);
78
79
80 /**
81  * Destroys the operation queue if it is empty.  If not empty return GNUNET_NO.
82  *
83  * @param queue the queue to destroy if empty
84  * @return GNUNET_YES if the queue is destroyed.  GNUNET_NO if not (because it
85  *           is not empty)
86  */
87 int
88 GNUNET_TESTBED_operation_queue_destroy_empty_ (struct OperationQueue *queue);
89
90
91 /**
92  * Function to reset the maximum number of operations in the given queue. If
93  * max_active is lesser than the number of currently active operations, the
94  * active operations are not stopped immediately.
95  *
96  * @param queue the operation queue which has to be modified
97  * @param max_active the new maximum number of active operations
98  */
99 void
100 GNUNET_TESTBED_operation_queue_reset_max_active_ (struct OperationQueue *queue,
101                                                   unsigned int max_active);
102
103
104 /**
105  * Add an operation to a queue.  An operation can be in multiple queues at
106  * once. Once the operation is inserted into all the queues
107  * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
108  * waiting for the operation to become active.
109  *
110  * @param queue queue to add the operation to
111  * @param op operation to add to the queue
112  * @param nres the number of units of the resources of queue needed by the
113  *          operation. Should be greater than 0.
114  */
115 void
116 GNUNET_TESTBED_operation_queue_insert2_ (struct OperationQueue *queue,
117                                          struct GNUNET_TESTBED_Operation *op,
118                                          unsigned int nres);
119
120
121 /**
122  * Add an operation to a queue.  An operation can be in multiple queues at
123  * once. Once the operation is inserted into all the queues
124  * GNUNET_TESTBED_operation_begin_wait_() has to be called to actually start
125  * waiting for the operation to become active.
126  *
127  * @param queue queue to add the operation to
128  * @param op operation to add to the queue
129  */
130 void
131 GNUNET_TESTBED_operation_queue_insert_ (struct OperationQueue *queue,
132                                         struct GNUNET_TESTBED_Operation *op);
133
134
135 /**
136  * Marks the given operation as waiting on the queues.  Once all queues permit
137  * the operation to become active, the operation will be activated.  The actual
138  * activation will occur in a separate task (thus allowing multiple queue
139  * insertions to be made without having the first one instantly trigger the
140  * operation if the first queue has sufficient resources).
141  *
142  * @param op the operation to marks as waiting
143  */
144 void
145 GNUNET_TESTBED_operation_begin_wait_ (struct GNUNET_TESTBED_Operation *op);
146
147
148 /**
149  * Function to call to start an operation once all
150  * queues the operation is part of declare that the
151  * operation can be activated.
152  *
153  * @param cls the closure from GNUNET_TESTBED_operation_create_()
154  */
155 typedef void (*OperationStart) (void *cls);
156
157
158 /**
159  * Function to call to cancel an operation (release all associated
160  * resources).  This can be because of a call to
161  * "GNUNET_TESTBED_operation_cancel" (before the operation generated
162  * an event) or AFTER the operation generated an event due to a call
163  * to "GNUNET_TESTBED_operation_done".  Thus it is not guaranteed that
164  * a callback to the 'OperationStart' preceeds the call to
165  * 'OperationRelease'.  Implementations of this function are expected
166  * to clean up whatever state is in 'cls' and release all resources
167  * associated with the operation.
168  *
169  * @param cls the closure from GNUNET_TESTBED_operation_create_()
170  */
171 typedef void (*OperationRelease) (void *cls);
172
173
174 /**
175  * Create an 'operation' to be performed.
176  *
177  * @param cls closure for the callbacks
178  * @param start function to call to start the operation
179  * @param release function to call to close down the operation
180  * @return handle to the operation
181  */
182 struct GNUNET_TESTBED_Operation *
183 GNUNET_TESTBED_operation_create_ (void *cls, OperationStart start,
184                                   OperationRelease release);
185
186
187 /**
188  * An operation is 'done' (was cancelled or finished); remove
189  * it from the queues and release associated resources.
190  *
191  * @param op operation that finished
192  */
193 void
194 GNUNET_TESTBED_operation_release_ (struct GNUNET_TESTBED_Operation *op);
195
196
197 /**
198  * Marks an active operation as inactive - the operation will be kept in a
199  * ready-to-be-released state and continues to hold resources until another
200  * operation contents for them.
201  *
202  * @param op the operation to be marked as inactive.  The operation start
203  *          callback should have been called before for this operation to mark
204  *          it as inactive.
205  */
206 void
207 GNUNET_TESTBED_operation_inactivate_ (struct GNUNET_TESTBED_Operation *op);
208
209
210 /**
211  * Marks and inactive operation as active.  This fuction should be called to
212  * ensure that the oprelease callback will not be called until it is either
213  * marked as inactive or released.
214  *
215  * @param op the operation to be marked as active
216  */
217 void
218 GNUNET_TESTBED_operation_activate_ (struct GNUNET_TESTBED_Operation *op);
219
220
221 /**
222  * Marks an operation as failed
223  *
224  * @param op the operation to be marked as failed
225  */
226 void
227 GNUNET_TESTBED_operation_mark_failed (struct GNUNET_TESTBED_Operation *op);
228
229
230 #endif
231 /* end of testbed_api_operations.h */