wip
[oweals/gnunet.git] / src / include / gnunet_scheduler_lib.h
1 /*
2       This file is part of GNUnet
3       (C) 2009 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 2, 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 include/gnunet_scheduler_lib.h
23  * @brief API to schedule computations using continuation passing style
24  * @author Christian Grothoff
25  */
26
27 #ifndef GNUNET_SCHEDULER_LIB_H
28 #define GNUNET_SCHEDULER_LIB_H
29
30 #ifdef __cplusplus
31 extern "C"
32 {
33 #if 0                           /* keep Emacsens' auto-indent happy */
34 }
35 #endif
36 #endif
37
38 /**
39  * Opaque reference to a task.
40  */
41 typedef unsigned long long GNUNET_SCHEDULER_TaskIdentifier;
42
43
44 /**
45  * Constant used to indicate that the scheduled
46  * task has no others as prerequisites.
47  */
48 #define GNUNET_SCHEDULER_NO_TASK ((GNUNET_SCHEDULER_TaskIdentifier) 0)
49
50 /**
51  * Reasons why the schedule may have triggered
52  * the task now.
53  */
54 enum GNUNET_SCHEDULER_Reason
55 {
56   /**
57    * This is the very first task run during startup.
58    */
59   GNUNET_SCHEDULER_REASON_STARTUP = 0,
60
61   /**
62    * We are shutting down and are running all shutdown-related tasks
63    * (regardless of timeout, etc.).
64    */
65   GNUNET_SCHEDULER_REASON_SHUTDOWN = 1,
66
67   /**
68    * The specified timeout has expired.
69    * (also set if the delay given was 0).
70    */
71   GNUNET_SCHEDULER_REASON_TIMEOUT = 2,
72
73   /**
74    * The reading socket is ready.
75    */
76   GNUNET_SCHEDULER_REASON_READ_READY = 4,
77
78   /**
79    * The writing socket is ready.
80    */
81   GNUNET_SCHEDULER_REASON_WRITE_READY = 8,
82
83   /**
84    * The prerequisite task is done.
85    */
86   GNUNET_SCHEDULER_REASON_PREREQ_DONE = 16
87 };
88
89
90 /**
91  * Valid task priorities.  Use these, do not
92  * pass random integers!
93  */
94 enum GNUNET_SCHEDULER_Priority
95 {
96   /**
97    * Run with the same priority as the current job.
98    */
99   GNUNET_SCHEDULER_PRIORITY_KEEP = 0,
100
101   /**
102    * Run when otherwise idle.
103    */
104   GNUNET_SCHEDULER_PRIORITY_IDLE = 1,
105
106   /**
107    * Run as background job (higher than idle,
108    * lower than default).
109    */
110   GNUNET_SCHEDULER_PRIORITY_BACKGROUND = 2,
111
112   /**
113    * Run with the default priority (normal
114    * P2P operations).  Higher than BACKGROUND.
115    */
116   GNUNET_SCHEDULER_PRIORITY_DEFAULT = 3,
117
118   /**
119    * Run with high priority (important requests).
120    * Higher than DEFAULT.
121    */
122   GNUNET_SCHEDULER_PRIORITY_HIGH = 4,
123
124   /**
125    * Run with priority for interactive tasks.
126    * Higher than "HIGH".
127    */
128   GNUNET_SCHEDULER_PRIORITY_UI = 5,
129
130   /**
131    * Run with priority for urgent tasks.  Use
132    * for things like aborts and shutdowns that
133    * need to preempt "UI"-level tasks.
134    * Higher than "UI".
135    */
136   GNUNET_SCHEDULER_PRIORITY_URGENT = 6,
137
138   /**
139    * This is an internal priority level that is only used for tasks
140    * that are being triggered due to shutdown (they have automatically
141    * highest priority).  User code must not use this priority level
142    * directly.  Tasks run with this priority level that internally
143    * schedule other tasks will see their original priority level
144    * be inherited (unless otherwise specified).
145    */
146   GNUNET_SCHEDULER_PRIORITY_SHUTDOWN = 7,
147
148   /**
149    * Number of priorities (must be the last priority).
150    * This priority must not be used by clients.
151    */
152   GNUNET_SCHEDULER_PRIORITY_COUNT = 8
153 };
154
155 #include "gnunet_time_lib.h"
156 #include "gnunet_network_lib.h"
157
158
159 /**
160  * Context information passed to each scheduler task.
161  */
162 struct GNUNET_SCHEDULER_TaskContext
163 {
164   /**
165    * Reason why the task is run now
166    */
167   enum GNUNET_SCHEDULER_Reason reason;
168
169   /**
170    * Set of file descriptors ready for reading;
171    * note that additional bits may be set
172    * that were not in the original request
173    */
174   const struct GNUNET_NETWORK_FDSet *read_ready;
175
176   /**
177    * Set of file descriptors ready for writing;
178    * note that additional bits may be set
179    * that were not in the original request.
180    */
181   const struct GNUNET_NETWORK_FDSet *write_ready;
182
183 };
184
185
186 /**
187  * Signature of the main function of a task.
188  *
189  * @param cls closure
190  * @param tc context information (why was this task triggered now)
191  */
192 typedef void (*GNUNET_SCHEDULER_Task) (void *cls,
193                                        const struct
194                                        GNUNET_SCHEDULER_TaskContext * tc);
195
196
197 /**
198  * Initialize and run scheduler.  This function will return when all
199  * tasks have completed.  On systems with signals, receiving a SIGTERM
200  * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
201  * to be run after the active task is complete.  As a result, SIGTERM
202  * causes all active tasks to be scheduled with reason
203  * "GNUNET_SCHEDULER_REASON_SHUTDOWN".  (However, tasks added
204  * afterwards will execute normally!).  Note that any particular
205  * signal will only shut down one scheduler; applications should
206  * always only create a single scheduler.
207  *
208  * @param task task to run first (and immediately)
209  * @param task_cls closure of task
210  */
211 void GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls);
212
213
214 /**
215  * Request the shutdown of a scheduler.  Marks all currently
216  * pending tasks as ready because of shutdown.  This will
217  * cause all tasks to run (as soon as possible, respecting
218  * priorities and prerequisite tasks).  Note that tasks
219  * scheduled AFTER this call may still be delayed arbitrarily.
220  */
221 void GNUNET_SCHEDULER_shutdown ();
222
223
224 /**
225  * Get information about the current load of this scheduler.  Use this
226  * function to determine if an elective task should be added or simply
227  * dropped (if the decision should be made based on the number of
228  * tasks ready to run).
229  *
230  * * @param p priority-level to query, use KEEP to query the level
231  *          of the current task, use COUNT to get the sum over
232  *          all priority levels
233  * @return number of tasks pending right now
234  */
235 unsigned int GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p);
236
237
238 /**
239  * Obtain the reason code for why the current task was
240  * started.  Will return the same value as 
241  * the GNUNET_SCHEDULER_TaskContext's reason field.
242  *
243  * * @return reason(s) why the current task is run
244  */
245 enum GNUNET_SCHEDULER_Reason
246 GNUNET_SCHEDULER_get_reason ();
247
248
249 /**
250  * Cancel the task with the specified identifier.
251  * The task must not yet have run.
252  *
253  * * @param task id of the task to cancel
254  * @return the closure of the callback of the cancelled task
255  */
256 void *GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task);
257
258
259 /**
260  * Continue the current execution with the given function.  This is
261  * similar to the other "add" functions except that there is no delay
262  * and the reason code can be specified.
263  *
264  * * @param task main function of the task
265  * @param task_cls closure of task
266  * @param reason reason for task invocation
267  */
268 void
269 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task,
270                                    void *task_cls,
271                                    enum GNUNET_SCHEDULER_Reason reason);
272
273
274 /**
275  * Schedule a new task to be run after the specified prerequisite task
276  * has completed. It will be run with the priority of the calling
277  * task.
278  *
279  * * @param prerequisite_task run this task after the task with the given
280  *        task identifier completes (and any of our other
281  *        conditions, such as delay, read or write-readiness
282  *        are satisfied).  Use  GNUNET_SCHEDULER_NO_TASK to not have any dependency
283  *        on completion of other tasks (this will cause the task to run as
284  *        soon as possible).
285  * @param task main function of the task
286  * @param task_cls closure of task
287  * @return unique task identifier for the job
288  *         only valid until "task" is started!
289  */
290 GNUNET_SCHEDULER_TaskIdentifier
291 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
292                             GNUNET_SCHEDULER_Task task,
293                             void *task_cls);
294
295
296 /**
297  * Schedule a new task to be run with a specified priority.
298  *
299  * * @param prio how important is the new task?
300  * @param task main function of the task
301  * @param task_cls closure of task
302  * @return unique task identifier for the job
303  *         only valid until "task" is started!
304  */
305 GNUNET_SCHEDULER_TaskIdentifier
306 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
307                                     GNUNET_SCHEDULER_Task task,
308                                     void *task_cls);
309
310
311 /**
312  * Schedule a new task to be run as soon as possible. The task
313  * will be run with the priority of the calling task.
314  *
315  * * @param task main function of the task
316  * @param task_cls closure of task
317  * @return unique task identifier for the job
318  *         only valid until "task" is started!
319  */
320 GNUNET_SCHEDULER_TaskIdentifier
321 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task,
322                           void *task_cls);
323
324
325 /**
326  * Schedule a new task to be run with a specified delay.  The task
327  * will be scheduled for execution once the delay has expired. It
328  * will be run with the priority of the calling task.
329  *
330  * * @param delay when should this operation time out? Use
331  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
332  * @param task main function of the task
333  * @param task_cls closure of task
334  * @return unique task identifier for the job
335  *         only valid until "task" is started!
336  */
337 GNUNET_SCHEDULER_TaskIdentifier
338 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
339                               GNUNET_SCHEDULER_Task task,
340                               void *task_cls);
341
342
343 /**
344  * Schedule a new task to be run with a specified delay or when the
345  * specified file descriptor is ready for reading.  The delay can be
346  * used as a timeout on the socket being ready.  The task will be
347  * scheduled for execution once either the delay has expired or the
348  * socket operation is ready.  It will be run with the priority of
349  * the calling task.
350  *
351  * * @param delay when should this operation time out? Use
352  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
353  * @param rfd read file-descriptor
354  * @param task main function of the task
355  * @param task_cls closure of task
356  * @return unique task identifier for the job
357  *         only valid until "task" is started!
358  */
359 GNUNET_SCHEDULER_TaskIdentifier
360 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
361                                struct GNUNET_NETWORK_Handle *rfd,
362                                GNUNET_SCHEDULER_Task task,
363                                void *task_cls);
364
365
366 /**
367  * Schedule a new task to be run with a specified delay or when the
368  * specified file descriptor is ready for writing.  The delay can be
369  * used as a timeout on the socket being ready.  The task will be
370  * scheduled for execution once either the delay has expired or the
371  * socket operation is ready.  It will be run with the priority of
372  * the calling task.
373  *
374  * * @param delay when should this operation time out? Use
375  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
376  * @param wfd write file-descriptor
377  * @param task main function of the task
378  * @param task_cls closure of task
379  * @return unique task identifier for the job
380  *         only valid until "task" is started!
381  */
382 GNUNET_SCHEDULER_TaskIdentifier
383 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
384                                 struct GNUNET_NETWORK_Handle *wfd, 
385                                 GNUNET_SCHEDULER_Task task, 
386                                 void *task_cls);
387
388
389 /**
390  * Schedule a new task to be run with a specified delay or when the
391  * specified file descriptor is ready for reading.  The delay can be
392  * used as a timeout on the socket being ready.  The task will be
393  * scheduled for execution once either the delay has expired or the
394  * socket operation is ready. It will be run with the priority of
395  * the calling task.
396  *
397  * * @param delay when should this operation time out? Use
398  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
399  * @param rfd read file-descriptor
400  * @param task main function of the task
401  * @param task_cls closure of task
402  * @return unique task identifier for the job
403  *         only valid until "task" is started!
404  */
405 GNUNET_SCHEDULER_TaskIdentifier
406 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
407                                 const struct GNUNET_DISK_FileHandle *rfd, 
408                                 GNUNET_SCHEDULER_Task task,
409                                 void *task_cls);
410
411
412 /**
413  * Schedule a new task to be run with a specified delay or when the
414  * specified file descriptor is ready for writing.  The delay can be
415  * used as a timeout on the socket being ready.  The task will be
416  * scheduled for execution once either the delay has expired or the
417  * socket operation is ready. It will be run with the priority of
418  * the calling task.
419  *
420  * * @param delay when should this operation time out? Use
421  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
422  * @param wfd write file-descriptor
423  * @param task main function of the task
424  * @param task_cls closure of task
425  * @return unique task identifier for the job
426  *         only valid until "task" is started!
427  */
428 GNUNET_SCHEDULER_TaskIdentifier
429 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
430                                  const struct GNUNET_DISK_FileHandle *wfd,
431                                  GNUNET_SCHEDULER_Task task, 
432                                  void *task_cls);
433
434
435 /**
436  * Schedule a new task to be run with a specified delay or when any of
437  * the specified file descriptor sets is ready.  The delay can be used
438  * as a timeout on the socket(s) being ready.  The task will be
439  * scheduled for execution once either the delay has expired or any of
440  * the socket operations is ready.  This is the most general
441  * function of the "add" family.  Note that the "prerequisite_task"
442  * must be satisfied in addition to any of the other conditions.  In
443  * other words, the task will be started when
444  * <code>
445  * (prerequisite-run)
446  * && (delay-ready
447  *     || any-rs-ready
448  *     || any-ws-ready
449  *     || shutdown-active)
450  * </code>
451  *
452  * * @param prio how important is this task?
453  * @param prerequisite_task run this task after the task with the given
454  *        task identifier completes (and any of our other
455  *        conditions, such as delay, read or write-readiness
456  *        are satisfied).  Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
457  *        on completion of other tasks.
458  * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
459  *        which means that the task will only be run after we receive SIGTERM
460  * @param rs set of file descriptors we want to read (can be NULL)
461  * @param ws set of file descriptors we want to write (can be NULL)
462  * @param task main function of the task
463  * @param task_cls closure of task
464  * @return unique task identifier for the job
465  *         only valid until "task" is started!
466  */
467 GNUNET_SCHEDULER_TaskIdentifier
468 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
469                              GNUNET_SCHEDULER_TaskIdentifier
470                              prerequisite_task,
471                              struct GNUNET_TIME_Relative delay,
472                              const struct GNUNET_NETWORK_FDSet * rs,
473                              const struct GNUNET_NETWORK_FDSet * ws,
474                              GNUNET_SCHEDULER_Task task, 
475                              void *task_cls);
476
477 #if 0                           /* keep Emacsens' auto-indent happy */
478 {
479 #endif
480 #ifdef __cplusplus
481 }
482 #endif
483
484 #endif