2 This file is part of GNUnet
3 Copyright (C) 2009-2016 GNUnet e.V.
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., 51 Franklin Street, Fifth Floor,
18 Boston, MA 02110-1301, USA.
22 * @author Christian Grothoff
25 * API to schedule computations using continuation passing style
27 * @defgroup scheduler Scheduler library
28 * Event loop (scheduler)
30 * Schedule computations using continuation passing style.
35 #ifndef GNUNET_SCHEDULER_LIB_H
36 #define GNUNET_SCHEDULER_LIB_H
41 #if 0 /* keep Emacsens' auto-indent happy */
47 * Opaque reference to a task.
49 struct GNUNET_SCHEDULER_Task;
52 * Reasons why the schedule may have triggered
55 enum GNUNET_SCHEDULER_Reason
58 * This task is not ready.
60 GNUNET_SCHEDULER_REASON_NONE = 0,
63 * This is the very first task run during startup.
65 GNUNET_SCHEDULER_REASON_STARTUP = 1,
68 * We are shutting down and are running all shutdown-related tasks.
70 GNUNET_SCHEDULER_REASON_SHUTDOWN = 2,
73 * The specified timeout has expired.
74 * (also set if the delay given was 0).
76 GNUNET_SCHEDULER_REASON_TIMEOUT = 4,
79 * The reading socket is ready.
81 GNUNET_SCHEDULER_REASON_READ_READY = 8,
84 * The writing socket is ready.
86 GNUNET_SCHEDULER_REASON_WRITE_READY = 16,
89 * The prerequisite task is done.
91 GNUNET_SCHEDULER_REASON_PREREQ_DONE = 32
95 #include "gnunet_time_lib.h"
96 #include "gnunet_network_lib.h"
100 * Possible events on FDs, used as a bitmask.
101 * Modelled after GPollFD.
103 enum GNUNET_SCHEDULER_EventType
107 * No event (useful for timeout).
109 GNUNET_SCHEDULER_ET_NONE = 0,
112 * Data available for reading.
114 GNUNET_SCHEDULER_ET_IN = 1,
117 * Buffer available for writing.
119 GNUNET_SCHEDULER_ET_OUT = 2,
124 GNUNET_SCHEDULER_ET_HUP = 4,
129 GNUNET_SCHEDULER_ET_ERR = 8,
134 GNUNET_SCHEDULER_ET_PRI = 16,
139 GNUNET_SCHEDULER_ET_NVAL = 32
145 * Information about an event relating to a file descriptor/socket.
147 struct GNUNET_SCHEDULER_FdInfo
151 * GNUnet network socket the event is about, matches @a sock,
152 * NULL if this is about a file handle or if no network
153 * handle was given to the scheduler originally.
155 struct GNUNET_NETWORK_Handle *fd;
158 * GNUnet file handle the event is about, matches @a sock,
159 * NULL if this is about a network socket or if no network
160 * handle was given to the scheduler originally.
162 struct GNUNET_DISK_FileHandle *fh;
165 * Type of the event that was generated related to @e sock.
167 enum GNUNET_SCHEDULER_EventType et;
170 * Underlying OS handle the event was about.
178 * Context information passed to each scheduler task.
180 struct GNUNET_SCHEDULER_TaskContext
183 * Reason why the task is run now
185 enum GNUNET_SCHEDULER_Reason reason;
188 * Length of the following array.
190 unsigned int fds_len;
193 * Array of length @e fds_len with information about ready FDs.
194 * Note that we use the same format regardless of the internal
195 * event loop that was used. The given array should only contain
196 * information about file descriptors relevant to the current task.
198 const struct GNUNET_SCHEDULER_FdInfo *fds;
201 * Set of file descriptors ready for reading; note that additional
202 * bits may be set that were not in the original request.
205 const struct GNUNET_NETWORK_FDSet *read_ready;
208 * Set of file descriptors ready for writing; note that additional
209 * bits may be set that were not in the original request.
212 const struct GNUNET_NETWORK_FDSet *write_ready;
218 * Function used by event-loop implementations to signal the scheduler
219 * that a particular @a task is ready due to an event of type @a et.
221 * This function will then queue the task to notify the application
222 * that the task is ready (with the respective priority).
224 * @param task the task that is ready
225 * @param et information about why the task is ready
228 GNUNET_SCHEDULER_task_ready (struct GNUNET_SCHEDULER_Task *task,
229 enum GNUNET_SCHEDULER_EventType et);
233 * Handle to the scheduler's state to be used by the driver.
235 struct GNUNET_SCHEDULER_Handle;
239 * Function called by the driver to tell the scheduler to run some of
240 * the tasks that are ready. This function may return even though
241 * there are tasks left to run just to give other tasks a chance as
242 * well. If we return #GNUNET_YES, the driver should call this
243 * function again as soon as possible, while if we return #GNUNET_NO
244 * it must block until the operating system has more work as the
245 * scheduler has no more work to do right now.
247 * @param sh scheduler handle that was given to the `loop`
248 * @return #GNUNET_OK if there are more tasks that are ready,
249 * and thus we would like to run more (yield to avoid
250 * blocking other activities for too long)
251 * #GNUNET_NO if we are done running tasks (yield to block)
252 * #GNUNET_SYSERR on error
255 GNUNET_SCHEDULER_run_from_driver (struct GNUNET_SCHEDULER_Handle *sh);
259 * API a driver has to implement for
260 * #GNUNET_SCHEDULER_run_with_driver().
262 struct GNUNET_SCHEDULER_Driver
266 * Closure to pass to the functions in this struct.
271 * Add a @a task to be run if the conditions given
272 * in @a fdi are satisfied.
275 * @param task task to add
276 * @param fdi conditions to watch for
277 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
278 * (i.e. @a fdi too high or invalid)
282 struct GNUNET_SCHEDULER_Task *task,
283 struct GNUNET_SCHEDULER_FdInfo *fdi);
286 * Delete a @a task from the set of tasks to be run.
289 * @param task task to delete
290 * @param fdi conditions to watch for (must match @e add call)
291 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
292 * (i.e. @a task or @a fdi do not match prior @e add call)
296 struct GNUNET_SCHEDULER_Task *task,
297 const struct GNUNET_SCHEDULER_FdInfo *fdi);
300 * Set time at which we definitively want to get a wakeup call.
303 * @param dt time when we want to wake up next
306 (*set_wakeup)(void *cls,
307 struct GNUNET_TIME_Absolute dt);
310 * Event loop's "main" function, to be called from
311 * #GNUNET_SCHEDULER_run_with_driver() to actually
315 * @param sh scheduler handle to pass to
316 * #GNUNET_SCHEDULER_run_from_driver()
317 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
321 struct GNUNET_SCHEDULER_Handle *sh);
327 * Signature of the main function of a task.
332 (*GNUNET_SCHEDULER_TaskCallback) (void *cls);
336 * Initialize and run scheduler. This function will return when all
337 * tasks have completed. On systems with signals, receiving a SIGTERM
338 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
339 * to be run after the active task is complete. As a result, SIGTERM
340 * causes all shutdown tasks to be scheduled with reason
341 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
342 * afterwards will execute normally!). Note that any particular
343 * signal will only shut down one scheduler; applications should
344 * always only create a single scheduler.
346 * @param driver drive to use for the event loop
347 * @param task task to run first (and immediately)
348 * @param task_cls closure of @a task
349 * @return #GNUNET_OK on success, #GNUNET_SYSERR on failure
352 GNUNET_SCHEDULER_run_with_driver (const struct GNUNET_SCHEDULER_Driver *driver,
353 GNUNET_SCHEDULER_TaskCallback task,
358 * Obtain the driver for using select() as the event loop.
360 * @return NULL on error
362 const struct GNUNET_SCHEDULER_Driver *
363 GNUNET_SCHEDULER_driver_select (void);
367 * Signature of the select function used by the scheduler.
368 * #GNUNET_NETWORK_socket_select matches it.
371 * @param rfds set of sockets to be checked for readability
372 * @param wfds set of sockets to be checked for writability
373 * @param efds set of sockets to be checked for exceptions
374 * @param timeout relative value when to return
375 * @return number of selected sockets, #GNUNET_SYSERR on error
378 (*GNUNET_SCHEDULER_select) (void *cls,
379 struct GNUNET_NETWORK_FDSet *rfds,
380 struct GNUNET_NETWORK_FDSet *wfds,
381 struct GNUNET_NETWORK_FDSet *efds,
382 struct GNUNET_TIME_Relative timeout);
386 * Initialize and run scheduler. This function will return when all
387 * tasks have completed. On systems with signals, receiving a SIGTERM
388 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
389 * to be run after the active task is complete. As a result, SIGTERM
390 * causes all shutdown tasks to be scheduled with reason
391 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
392 * afterwards will execute normally!). Note that any particular
393 * signal will only shut down one scheduler; applications should
394 * always only create a single scheduler.
396 * @param task task to run first (and immediately)
397 * @param task_cls closure of @a task
400 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
405 * Request the shutdown of a scheduler. Marks all tasks
406 * awaiting shutdown as ready. Note that tasks
407 * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call
408 * will be delayed until the next shutdown signal.
411 GNUNET_SCHEDULER_shutdown (void);
415 * Get information about the current load of this scheduler. Use this
416 * function to determine if an elective task should be added or simply
417 * dropped (if the decision should be made based on the number of
418 * tasks ready to run).
420 * @param p priority-level to query, use KEEP to query the level
421 * of the current task, use COUNT to get the sum over
422 * all priority levels
423 * @return number of tasks pending right now
426 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p);
430 * Obtain the reasoning why the current task was
433 * @return task context with information why the current task is run
435 const struct GNUNET_SCHEDULER_TaskContext *
436 GNUNET_SCHEDULER_get_task_context (void);
440 * Cancel the task with the specified identifier.
441 * The task must not yet have run.
443 * @param task id of the task to cancel
444 * @return the closure of the callback of the cancelled task
447 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task);
451 * Continue the current execution with the given function. This is
452 * similar to the other "add" functions except that there is no delay
453 * and the reason code can be specified.
455 * @param task main function of the task
456 * @param task_cls closure for @a task
457 * @param reason reason for task invocation
458 * @param priority priority to use for the task
461 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
463 enum GNUNET_SCHEDULER_Reason reason,
464 enum GNUNET_SCHEDULER_Priority priority);
468 * Schedule a new task to be run with a specified priority.
470 * @param prio how important is the new task?
471 * @param task main function of the task
472 * @param task_cls closure of @a task
473 * @return unique task identifier for the job
474 * only valid until @a task is started!
476 struct GNUNET_SCHEDULER_Task *
477 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
478 GNUNET_SCHEDULER_TaskCallback task,
483 * Schedule a new task to be run as soon as possible. Note that this
484 * does not guarantee that this will be the next task that is being
485 * run, as other tasks with higher priority (or that are already ready
486 * to run) might get to run first. Just as with delays, clients must
487 * not rely on any particular order of execution between tasks
488 * scheduled concurrently.
490 * The task will be run with the DEFAULT priority.
492 * @param task main function of the task
493 * @param task_cls closure of @a task
494 * @return unique task identifier for the job
495 * only valid until @a task is started!
497 struct GNUNET_SCHEDULER_Task *
498 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
503 * Schedule a new task to be run on shutdown, that is when a CTRL-C
504 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
507 * @param task main function of the task
508 * @param task_cls closure of @a task
509 * @return unique task identifier for the job
510 * only valid until @a task is started!
512 struct GNUNET_SCHEDULER_Task *
513 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
518 * Schedule a new task to be run as soon as possible with the
519 * (transitive) ignore-shutdown flag either explicitly set or
520 * explicitly enabled. This task (and all tasks created from it,
521 * other than by another call to this function) will either count or
522 * not count for the 'lifeness' of the process. This API is only
523 * useful in a few special cases.
525 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
526 * @param task main function of the task
527 * @param task_cls closure of @a task
528 * @return unique task identifier for the job
529 * only valid until @a task is started!
531 struct GNUNET_SCHEDULER_Task *
532 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
533 GNUNET_SCHEDULER_TaskCallback task,
538 * Schedule a new task to be run with a specified delay. The task
539 * will be scheduled for execution once the delay has expired. It
540 * will be run with the DEFAULT priority.
542 * @param delay with which the operation should be run
543 * @param task main function of the task
544 * @param task_cls closure of @a task
545 * @return unique task identifier for the job
546 * only valid until @a task is started!
548 struct GNUNET_SCHEDULER_Task *
549 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
550 GNUNET_SCHEDULER_TaskCallback task,
555 * Schedule a new task to be run at the specified time. The task
556 * will be scheduled for execution once specified time has been
557 * reached. It will be run with the DEFAULT priority.
559 * @param at time at which this operation should run
560 * @param task main function of the task
561 * @param task_cls closure of @a task
562 * @return unique task identifier for the job
563 * only valid until @a task is started!
565 struct GNUNET_SCHEDULER_Task *
566 GNUNET_SCHEDULER_add_at (struct GNUNET_TIME_Absolute at,
567 GNUNET_SCHEDULER_TaskCallback task,
572 * Schedule a new task to be run with a specified delay. The task
573 * will be scheduled for execution once the delay has expired.
575 * @param delay when should this operation time out?
576 * @param priority priority to use for the task
577 * @param task main function of the task
578 * @param task_cls closure of @a task
579 * @return unique task identifier for the job
580 * only valid until @a task is started!
582 struct GNUNET_SCHEDULER_Task *
583 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
584 enum GNUNET_SCHEDULER_Priority priority,
585 GNUNET_SCHEDULER_TaskCallback task,
590 * Schedule a new task to be run at the specified time. The task
591 * will be scheduled for execution at time @a at.
593 * @param at time when the operation should run
594 * @param priority priority to use for the task
595 * @param task main function of the task
596 * @param task_cls closure of @a task
597 * @return unique task identifier for the job
598 * only valid until @a task is started!
600 struct GNUNET_SCHEDULER_Task *
601 GNUNET_SCHEDULER_add_at_with_priority (struct GNUNET_TIME_Absolute at,
602 enum GNUNET_SCHEDULER_Priority priority,
603 GNUNET_SCHEDULER_TaskCallback task,
608 * Schedule a new task to be run with a specified delay or when the
609 * specified file descriptor is ready for reading. The delay can be
610 * used as a timeout on the socket being ready. The task will be
611 * scheduled for execution once either the delay has expired or the
612 * socket operation is ready. It will be run with the DEFAULT priority.
614 * @param delay when should this operation time out?
615 * @param rfd read file-descriptor
616 * @param task main function of the task
617 * @param task_cls closure of @a task
618 * @return unique task identifier for the job
619 * only valid until @a task is started!
621 struct GNUNET_SCHEDULER_Task *
622 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
623 struct GNUNET_NETWORK_Handle *rfd,
624 GNUNET_SCHEDULER_TaskCallback task,
629 * Schedule a new task to be run with a specified priority and to be
630 * run after the specified delay or when the specified file descriptor
631 * is ready for reading. The delay can be used as a timeout on the
632 * socket being ready. The task will be scheduled for execution once
633 * either the delay has expired or the socket operation is ready. It
634 * will be run with the DEFAULT priority.
636 * @param delay when should this operation time out?
637 * @param priority priority to use for the task
638 * @param rfd read file-descriptor
639 * @param task main function of the task
640 * @param task_cls closure of @a task
641 * @return unique task identifier for the job
642 * only valid until @a task is started!
644 struct GNUNET_SCHEDULER_Task *
645 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
646 enum GNUNET_SCHEDULER_Priority priority,
647 struct GNUNET_NETWORK_Handle *rfd,
648 GNUNET_SCHEDULER_TaskCallback task,
653 * Schedule a new task to be run with a specified delay or when the
654 * specified file descriptor is ready for writing. The delay can be
655 * used as a timeout on the socket being ready. The task will be
656 * scheduled for execution once either the delay has expired or the
657 * socket operation is ready. It will be run with the DEFAULT priority.
659 * * @param delay when should this operation time out?
660 * @param wfd write file-descriptor
661 * @param task main function of the task
662 * @param task_cls closure of @a task
663 * @return unique task identifier for the job
664 * only valid until @a task is started!
666 struct GNUNET_SCHEDULER_Task *
667 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
668 struct GNUNET_NETWORK_Handle *wfd,
669 GNUNET_SCHEDULER_TaskCallback task,
674 * Schedule a new task to be run with a specified delay or when the
675 * specified file descriptor is ready. The delay can be
676 * used as a timeout on the socket being ready. The task will be
677 * scheduled for execution once either the delay has expired or the
678 * socket operation is ready.
680 * @param delay when should this operation time out?
681 * @param priority priority of the task
682 * @param fd file-descriptor
683 * @param on_read whether to poll the file-descriptor for readability
684 * @param on_write whether to poll the file-descriptor for writability
685 * @param task main function of the task
686 * @param task_cls closure of @a task
687 * @return unique task identifier for the job
688 * only valid until "task" is started!
690 struct GNUNET_SCHEDULER_Task *
691 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
692 enum GNUNET_SCHEDULER_Priority priority,
693 struct GNUNET_NETWORK_Handle *fd,
696 GNUNET_SCHEDULER_TaskCallback task,
700 * Schedule a new task to be run with a specified delay or when the
701 * specified file descriptor is ready for reading. The delay can be
702 * used as a timeout on the socket being ready. The task will be
703 * scheduled for execution once either the delay has expired or the
704 * socket operation is ready. It will be run with the DEFAULT priority.
706 * * @param delay when should this operation time out?
707 * @param rfd read file-descriptor
708 * @param task main function of the task
709 * @param task_cls closure of @a task
710 * @return unique task identifier for the job
711 * only valid until "task" is started!
713 struct GNUNET_SCHEDULER_Task *
714 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
715 const struct GNUNET_DISK_FileHandle *rfd,
716 GNUNET_SCHEDULER_TaskCallback task,
721 * Schedule a new task to be run with a specified delay or when the
722 * specified file descriptor is ready for writing. The delay can be
723 * used as a timeout on the socket being ready. The task will be
724 * scheduled for execution once either the delay has expired or the
725 * socket operation is ready. It will be run with the DEFAULT priority.
727 * * @param delay when should this operation time out?
728 * @param wfd write file-descriptor
729 * @param task main function of the task
730 * @param task_cls closure of @a task
731 * @return unique task identifier for the job
732 * only valid until @a task is started!
734 struct GNUNET_SCHEDULER_Task *
735 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
736 const struct GNUNET_DISK_FileHandle *wfd,
737 GNUNET_SCHEDULER_TaskCallback task,
742 * Schedule a new task to be run with a specified delay or when the
743 * specified file descriptor is ready. The delay can be
744 * used as a timeout on the socket being ready. The task will be
745 * scheduled for execution once either the delay has expired or the
746 * socket operation is ready.
748 * @param delay when should this operation time out?
749 * @param priority priority of the task
750 * @param fd file-descriptor
751 * @param on_read whether to poll the file-descriptor for readability
752 * @param on_write whether to poll the file-descriptor for writability
753 * @param task main function of the task
754 * @param task_cls closure of @a task
755 * @return unique task identifier for the job
756 * only valid until @a task is started!
758 struct GNUNET_SCHEDULER_Task *
759 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
760 enum GNUNET_SCHEDULER_Priority priority,
761 const struct GNUNET_DISK_FileHandle *fd,
762 int on_read, int on_write,
763 GNUNET_SCHEDULER_TaskCallback task,
768 * Schedule a new task to be run with a specified delay or when any of
769 * the specified file descriptor sets is ready. The delay can be used
770 * as a timeout on the socket(s) being ready. The task will be
771 * scheduled for execution once either the delay has expired or any of
772 * the socket operations is ready. This is the most general
773 * function of the "add" family. Note that the "prerequisite_task"
774 * must be satisfied in addition to any of the other conditions. In
775 * other words, the task will be started when
783 * @param prio how important is this task?
784 * @param delay how long should we wait?
785 * @param rs set of file descriptors we want to read (can be NULL)
786 * @param ws set of file descriptors we want to write (can be NULL)
787 * @param task main function of the task
788 * @param task_cls closure of @a task
789 * @return unique task identifier for the job
790 * only valid until "task" is started!
792 struct GNUNET_SCHEDULER_Task *
793 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
794 struct GNUNET_TIME_Relative delay,
795 const struct GNUNET_NETWORK_FDSet *rs,
796 const struct GNUNET_NETWORK_FDSet *ws,
797 GNUNET_SCHEDULER_TaskCallback task,
801 * Sets the select function to use in the scheduler (scheduler_select).
803 * @param new_select new select function to use (NULL to reset to default)
804 * @param new_select_cls closure for @a new_select
807 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
808 void *new_select_cls);
811 #if 0 /* keep Emacsens' auto-indent happy */
820 /** @} */ /* end of group scheduler */