2 This file is part of GNUnet
3 Copyright (C) 2009-2013 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 3, 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
25 * @defgroup scheduler Event loop (scheduler)
29 #ifndef GNUNET_SCHEDULER_LIB_H
30 #define GNUNET_SCHEDULER_LIB_H
35 #if 0 /* keep Emacsens' auto-indent happy */
41 * Opaque reference to a task.
43 struct GNUNET_SCHEDULER_Task;
46 * Reasons why the schedule may have triggered
49 enum GNUNET_SCHEDULER_Reason
52 * This task is not ready.
54 GNUNET_SCHEDULER_REASON_NONE = 0,
57 * This is the very first task run during startup.
59 GNUNET_SCHEDULER_REASON_STARTUP = 1,
62 * We are shutting down and are running all shutdown-related tasks
63 * (regardless of timeout, etc.).
65 GNUNET_SCHEDULER_REASON_SHUTDOWN = 2,
68 * The specified timeout has expired.
69 * (also set if the delay given was 0).
71 GNUNET_SCHEDULER_REASON_TIMEOUT = 4,
74 * The reading socket is ready.
76 GNUNET_SCHEDULER_REASON_READ_READY = 8,
79 * The writing socket is ready.
81 GNUNET_SCHEDULER_REASON_WRITE_READY = 16,
84 * The prerequisite task is done.
86 GNUNET_SCHEDULER_REASON_PREREQ_DONE = 32
90 #include "gnunet_time_lib.h"
91 #include "gnunet_network_lib.h"
95 * Context information passed to each scheduler task.
97 struct GNUNET_SCHEDULER_TaskContext
100 * Reason why the task is run now
102 enum GNUNET_SCHEDULER_Reason reason;
105 * Set of file descriptors ready for reading;
106 * note that additional bits may be set
107 * that were not in the original request
109 const struct GNUNET_NETWORK_FDSet *read_ready;
112 * Set of file descriptors ready for writing;
113 * note that additional bits may be set
114 * that were not in the original request.
116 const struct GNUNET_NETWORK_FDSet *write_ready;
122 * Signature of the main function of a task.
125 * @param tc context information (why was this task triggered now)
128 (*GNUNET_SCHEDULER_TaskCallback) (void *cls,
129 const struct GNUNET_SCHEDULER_TaskContext *tc);
133 * Signature of the select function used by the scheduler.
134 * #GNUNET_NETWORK_socket_select matches it.
137 * @param rfds set of sockets to be checked for readability
138 * @param wfds set of sockets to be checked for writability
139 * @param efds set of sockets to be checked for exceptions
140 * @param timeout relative value when to return
141 * @return number of selected sockets, #GNUNET_SYSERR on error
144 (*GNUNET_SCHEDULER_select) (void *cls,
145 struct GNUNET_NETWORK_FDSet *rfds,
146 struct GNUNET_NETWORK_FDSet *wfds,
147 struct GNUNET_NETWORK_FDSet *efds,
148 struct GNUNET_TIME_Relative timeout);
152 * Initialize and run scheduler. This function will return when all
153 * tasks have completed. On systems with signals, receiving a SIGTERM
154 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
155 * to be run after the active task is complete. As a result, SIGTERM
156 * causes all active tasks to be scheduled with reason
157 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
158 * afterwards will execute normally!). Note that any particular
159 * signal will only shut down one scheduler; applications should
160 * always only create a single scheduler.
162 * @param task task to run first (and immediately)
163 * @param task_cls closure of @a task
166 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
171 * Request the shutdown of the scheduler. Marks all currently
172 * pending tasks as ready because of shutdown. This will
173 * cause all tasks to run (as soon as possible, respecting
174 * priorities and prerequisite tasks). Note that tasks
175 * scheduled AFTER this call may still be delayed arbitrarily.
178 GNUNET_SCHEDULER_shutdown (void);
182 * Get information about the current load of this scheduler. Use this
183 * function to determine if an elective task should be added or simply
184 * dropped (if the decision should be made based on the number of
185 * tasks ready to run).
187 * @param p priority-level to query, use KEEP to query the level
188 * of the current task, use COUNT to get the sum over
189 * all priority levels
190 * @return number of tasks pending right now
193 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p);
197 * Obtain the reason code for why the current task was
198 * started. Will return the same value as
199 * the GNUNET_SCHEDULER_TaskContext's reason field.
201 * @return reason(s) why the current task is run
203 enum GNUNET_SCHEDULER_Reason
204 GNUNET_SCHEDULER_get_reason (void);
208 * Cancel the task with the specified identifier.
209 * The task must not yet have run.
211 * @param task id of the task to cancel
212 * @return the closure of the callback of the cancelled task
215 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task);
219 * Continue the current execution with the given function. This is
220 * similar to the other "add" functions except that there is no delay
221 * and the reason code can be specified.
223 * @param task main function of the task
224 * @param task_cls closure of task
225 * @param reason reason for task invocation
228 GNUNET_SCHEDULER_add_continuation (GNUNET_SCHEDULER_TaskCallback task,
230 enum GNUNET_SCHEDULER_Reason reason);
234 * Continue the current execution with the given function. This is
235 * similar to the other "add" functions except that there is no delay
236 * and the reason code can be specified.
238 * @param task main function of the task
239 * @param task_cls closure for @a task
240 * @param reason reason for task invocation
241 * @param priority priority to use for the task
244 GNUNET_SCHEDULER_add_continuation_with_priority (GNUNET_SCHEDULER_TaskCallback task,
246 enum GNUNET_SCHEDULER_Reason reason,
247 enum GNUNET_SCHEDULER_Priority priority);
251 * Schedule a new task to be run with a specified priority.
253 * @param prio how important is the new task?
254 * @param task main function of the task
255 * @param task_cls closure of @a task
256 * @return unique task identifier for the job
257 * only valid until @a task is started!
259 struct GNUNET_SCHEDULER_Task *
260 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
261 GNUNET_SCHEDULER_TaskCallback task,
266 * Schedule a new task to be run as soon as possible. Note that this
267 * does not guarantee that this will be the next task that is being
268 * run, as other tasks with higher priority (or that are already ready
269 * to run) might get to run first. Just as with delays, clients must
270 * not rely on any particular order of execution between tasks
271 * scheduled concurrently.
273 * The task will be run with the DEFAULT priority.
275 * @param task main function of the task
276 * @param task_cls closure of @a task
277 * @return unique task identifier for the job
278 * only valid until @a task is started!
280 struct GNUNET_SCHEDULER_Task *
281 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
286 * Schedule a new task to be run as soon as possible with the
287 * (transitive) ignore-shutdown flag either explicitly set or
288 * explicitly enabled. This task (and all tasks created from it,
289 * other than by another call to this function) will either count or
290 * not count for the 'lifeness' of the process. This API is only
291 * useful in a few special cases.
293 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
294 * @param task main function of the task
295 * @param task_cls closure of @a task
296 * @return unique task identifier for the job
297 * only valid until @a task is started!
299 struct GNUNET_SCHEDULER_Task *
300 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
301 GNUNET_SCHEDULER_TaskCallback task,
306 * Schedule a new task to be run with a specified delay. The task
307 * will be scheduled for execution once the delay has expired. It
308 * will be run with the DEFAULT priority.
310 * * @param delay when should this operation time out? Use
311 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
312 * @param task main function of the task
313 * @param task_cls closure of @a task
314 * @return unique task identifier for the job
315 * only valid until @a task is started!
317 struct GNUNET_SCHEDULER_Task *
318 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
319 GNUNET_SCHEDULER_TaskCallback task,
324 * Schedule a new task to be run with a specified delay. The task
325 * will be scheduled for execution once the delay has expired.
327 * @param delay when should this operation time out? Use
328 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
329 * @param priority priority to use for the task
330 * @param task main function of the task
331 * @param task_cls closure of @a task
332 * @return unique task identifier for the job
333 * only valid until @a task is started!
335 struct GNUNET_SCHEDULER_Task *
336 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
337 enum GNUNET_SCHEDULER_Priority priority,
338 GNUNET_SCHEDULER_TaskCallback task,
343 * Schedule a new task to be run with a specified delay or when the
344 * specified file descriptor is ready for reading. The delay can be
345 * used as a timeout on the socket being ready. The task will be
346 * scheduled for execution once either the delay has expired or the
347 * socket operation is ready. It will be run with the DEFAULT priority.
349 * * @param delay when should this operation time out? Use
350 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
351 * @param rfd read file-descriptor
352 * @param task main function of the task
353 * @param task_cls closure of @a task
354 * @return unique task identifier for the job
355 * only valid until @a task is started!
357 struct GNUNET_SCHEDULER_Task *
358 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
359 struct GNUNET_NETWORK_Handle *rfd,
360 GNUNET_SCHEDULER_TaskCallback task,
365 * Schedule a new task to be run with a specified priority and to be
366 * run after the specified delay or when the specified file descriptor
367 * is ready for reading. The delay can be used as a timeout on the
368 * socket being ready. The task will be scheduled for execution once
369 * either the delay has expired or the socket operation is ready. It
370 * will be run with the DEFAULT priority.
372 * @param delay when should this operation time out? Use
373 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
374 * @param priority priority to use for the task
375 * @param rfd read file-descriptor
376 * @param task main function of the task
377 * @param task_cls closure of @a task
378 * @return unique task identifier for the job
379 * only valid until @a task is started!
381 struct GNUNET_SCHEDULER_Task *
382 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
383 enum GNUNET_SCHEDULER_Priority priority,
384 struct GNUNET_NETWORK_Handle *rfd,
385 GNUNET_SCHEDULER_TaskCallback task,
390 * Schedule a new task to be run with a specified delay or when the
391 * specified file descriptor is ready for writing. 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 DEFAULT priority.
396 * * @param delay when should this operation time out? Use
397 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
398 * @param wfd write file-descriptor
399 * @param task main function of the task
400 * @param task_cls closure of @a task
401 * @return unique task identifier for the job
402 * only valid until @a task is started!
404 struct GNUNET_SCHEDULER_Task *
405 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
406 struct GNUNET_NETWORK_Handle *wfd,
407 GNUNET_SCHEDULER_TaskCallback task,
412 * Schedule a new task to be run with a specified delay or when the
413 * specified file descriptor is ready. The delay can be
414 * used as a timeout on the socket being ready. The task will be
415 * scheduled for execution once either the delay has expired or the
416 * socket operation is ready.
418 * @param delay when should this operation time out? Use
419 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
420 * @param priority priority of the task
421 * @param fd file-descriptor
422 * @param on_read whether to poll the file-descriptor for readability
423 * @param on_write whether to poll the file-descriptor for writability
424 * @param task main function of the task
425 * @param task_cls closure of @a task
426 * @return unique task identifier for the job
427 * only valid until "task" is started!
429 struct GNUNET_SCHEDULER_Task *
430 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
431 enum GNUNET_SCHEDULER_Priority priority,
432 struct GNUNET_NETWORK_Handle *fd,
435 GNUNET_SCHEDULER_TaskCallback task,
439 * Schedule a new task to be run with a specified delay or when the
440 * specified file descriptor is ready for reading. The delay can be
441 * used as a timeout on the socket being ready. The task will be
442 * scheduled for execution once either the delay has expired or the
443 * socket operation is ready. It will be run with the DEFAULT priority.
445 * * @param delay when should this operation time out? Use
446 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
447 * @param rfd read file-descriptor
448 * @param task main function of the task
449 * @param task_cls closure of @a task
450 * @return unique task identifier for the job
451 * only valid until "task" is started!
453 struct GNUNET_SCHEDULER_Task *
454 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
455 const struct GNUNET_DISK_FileHandle *rfd,
456 GNUNET_SCHEDULER_TaskCallback task,
461 * Schedule a new task to be run with a specified delay or when the
462 * specified file descriptor is ready for writing. The delay can be
463 * used as a timeout on the socket being ready. The task will be
464 * scheduled for execution once either the delay has expired or the
465 * socket operation is ready. It will be run with the DEFAULT priority.
467 * * @param delay when should this operation time out? Use
468 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
469 * @param wfd write file-descriptor
470 * @param task main function of the task
471 * @param task_cls closure of @a task
472 * @return unique task identifier for the job
473 * only valid until @a task is started!
475 struct GNUNET_SCHEDULER_Task *
476 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
477 const struct GNUNET_DISK_FileHandle *wfd,
478 GNUNET_SCHEDULER_TaskCallback task,
483 * Schedule a new task to be run with a specified delay or when the
484 * specified file descriptor is ready. The delay can be
485 * used as a timeout on the socket being ready. The task will be
486 * scheduled for execution once either the delay has expired or the
487 * socket operation is ready.
489 * @param delay when should this operation time out? Use
490 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
491 * @param priority priority of the task
492 * @param fd file-descriptor
493 * @param on_read whether to poll the file-descriptor for readability
494 * @param on_write whether to poll the file-descriptor for writability
495 * @param task main function of the task
496 * @param task_cls closure of @a task
497 * @return unique task identifier for the job
498 * only valid until @a task is started!
500 struct GNUNET_SCHEDULER_Task *
501 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
502 enum GNUNET_SCHEDULER_Priority priority,
503 const struct GNUNET_DISK_FileHandle *fd,
504 int on_read, int on_write,
505 GNUNET_SCHEDULER_TaskCallback task,
510 * Schedule a new task to be run with a specified delay or when any of
511 * the specified file descriptor sets is ready. The delay can be used
512 * as a timeout on the socket(s) being ready. The task will be
513 * scheduled for execution once either the delay has expired or any of
514 * the socket operations is ready. This is the most general
515 * function of the "add" family. Note that the "prerequisite_task"
516 * must be satisfied in addition to any of the other conditions. In
517 * other words, the task will be started when
523 * || shutdown-active)
526 * @param prio how important is this task?
527 * @param delay how long should we wait? Use #GNUNET_TIME_UNIT_FOREVER_REL for "forever",
528 * which means that the task will only be run after we receive SIGTERM
529 * @param rs set of file descriptors we want to read (can be NULL)
530 * @param ws set of file descriptors we want to write (can be NULL)
531 * @param task main function of the task
532 * @param task_cls closure of @a task
533 * @return unique task identifier for the job
534 * only valid until "task" is started!
536 struct GNUNET_SCHEDULER_Task *
537 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
538 struct GNUNET_TIME_Relative delay,
539 const struct GNUNET_NETWORK_FDSet *rs,
540 const struct GNUNET_NETWORK_FDSet *ws,
541 GNUNET_SCHEDULER_TaskCallback task,
545 * Sets the select function to use in the scheduler (scheduler_select).
547 * @param new_select new select function to use (NULL to reset to default)
548 * @param new_select_cls closure for 'new_select'
551 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
552 void *new_select_cls);
555 /** @} */ /* end of group scheduler */
557 #if 0 /* keep Emacsens' auto-indent happy */