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
69 * (regardless of timeout, etc.).
71 GNUNET_SCHEDULER_REASON_SHUTDOWN = 2,
74 * The specified timeout has expired.
75 * (also set if the delay given was 0).
77 GNUNET_SCHEDULER_REASON_TIMEOUT = 4,
80 * The reading socket is ready.
82 GNUNET_SCHEDULER_REASON_READ_READY = 8,
85 * The writing socket is ready.
87 GNUNET_SCHEDULER_REASON_WRITE_READY = 16,
90 * The prerequisite task is done.
92 GNUNET_SCHEDULER_REASON_PREREQ_DONE = 32
96 #include "gnunet_time_lib.h"
97 #include "gnunet_network_lib.h"
101 * Context information passed to each scheduler task.
103 struct GNUNET_SCHEDULER_TaskContext
106 * Reason why the task is run now
108 enum GNUNET_SCHEDULER_Reason reason;
111 * Set of file descriptors ready for reading;
112 * note that additional bits may be set
113 * that were not in the original request
115 const struct GNUNET_NETWORK_FDSet *read_ready;
118 * Set of file descriptors ready for writing;
119 * note that additional bits may be set
120 * that were not in the original request.
122 const struct GNUNET_NETWORK_FDSet *write_ready;
128 * Signature of the main function of a task.
131 * @param tc context information (why was this task triggered now)
134 (*GNUNET_SCHEDULER_TaskCallback) (void *cls);
138 * Signature of the select function used by the scheduler.
139 * #GNUNET_NETWORK_socket_select matches it.
142 * @param rfds set of sockets to be checked for readability
143 * @param wfds set of sockets to be checked for writability
144 * @param efds set of sockets to be checked for exceptions
145 * @param timeout relative value when to return
146 * @return number of selected sockets, #GNUNET_SYSERR on error
149 (*GNUNET_SCHEDULER_select) (void *cls,
150 struct GNUNET_NETWORK_FDSet *rfds,
151 struct GNUNET_NETWORK_FDSet *wfds,
152 struct GNUNET_NETWORK_FDSet *efds,
153 struct GNUNET_TIME_Relative timeout);
157 * Initialize and run scheduler. This function will return when all
158 * tasks have completed. On systems with signals, receiving a SIGTERM
159 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
160 * to be run after the active task is complete. As a result, SIGTERM
161 * causes all active tasks to be scheduled with reason
162 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
163 * afterwards will execute normally!). Note that any particular
164 * signal will only shut down one scheduler; applications should
165 * always only create a single scheduler.
167 * @param task task to run first (and immediately)
168 * @param task_cls closure of @a task
171 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
176 * Request the shutdown of the scheduler. Marks all currently
177 * pending tasks as ready because of shutdown. This will
178 * cause all tasks to run (as soon as possible, respecting
179 * priorities and prerequisite tasks). Note that tasks
180 * scheduled AFTER this call may still be delayed arbitrarily.
183 GNUNET_SCHEDULER_shutdown (void);
187 * Get information about the current load of this scheduler. Use this
188 * function to determine if an elective task should be added or simply
189 * dropped (if the decision should be made based on the number of
190 * tasks ready to run).
192 * @param p priority-level to query, use KEEP to query the level
193 * of the current task, use COUNT to get the sum over
194 * all priority levels
195 * @return number of tasks pending right now
198 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p);
202 * Obtain the reasoning why the current task was
205 * @return task context with information why the current task is run
207 const struct GNUNET_SCHEDULER_TaskContext *
208 GNUNET_SCHEDULER_get_task_context (void);
212 * Cancel the task with the specified identifier.
213 * The task must not yet have run.
215 * @param task id of the task to cancel
216 * @return the closure of the callback of the cancelled task
219 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task);
223 * Continue the current execution with the given function. This is
224 * similar to the other "add" functions except that there is no delay
225 * and the reason code can be specified.
227 * @param task main function of the task
228 * @param task_cls closure for @a task
229 * @param reason reason for task invocation
230 * @param priority priority to use for the task
233 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
235 enum GNUNET_SCHEDULER_Reason reason,
236 enum GNUNET_SCHEDULER_Priority priority);
240 * Schedule a new task to be run with a specified priority.
242 * @param prio how important is the new task?
243 * @param task main function of the task
244 * @param task_cls closure of @a task
245 * @return unique task identifier for the job
246 * only valid until @a task is started!
248 struct GNUNET_SCHEDULER_Task *
249 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
250 GNUNET_SCHEDULER_TaskCallback task,
255 * Schedule a new task to be run as soon as possible. Note that this
256 * does not guarantee that this will be the next task that is being
257 * run, as other tasks with higher priority (or that are already ready
258 * to run) might get to run first. Just as with delays, clients must
259 * not rely on any particular order of execution between tasks
260 * scheduled concurrently.
262 * The task will be run with the DEFAULT priority.
264 * @param task main function of the task
265 * @param task_cls closure of @a task
266 * @return unique task identifier for the job
267 * only valid until @a task is started!
269 struct GNUNET_SCHEDULER_Task *
270 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
275 * Schedule a new task to be run as soon as possible with the
276 * (transitive) ignore-shutdown flag either explicitly set or
277 * explicitly enabled. This task (and all tasks created from it,
278 * other than by another call to this function) will either count or
279 * not count for the 'lifeness' of the process. This API is only
280 * useful in a few special cases.
282 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
283 * @param task main function of the task
284 * @param task_cls closure of @a task
285 * @return unique task identifier for the job
286 * only valid until @a task is started!
288 struct GNUNET_SCHEDULER_Task *
289 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
290 GNUNET_SCHEDULER_TaskCallback task,
295 * Schedule a new task to be run with a specified delay. The task
296 * will be scheduled for execution once the delay has expired. It
297 * will be run with the DEFAULT priority.
299 * * @param delay when should this operation time out? Use
300 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
301 * @param task main function of the task
302 * @param task_cls closure of @a task
303 * @return unique task identifier for the job
304 * only valid until @a task is started!
306 struct GNUNET_SCHEDULER_Task *
307 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
308 GNUNET_SCHEDULER_TaskCallback task,
313 * Schedule a new task to be run with a specified delay. The task
314 * will be scheduled for execution once the delay has expired.
316 * @param delay when should this operation time out? Use
317 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
318 * @param priority priority to use for the task
319 * @param task main function of the task
320 * @param task_cls closure of @a task
321 * @return unique task identifier for the job
322 * only valid until @a task is started!
324 struct GNUNET_SCHEDULER_Task *
325 GNUNET_SCHEDULER_add_delayed_with_priority (struct GNUNET_TIME_Relative delay,
326 enum GNUNET_SCHEDULER_Priority priority,
327 GNUNET_SCHEDULER_TaskCallback task,
332 * Schedule a new task to be run with a specified delay or when the
333 * specified file descriptor is ready for reading. The delay can be
334 * used as a timeout on the socket being ready. The task will be
335 * scheduled for execution once either the delay has expired or the
336 * socket operation is ready. It will be run with the DEFAULT priority.
338 * * @param delay when should this operation time out? Use
339 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
340 * @param rfd read file-descriptor
341 * @param task main function of the task
342 * @param task_cls closure of @a task
343 * @return unique task identifier for the job
344 * only valid until @a task is started!
346 struct GNUNET_SCHEDULER_Task *
347 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
348 struct GNUNET_NETWORK_Handle *rfd,
349 GNUNET_SCHEDULER_TaskCallback task,
354 * Schedule a new task to be run with a specified priority and to be
355 * run after the specified delay or when the specified file descriptor
356 * is ready for reading. The delay can be used as a timeout on the
357 * socket being ready. The task will be scheduled for execution once
358 * either the delay has expired or the socket operation is ready. It
359 * will be run with the DEFAULT priority.
361 * @param delay when should this operation time out? Use
362 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
363 * @param priority priority to use for the task
364 * @param rfd read file-descriptor
365 * @param task main function of the task
366 * @param task_cls closure of @a task
367 * @return unique task identifier for the job
368 * only valid until @a task is started!
370 struct GNUNET_SCHEDULER_Task *
371 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
372 enum GNUNET_SCHEDULER_Priority priority,
373 struct GNUNET_NETWORK_Handle *rfd,
374 GNUNET_SCHEDULER_TaskCallback task,
379 * Schedule a new task to be run with a specified delay or when the
380 * specified file descriptor is ready for writing. 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 DEFAULT priority.
385 * * @param delay when should this operation time out? Use
386 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
387 * @param wfd write file-descriptor
388 * @param task main function of the task
389 * @param task_cls closure of @a task
390 * @return unique task identifier for the job
391 * only valid until @a task is started!
393 struct GNUNET_SCHEDULER_Task *
394 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
395 struct GNUNET_NETWORK_Handle *wfd,
396 GNUNET_SCHEDULER_TaskCallback task,
401 * Schedule a new task to be run with a specified delay or when the
402 * specified file descriptor is ready. 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.
407 * @param delay when should this operation time out? Use
408 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
409 * @param priority priority of the task
410 * @param fd file-descriptor
411 * @param on_read whether to poll the file-descriptor for readability
412 * @param on_write whether to poll the file-descriptor for writability
413 * @param task main function of the task
414 * @param task_cls closure of @a task
415 * @return unique task identifier for the job
416 * only valid until "task" is started!
418 struct GNUNET_SCHEDULER_Task *
419 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
420 enum GNUNET_SCHEDULER_Priority priority,
421 struct GNUNET_NETWORK_Handle *fd,
424 GNUNET_SCHEDULER_TaskCallback task,
428 * Schedule a new task to be run with a specified delay or when the
429 * specified file descriptor is ready for reading. The delay can be
430 * used as a timeout on the socket being ready. The task will be
431 * scheduled for execution once either the delay has expired or the
432 * socket operation is ready. It will be run with the DEFAULT priority.
434 * * @param delay when should this operation time out? Use
435 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
436 * @param rfd read file-descriptor
437 * @param task main function of the task
438 * @param task_cls closure of @a task
439 * @return unique task identifier for the job
440 * only valid until "task" is started!
442 struct GNUNET_SCHEDULER_Task *
443 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
444 const struct GNUNET_DISK_FileHandle *rfd,
445 GNUNET_SCHEDULER_TaskCallback task,
450 * Schedule a new task to be run with a specified delay or when the
451 * specified file descriptor is ready for writing. The delay can be
452 * used as a timeout on the socket being ready. The task will be
453 * scheduled for execution once either the delay has expired or the
454 * socket operation is ready. It will be run with the DEFAULT priority.
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 @a task
461 * @return unique task identifier for the job
462 * only valid until @a task is started!
464 struct GNUNET_SCHEDULER_Task *
465 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
466 const struct GNUNET_DISK_FileHandle *wfd,
467 GNUNET_SCHEDULER_TaskCallback task,
472 * Schedule a new task to be run with a specified delay or when the
473 * specified file descriptor is ready. The delay can be
474 * used as a timeout on the socket being ready. The task will be
475 * scheduled for execution once either the delay has expired or the
476 * socket operation is ready.
478 * @param delay when should this operation time out? Use
479 * #GNUNET_TIME_UNIT_FOREVER_REL for "on shutdown"
480 * @param priority priority of the task
481 * @param fd file-descriptor
482 * @param on_read whether to poll the file-descriptor for readability
483 * @param on_write whether to poll the file-descriptor for writability
484 * @param task main function of the task
485 * @param task_cls closure of @a task
486 * @return unique task identifier for the job
487 * only valid until @a task is started!
489 struct GNUNET_SCHEDULER_Task *
490 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
491 enum GNUNET_SCHEDULER_Priority priority,
492 const struct GNUNET_DISK_FileHandle *fd,
493 int on_read, int on_write,
494 GNUNET_SCHEDULER_TaskCallback task,
499 * Schedule a new task to be run with a specified delay or when any of
500 * the specified file descriptor sets is ready. The delay can be used
501 * as a timeout on the socket(s) being ready. The task will be
502 * scheduled for execution once either the delay has expired or any of
503 * the socket operations is ready. This is the most general
504 * function of the "add" family. Note that the "prerequisite_task"
505 * must be satisfied in addition to any of the other conditions. In
506 * other words, the task will be started when
512 * || shutdown-active)
515 * @param prio how important is this task?
516 * @param delay how long should we wait? Use #GNUNET_TIME_UNIT_FOREVER_REL for "forever",
517 * which means that the task will only be run after we receive SIGTERM
518 * @param rs set of file descriptors we want to read (can be NULL)
519 * @param ws set of file descriptors we want to write (can be NULL)
520 * @param task main function of the task
521 * @param task_cls closure of @a task
522 * @return unique task identifier for the job
523 * only valid until "task" is started!
525 struct GNUNET_SCHEDULER_Task *
526 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
527 struct GNUNET_TIME_Relative delay,
528 const struct GNUNET_NETWORK_FDSet *rs,
529 const struct GNUNET_NETWORK_FDSet *ws,
530 GNUNET_SCHEDULER_TaskCallback task,
534 * Sets the select function to use in the scheduler (scheduler_select).
536 * @param new_select new select function to use (NULL to reset to default)
537 * @param new_select_cls closure for 'new_select'
540 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
541 void *new_select_cls);
544 #if 0 /* keep Emacsens' auto-indent happy */
553 /** @} */ /* end of group scheduler */