2 This file is part of GNUnet
3 (C) 2009, 2011 Christian Grothoff (and other contributing authors)
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.
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.
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.
22 * @file include/gnunet_scheduler_lib.h
23 * @brief API to schedule computations using continuation passing style
24 * @author Christian Grothoff
27 #ifndef GNUNET_SCHEDULER_LIB_H
28 #define GNUNET_SCHEDULER_LIB_H
33 #if 0 /* keep Emacsens' auto-indent happy */
39 * Opaque reference to a task.
41 typedef unsigned long long GNUNET_SCHEDULER_TaskIdentifier;
45 * Constant used to indicate that the scheduled
46 * task has no others as prerequisites.
48 #define GNUNET_SCHEDULER_NO_TASK ((GNUNET_SCHEDULER_TaskIdentifier) 0)
51 * Reasons why the schedule may have triggered
54 enum GNUNET_SCHEDULER_Reason
57 * This is the very first task run during startup.
59 GNUNET_SCHEDULER_REASON_STARTUP = 0,
62 * We are shutting down and are running all shutdown-related tasks
63 * (regardless of timeout, etc.).
65 GNUNET_SCHEDULER_REASON_SHUTDOWN = 1,
68 * The specified timeout has expired.
69 * (also set if the delay given was 0).
71 GNUNET_SCHEDULER_REASON_TIMEOUT = 2,
74 * The reading socket is ready.
76 GNUNET_SCHEDULER_REASON_READ_READY = 4,
79 * The writing socket is ready.
81 GNUNET_SCHEDULER_REASON_WRITE_READY = 8,
84 * The prerequisite task is done.
86 GNUNET_SCHEDULER_REASON_PREREQ_DONE = 16
91 * Valid task priorities. Use these, do not
92 * pass random integers!
94 enum GNUNET_SCHEDULER_Priority
97 * Run with the same priority as the current job.
99 GNUNET_SCHEDULER_PRIORITY_KEEP = 0,
102 * Run when otherwise idle.
104 GNUNET_SCHEDULER_PRIORITY_IDLE = 1,
107 * Run as background job (higher than idle,
108 * lower than default).
110 GNUNET_SCHEDULER_PRIORITY_BACKGROUND = 2,
113 * Run with the default priority (normal
114 * P2P operations). Higher than BACKGROUND.
116 GNUNET_SCHEDULER_PRIORITY_DEFAULT = 3,
119 * Run with high priority (important requests).
120 * Higher than DEFAULT.
122 GNUNET_SCHEDULER_PRIORITY_HIGH = 4,
125 * Run with priority for interactive tasks.
126 * Higher than "HIGH".
128 GNUNET_SCHEDULER_PRIORITY_UI = 5,
131 * Run with priority for urgent tasks. Use
132 * for things like aborts and shutdowns that
133 * need to preempt "UI"-level tasks.
136 GNUNET_SCHEDULER_PRIORITY_URGENT = 6,
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).
146 GNUNET_SCHEDULER_PRIORITY_SHUTDOWN = 7,
149 * Number of priorities (must be the last priority).
150 * This priority must not be used by clients.
152 GNUNET_SCHEDULER_PRIORITY_COUNT = 8
155 #include "gnunet_time_lib.h"
156 #include "gnunet_network_lib.h"
160 * Context information passed to each scheduler task.
162 struct GNUNET_SCHEDULER_TaskContext
165 * Reason why the task is run now
167 enum GNUNET_SCHEDULER_Reason reason;
170 * Set of file descriptors ready for reading;
171 * note that additional bits may be set
172 * that were not in the original request
174 const struct GNUNET_NETWORK_FDSet *read_ready;
177 * Set of file descriptors ready for writing;
178 * note that additional bits may be set
179 * that were not in the original request.
181 const struct GNUNET_NETWORK_FDSet *write_ready;
187 * Signature of the main function of a task.
190 * @param tc context information (why was this task triggered now)
192 typedef void (*GNUNET_SCHEDULER_Task) (void *cls,
194 GNUNET_SCHEDULER_TaskContext * tc);
198 * Signature of the select function used by the scheduler.
199 * GNUNET_NETWORK_socket_select matches it.
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
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);
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.
224 * @param task task to run first (and immediately)
225 * @param task_cls closure of task
227 void GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_Task task, void *task_cls);
231 * Request the shutdown of a scheduler. Marks all currently
232 * pending tasks as ready because of shutdown. This will
233 * cause all tasks to run (as soon as possible, respecting
234 * priorities and prerequisite tasks). Note that tasks
235 * scheduled AFTER this call may still be delayed arbitrarily.
237 void GNUNET_SCHEDULER_shutdown ();
241 * Get information about the current load of this scheduler. Use this
242 * function to determine if an elective task should be added or simply
243 * dropped (if the decision should be made based on the number of
244 * tasks ready to run).
246 * * @param p priority-level to query, use KEEP to query the level
247 * of the current task, use COUNT to get the sum over
248 * all priority levels
249 * @return number of tasks pending right now
251 unsigned int GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p);
255 * Obtain the reason code for why the current task was
256 * started. Will return the same value as
257 * the GNUNET_SCHEDULER_TaskContext's reason field.
259 * * @return reason(s) why the current task is run
261 enum GNUNET_SCHEDULER_Reason
262 GNUNET_SCHEDULER_get_reason ();
266 * Cancel the task with the specified identifier.
267 * The task must not yet have run.
269 * * @param task id of the task to cancel
270 * @return the closure of the callback of the cancelled task
272 void *GNUNET_SCHEDULER_cancel (GNUNET_SCHEDULER_TaskIdentifier task);
276 * Continue the current execution with the given function. This is
277 * similar to the other "add" functions except that there is no delay
278 * and the reason code can be specified.
280 * * @param task main function of the task
281 * @param task_cls closure of task
282 * @param reason reason for task invocation
285 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_Task task,
287 enum GNUNET_SCHEDULER_Reason reason);
291 * Schedule a new task to be run after the specified prerequisite task
292 * has completed. It will be run with the priority of the calling
295 * * @param prerequisite_task run this task after the task with the given
296 * task identifier completes (and any of our other
297 * conditions, such as delay, read or write-readiness
298 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
299 * on completion of other tasks (this will cause the task to run as
301 * @param task main function of the task
302 * @param task_cls closure of task
303 * @return unique task identifier for the job
304 * only valid until "task" is started!
306 GNUNET_SCHEDULER_TaskIdentifier
307 GNUNET_SCHEDULER_add_after (GNUNET_SCHEDULER_TaskIdentifier prerequisite_task,
308 GNUNET_SCHEDULER_Task task,
313 * Schedule a new task to be run with a specified priority.
315 * * @param prio how important is the new task?
316 * @param task main function of the task
317 * @param task_cls closure of task
318 * @return unique task identifier for the job
319 * only valid until "task" is started!
321 GNUNET_SCHEDULER_TaskIdentifier
322 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
323 GNUNET_SCHEDULER_Task task,
328 * Schedule a new task to be run as soon as possible. The task
329 * will be run with the priority of the calling task.
331 * * @param task main function of the task
332 * @param task_cls closure of task
333 * @return unique task identifier for the job
334 * only valid until "task" is started!
336 GNUNET_SCHEDULER_TaskIdentifier
337 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_Task task,
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.
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!
355 GNUNET_SCHEDULER_TaskIdentifier
356 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
357 GNUNET_SCHEDULER_Task task,
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.
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!
373 GNUNET_SCHEDULER_TaskIdentifier
374 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
375 GNUNET_SCHEDULER_Task task,
380 * Schedule a new task to be run with a specified delay or when the
381 * specified file descriptor is ready for reading. The delay can be
382 * used as a timeout on the socket being ready. The task will be
383 * scheduled for execution once either the delay has expired or the
384 * socket operation is ready. It will be run with the priority of
387 * * @param delay when should this operation time out? Use
388 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
389 * @param rfd read file-descriptor
390 * @param task main function of the task
391 * @param task_cls closure of task
392 * @return unique task identifier for the job
393 * only valid until "task" is started!
395 GNUNET_SCHEDULER_TaskIdentifier
396 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
397 struct GNUNET_NETWORK_Handle *rfd,
398 GNUNET_SCHEDULER_Task task,
403 * Schedule a new task to be run with a specified delay or when the
404 * specified file descriptor is ready for writing. The delay can be
405 * used as a timeout on the socket being ready. The task will be
406 * scheduled for execution once either the delay has expired or the
407 * socket operation is ready. It will be run with the priority of
410 * * @param delay when should this operation time out? Use
411 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
412 * @param wfd write file-descriptor
413 * @param task main function of the task
414 * @param task_cls closure of task
415 * @return unique task identifier for the job
416 * only valid until "task" is started!
418 GNUNET_SCHEDULER_TaskIdentifier
419 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
420 struct GNUNET_NETWORK_Handle *wfd,
421 GNUNET_SCHEDULER_Task task,
426 * Schedule a new task to be run with a specified delay or when the
427 * specified file descriptor is ready for reading. The delay can be
428 * used as a timeout on the socket being ready. The task will be
429 * scheduled for execution once either the delay has expired or the
430 * socket operation is ready. It will be run with the priority of
433 * * @param delay when should this operation time out? Use
434 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
435 * @param rfd read file-descriptor
436 * @param task main function of the task
437 * @param task_cls closure of task
438 * @return unique task identifier for the job
439 * only valid until "task" is started!
441 GNUNET_SCHEDULER_TaskIdentifier
442 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
443 const struct GNUNET_DISK_FileHandle *rfd,
444 GNUNET_SCHEDULER_Task task,
449 * Schedule a new task to be run with a specified delay or when the
450 * specified file descriptor is ready for writing. The delay can be
451 * used as a timeout on the socket being ready. The task will be
452 * scheduled for execution once either the delay has expired or the
453 * socket operation is ready. It will be run with the priority of
456 * * @param delay when should this operation time out? Use
457 * GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
458 * @param wfd write file-descriptor
459 * @param task main function of the task
460 * @param task_cls closure of task
461 * @return unique task identifier for the job
462 * only valid until "task" is started!
464 GNUNET_SCHEDULER_TaskIdentifier
465 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
466 const struct GNUNET_DISK_FileHandle *wfd,
467 GNUNET_SCHEDULER_Task task,
472 * Schedule a new task to be run with a specified delay or when any of
473 * the specified file descriptor sets is ready. The delay can be used
474 * as a timeout on the socket(s) being ready. The task will be
475 * scheduled for execution once either the delay has expired or any of
476 * the socket operations is ready. This is the most general
477 * function of the "add" family. Note that the "prerequisite_task"
478 * must be satisfied in addition to any of the other conditions. In
479 * other words, the task will be started when
485 * || shutdown-active)
488 * * @param prio how important is this task?
489 * @param prerequisite_task run this task after the task with the given
490 * task identifier completes (and any of our other
491 * conditions, such as delay, read or write-readiness
492 * are satisfied). Use GNUNET_SCHEDULER_NO_TASK to not have any dependency
493 * on completion of other tasks.
494 * @param delay how long should we wait? Use GNUNET_TIME_UNIT_FOREVER_REL for "forever",
495 * which means that the task will only be run after we receive SIGTERM
496 * @param rs set of file descriptors we want to read (can be NULL)
497 * @param ws set of file descriptors we want to write (can be NULL)
498 * @param task main function of the task
499 * @param task_cls closure of task
500 * @return unique task identifier for the job
501 * only valid until "task" is started!
503 GNUNET_SCHEDULER_TaskIdentifier
504 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
505 GNUNET_SCHEDULER_TaskIdentifier
507 struct GNUNET_TIME_Relative delay,
508 const struct GNUNET_NETWORK_FDSet * rs,
509 const struct GNUNET_NETWORK_FDSet * ws,
510 GNUNET_SCHEDULER_Task task,
514 * Sets the select function to use in the scheduler (scheduler_select).
516 * @param new_select new select function to use (NULL to reset to default)
517 * @param new_select_cls closure for 'new_select'
520 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
521 void *new_select_cls);
524 #if 0 /* keep Emacsens' auto-indent happy */