curly wars / auto-indentation
[oweals/gnunet.git] / src / include / gnunet_scheduler_lib.h
1 /*
2       This file is part of GNUnet
3       (C) 2009, 2011 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 GNUNET_SCHEDULER_TaskContext
194                                        * tc);
195
196
197 /**
198  * Signature of the select function used by the scheduler.
199  * GNUNET_NETWORK_socket_select matches it.
200  *
201  * @param cls closure
202  * @param rfds set of sockets to be checked for readability
203  * @param wfds set of sockets to be checked for writability
204  * @param efds set of sockets to be checked for exceptions
205  * @param timeout relative value when to return
206  * @return number of selected sockets, GNUNET_SYSERR on error
207  */
208 typedef int (*GNUNET_SCHEDULER_select) (void *cls,
209                                         struct GNUNET_NETWORK_FDSet * rfds,
210                                         struct GNUNET_NETWORK_FDSet * wfds,
211                                         struct GNUNET_NETWORK_FDSet * efds,
212                                         struct GNUNET_TIME_Relative timeout);
213 /**
214  * Initialize and run scheduler.  This function will return when all
215  * tasks have completed.  On systems with signals, receiving a SIGTERM
216  * (and other similar signals) will cause "GNUNET_SCHEDULER_shutdown"
217  * to be run after the active task is complete.  As a result, SIGTERM
218  * causes all active tasks to be scheduled with reason
219  * "GNUNET_SCHEDULER_REASON_SHUTDOWN".  (However, tasks added
220  * afterwards will execute normally!).  Note that any particular
221  * signal will only shut down one scheduler; applications should
222  * always only create a single scheduler.
223  *
224  * @param task task to run first (and immediately)
225  * @param task_cls closure of task
226  */
227 void
228 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls);
229
230
231 /**
232  * Request the shutdown of a scheduler.  Marks all currently
233  * pending tasks as ready because of shutdown.  This will
234  * cause all tasks to run (as soon as possible, respecting
235  * priorities and prerequisite tasks).  Note that tasks
236  * scheduled AFTER this call may still be delayed arbitrarily.
237  */
238 void
239 GNUNET_SCHEDULER_shutdown ();
240
241
242 /**
243  * Get information about the current load of this scheduler.  Use this
244  * function to determine if an elective task should be added or simply
245  * dropped (if the decision should be made based on the number of
246  * tasks ready to run).
247  *
248  * * @param p priority-level to query, use KEEP to query the level
249  *          of the current task, use COUNT to get the sum over
250  *          all priority levels
251  * @return number of tasks pending right now
252  */
253 unsigned int
254 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p);
255
256
257 /**
258  * Obtain the reason code for why the current task was
259  * started.  Will return the same value as
260  * the GNUNET_SCHEDULER_TaskContext's reason field.
261  *
262  * * @return reason(s) why the current task is run
263  */
264 enum GNUNET_SCHEDULER_Reason
265 GNUNET_SCHEDULER_get_reason ();
266
267
268 /**
269  * Cancel the task with the specified identifier.
270  * The task must not yet have run.
271  *
272  * * @param task id of the task to cancel
273  * @return the closure of the callback of the cancelled task
274  */
275 void *
276 GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task);
277
278
279 /**
280  * Continue the current execution with the given function.  This is
281  * similar to the other "add" functions except that there is no delay
282  * and the reason code can be specified.
283  *
284  * * @param task main function of the task
285  * @param task_cls closure of task
286  * @param reason reason for task invocation
287  */
288 void
289 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task, void *task_cls,
290                                    enum GNUNET_SCHEDULER_Reason reason);
291
292
293 /**
294  * Schedule a new task to be run after the specified prerequisite task
295  * has completed. It will be run with the priority of the calling
296  * task.
297  *
298  * * @param prerequisite_task run this task after the task with the given
299  *        task identifier completes (and any of our other
300  *        conditions, such as delay, read or write-readiness
301  *        are satisfied).  Use  GNUNET_SCHEDULER_NO_TASK to not have any dependency
302  *        on completion of other tasks (this will cause the task to run as
303  *        soon as possible).
304  * @param task main function of the task
305  * @param task_cls closure of task
306  * @return unique task identifier for the job
307  *         only valid until "task" is started!
308  */
309 GNUNET_SCHEDULER_TaskIdentifier
310 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
311                             GNUNET_SCHEDULER_Task task, void *task_cls);
312
313
314 /**
315  * Schedule a new task to be run with a specified priority.
316  *
317  * * @param prio how important is the new task?
318  * @param task main function of the task
319  * @param task_cls closure of task
320  * @return unique task identifier for the job
321  *         only valid until "task" is started!
322  */
323 GNUNET_SCHEDULER_TaskIdentifier
324 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
325                                     GNUNET_SCHEDULER_Task task, void *task_cls);
326
327
328 /**
329  * Schedule a new task to be run as soon as possible. The task
330  * will be run with the priority of the calling task.
331  *
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_now (GNUNET_SCHEDULER_Task task, void *task_cls);
339
340
341 /**
342  * Schedule a new task to be run as soon as possible with the
343  * (transitive) ignore-shutdown flag either explicitly set or
344  * explicitly enabled.  This task (and all tasks created from it,
345  * other than by another call to this function) will either count or
346  * not count for the 'lifeness' of the process.  This API is only
347  * useful in a few special cases.
348  *
349  * @param lifeness GNUNET_YES if the task counts for lifeness, GNUNET_NO if not.
350  * @param task main function of the task
351  * @param task_cls closure of task
352  * @return unique task identifier for the job
353  *         only valid until "task" is started!
354  */
355 GNUNET_SCHEDULER_TaskIdentifier
356 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
357                                         GNUNET_SCHEDULER_Task task,
358                                         void *task_cls);
359
360
361 /**
362  * Schedule a new task to be run with a specified delay.  The task
363  * will be scheduled for execution once the delay has expired. It
364  * will be run with the priority of the calling task.
365  *
366  * * @param delay when should this operation time out? Use
367  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
368  * @param task main function of the task
369  * @param task_cls closure of task
370  * @return unique task identifier for the job
371  *         only valid until "task" is started!
372  */
373 GNUNET_SCHEDULER_TaskIdentifier
374 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
375                               GNUNET_SCHEDULER_Task task, void *task_cls);
376
377
378 /**
379  * Schedule a new task to be run with a specified delay or when the
380  * specified file descriptor is ready for reading.  The delay can be
381  * used as a timeout on the socket being ready.  The task will be
382  * scheduled for execution once either the delay has expired or the
383  * socket operation is ready.  It will be run with the priority of
384  * the calling task.
385  *
386  * * @param delay when should this operation time out? Use
387  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
388  * @param rfd read file-descriptor
389  * @param task main function of the task
390  * @param task_cls closure of task
391  * @return unique task identifier for the job
392  *         only valid until "task" is started!
393  */
394 GNUNET_SCHEDULER_TaskIdentifier
395 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
396                                struct GNUNET_NETWORK_Handle *rfd,
397                                GNUNET_SCHEDULER_Task task, void *task_cls);
398
399
400 /**
401  * Schedule a new task to be run with a specified delay or when the
402  * specified file descriptor is ready for writing.  The delay can be
403  * used as a timeout on the socket being ready.  The task will be
404  * scheduled for execution once either the delay has expired or the
405  * socket operation is ready.  It will be run with the priority of
406  * the calling task.
407  *
408  * * @param delay when should this operation time out? Use
409  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
410  * @param wfd write file-descriptor
411  * @param task main function of the task
412  * @param task_cls closure of task
413  * @return unique task identifier for the job
414  *         only valid until "task" is started!
415  */
416 GNUNET_SCHEDULER_TaskIdentifier
417 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
418                                 struct GNUNET_NETWORK_Handle *wfd,
419                                 GNUNET_SCHEDULER_Task task, void *task_cls);
420
421
422 /**
423  * Schedule a new task to be run with a specified delay or when the
424  * specified file descriptor is ready for reading.  The delay can be
425  * used as a timeout on the socket being ready.  The task will be
426  * scheduled for execution once either the delay has expired or the
427  * socket operation is ready. It will be run with the priority of
428  * the calling task.
429  *
430  * * @param delay when should this operation time out? Use
431  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
432  * @param rfd read file-descriptor
433  * @param task main function of the task
434  * @param task_cls closure of task
435  * @return unique task identifier for the job
436  *         only valid until "task" is started!
437  */
438 GNUNET_SCHEDULER_TaskIdentifier
439 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
440                                 const struct GNUNET_DISK_FileHandle *rfd,
441                                 GNUNET_SCHEDULER_Task task, void *task_cls);
442
443
444 /**
445  * Schedule a new task to be run with a specified delay or when the
446  * specified file descriptor is ready for writing.  The delay can be
447  * used as a timeout on the socket being ready.  The task will be
448  * scheduled for execution once either the delay has expired or the
449  * socket operation is ready. It will be run with the priority of
450  * the calling task.
451  *
452  * * @param delay when should this operation time out? Use
453  *        GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
454  * @param wfd write file-descriptor
455  * @param task main function of the task
456  * @param task_cls closure of task
457  * @return unique task identifier for the job
458  *         only valid until "task" is started!
459  */
460 GNUNET_SCHEDULER_TaskIdentifier
461 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
462                                  const struct GNUNET_DISK_FileHandle *wfd,
463                                  GNUNET_SCHEDULER_Task task, void *task_cls);
464
465
466 /**
467  * Schedule a new task to be run with a specified delay or when any of
468  * the specified file descriptor sets is ready.  The delay can be used
469  * as a timeout on the socket(s) being ready.  The task will be
470  * scheduled for execution once either the delay has expired or any of
471  * the socket operations is ready.  This is the most general
472  * function of the "add" family.  Note that the "prerequisite_task"
473  * must be satisfied in addition to any of the other conditions.  In
474  * other words, the task will be started when
475  * <code>
476  * (prerequisite-run)
477  * && (delay-ready
478  *     || any-rs-ready
479  *     || any-ws-ready
480  *     || shutdown-active)
481  * </code>
482  *
483  * * @param prio how important is this task?
484  * @param prerequisite_task run this task after the task with the given
485  *        task identifier completes (and any of our other
486  *        conditions, such as delay, read or write-readiness
487  *        are satisfied).  Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
488  *        on completion of other tasks.
489  * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
490  *        which means that the task will only be run after we receive SIGTERM
491  * @param rs set of file descriptors we want to read (can be NULL)
492  * @param ws set of file descriptors we want to write (can be NULL)
493  * @param task main function of the task
494  * @param task_cls closure of task
495  * @return unique task identifier for the job
496  *         only valid until "task" is started!
497  */
498 GNUNET_SCHEDULER_TaskIdentifier
499 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
500                              GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
501                              struct GNUNET_TIME_Relative delay,
502                              const struct GNUNET_NETWORK_FDSet *rs,
503                              const struct GNUNET_NETWORK_FDSet *ws,
504                              GNUNET_SCHEDULER_Task task, void *task_cls);
505
506 /**
507  * Sets the select function to use in the scheduler (scheduler_select).
508  *
509  * @param new_select new select function to use (NULL to reset to default)
510  * @param new_select_cls closure for 'new_select'
511  */
512 void
513 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
514                              void *new_select_cls);
515
516
517 #if 0                           /* keep Emacsens' auto-indent happy */
518 {
519 #endif
520 #ifdef __cplusplus
521 }
522 #endif
523
524 #endif