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 * Context information passed to each scheduler task.
102 struct GNUNET_SCHEDULER_TaskContext
105 * Reason why the task is run now
107 enum GNUNET_SCHEDULER_Reason reason;
110 * Set of file descriptors ready for reading;
111 * note that additional bits may be set
112 * that were not in the original request
114 const struct GNUNET_NETWORK_FDSet *read_ready;
117 * Set of file descriptors ready for writing;
118 * note that additional bits may be set
119 * that were not in the original request.
121 const struct GNUNET_NETWORK_FDSet *write_ready;
127 * Signature of the main function of a task.
130 * @param tc context information (why was this task triggered now)
133 (*GNUNET_SCHEDULER_TaskCallback) (void *cls);
137 * Signature of the select function used by the scheduler.
138 * #GNUNET_NETWORK_socket_select matches it.
141 * @param rfds set of sockets to be checked for readability
142 * @param wfds set of sockets to be checked for writability
143 * @param efds set of sockets to be checked for exceptions
144 * @param timeout relative value when to return
145 * @return number of selected sockets, #GNUNET_SYSERR on error
148 (*GNUNET_SCHEDULER_select) (void *cls,
149 struct GNUNET_NETWORK_FDSet *rfds,
150 struct GNUNET_NETWORK_FDSet *wfds,
151 struct GNUNET_NETWORK_FDSet *efds,
152 struct GNUNET_TIME_Relative timeout);
156 * Initialize and run scheduler. This function will return when all
157 * tasks have completed. On systems with signals, receiving a SIGTERM
158 * (and other similar signals) will cause #GNUNET_SCHEDULER_shutdown
159 * to be run after the active task is complete. As a result, SIGTERM
160 * causes all shutdown tasks to be scheduled with reason
161 * #GNUNET_SCHEDULER_REASON_SHUTDOWN. (However, tasks added
162 * afterwards will execute normally!). Note that any particular
163 * signal will only shut down one scheduler; applications should
164 * always only create a single scheduler.
166 * @param task task to run first (and immediately)
167 * @param task_cls closure of @a task
170 GNUNET_SCHEDULER_run (GNUNET_SCHEDULER_TaskCallback task,
175 * Request the shutdown of a scheduler. Marks all tasks
176 * awaiting shutdown as ready. Note that tasks
177 * scheduled with #GNUNET_SCHEDULER_add_shutdown() AFTER this call
178 * will be delayed until the next shutdown signal.
181 GNUNET_SCHEDULER_shutdown (void);
185 * Get information about the current load of this scheduler. Use this
186 * function to determine if an elective task should be added or simply
187 * dropped (if the decision should be made based on the number of
188 * tasks ready to run).
190 * @param p priority-level to query, use KEEP to query the level
191 * of the current task, use COUNT to get the sum over
192 * all priority levels
193 * @return number of tasks pending right now
196 GNUNET_SCHEDULER_get_load (enum GNUNET_SCHEDULER_Priority p);
200 * Obtain the reasoning why the current task was
203 * @return task context with information why the current task is run
205 const struct GNUNET_SCHEDULER_TaskContext *
206 GNUNET_SCHEDULER_get_task_context (void);
210 * Cancel the task with the specified identifier.
211 * The task must not yet have run.
213 * @param task id of the task to cancel
214 * @return the closure of the callback of the cancelled task
217 GNUNET_SCHEDULER_cancel (struct GNUNET_SCHEDULER_Task *task);
221 * Continue the current execution with the given function. This is
222 * similar to the other "add" functions except that there is no delay
223 * and the reason code can be specified.
225 * @param task main function of the task
226 * @param task_cls closure for @a task
227 * @param reason reason for task invocation
228 * @param priority priority to use for the task
231 GNUNET_SCHEDULER_add_with_reason_and_priority (GNUNET_SCHEDULER_TaskCallback task,
233 enum GNUNET_SCHEDULER_Reason reason,
234 enum GNUNET_SCHEDULER_Priority priority);
238 * Schedule a new task to be run with a specified priority.
240 * @param prio how important is the new task?
241 * @param task main function of the task
242 * @param task_cls closure of @a task
243 * @return unique task identifier for the job
244 * only valid until @a task is started!
246 struct GNUNET_SCHEDULER_Task *
247 GNUNET_SCHEDULER_add_with_priority (enum GNUNET_SCHEDULER_Priority prio,
248 GNUNET_SCHEDULER_TaskCallback task,
253 * Schedule a new task to be run as soon as possible. Note that this
254 * does not guarantee that this will be the next task that is being
255 * run, as other tasks with higher priority (or that are already ready
256 * to run) might get to run first. Just as with delays, clients must
257 * not rely on any particular order of execution between tasks
258 * scheduled concurrently.
260 * The task will be run with the DEFAULT priority.
262 * @param task main function of the task
263 * @param task_cls closure of @a task
264 * @return unique task identifier for the job
265 * only valid until @a task is started!
267 struct GNUNET_SCHEDULER_Task *
268 GNUNET_SCHEDULER_add_now (GNUNET_SCHEDULER_TaskCallback task,
273 * Schedule a new task to be run on shutdown, that is when a CTRL-C
274 * signal is received, or when #GNUNET_SCHEDULER_shutdown() is being
277 * @param task main function of the task
278 * @param task_cls closure of @a task
279 * @return unique task identifier for the job
280 * only valid until @a task is started!
282 struct GNUNET_SCHEDULER_Task *
283 GNUNET_SCHEDULER_add_shutdown (GNUNET_SCHEDULER_TaskCallback task,
288 * Schedule a new task to be run as soon as possible with the
289 * (transitive) ignore-shutdown flag either explicitly set or
290 * explicitly enabled. This task (and all tasks created from it,
291 * other than by another call to this function) will either count or
292 * not count for the 'lifeness' of the process. This API is only
293 * useful in a few special cases.
295 * @param lifeness #GNUNET_YES if the task counts for lifeness, #GNUNET_NO if not.
296 * @param task main function of the task
297 * @param task_cls closure of @a task
298 * @return unique task identifier for the job
299 * only valid until @a task is started!
301 struct GNUNET_SCHEDULER_Task *
302 GNUNET_SCHEDULER_add_now_with_lifeness (int lifeness,
303 GNUNET_SCHEDULER_TaskCallback task,
308 * Schedule a new task to be run with a specified delay. The task
309 * will be scheduled for execution once the delay has expired. It
310 * will be run with the DEFAULT priority.
312 * @param delay when should this operation time out?
313 * @param task main function of the task
314 * @param task_cls closure of @a task
315 * @return unique task identifier for the job
316 * only valid until @a task is started!
318 struct GNUNET_SCHEDULER_Task *
319 GNUNET_SCHEDULER_add_delayed (struct GNUNET_TIME_Relative delay,
320 GNUNET_SCHEDULER_TaskCallback task,
325 * Schedule a new task to be run with a specified delay. The task
326 * will be scheduled for execution once the delay has expired.
328 * @param delay when should this operation time out?
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?
350 * @param rfd read file-descriptor
351 * @param task main function of the task
352 * @param task_cls closure of @a task
353 * @return unique task identifier for the job
354 * only valid until @a task is started!
356 struct GNUNET_SCHEDULER_Task *
357 GNUNET_SCHEDULER_add_read_net (struct GNUNET_TIME_Relative delay,
358 struct GNUNET_NETWORK_Handle *rfd,
359 GNUNET_SCHEDULER_TaskCallback task,
364 * Schedule a new task to be run with a specified priority and to be
365 * run after the specified delay or when the specified file descriptor
366 * is ready for reading. The delay can be used as a timeout on the
367 * socket being ready. The task will be scheduled for execution once
368 * either the delay has expired or the socket operation is ready. It
369 * will be run with the DEFAULT priority.
371 * @param delay when should this operation time out?
372 * @param priority priority to use for the task
373 * @param rfd read file-descriptor
374 * @param task main function of the task
375 * @param task_cls closure of @a task
376 * @return unique task identifier for the job
377 * only valid until @a task is started!
379 struct GNUNET_SCHEDULER_Task *
380 GNUNET_SCHEDULER_add_read_net_with_priority (struct GNUNET_TIME_Relative delay,
381 enum GNUNET_SCHEDULER_Priority priority,
382 struct GNUNET_NETWORK_Handle *rfd,
383 GNUNET_SCHEDULER_TaskCallback task,
388 * Schedule a new task to be run with a specified delay or when the
389 * specified file descriptor is ready for writing. The delay can be
390 * used as a timeout on the socket being ready. The task will be
391 * scheduled for execution once either the delay has expired or the
392 * socket operation is ready. It will be run with the DEFAULT priority.
394 * * @param delay when should this operation time out?
395 * @param wfd write file-descriptor
396 * @param task main function of the task
397 * @param task_cls closure of @a task
398 * @return unique task identifier for the job
399 * only valid until @a task is started!
401 struct GNUNET_SCHEDULER_Task *
402 GNUNET_SCHEDULER_add_write_net (struct GNUNET_TIME_Relative delay,
403 struct GNUNET_NETWORK_Handle *wfd,
404 GNUNET_SCHEDULER_TaskCallback task,
409 * Schedule a new task to be run with a specified delay or when the
410 * specified file descriptor is ready. The delay can be
411 * used as a timeout on the socket being ready. The task will be
412 * scheduled for execution once either the delay has expired or the
413 * socket operation is ready.
415 * @param delay when should this operation time out?
416 * @param priority priority of the task
417 * @param fd file-descriptor
418 * @param on_read whether to poll the file-descriptor for readability
419 * @param on_write whether to poll the file-descriptor for writability
420 * @param task main function of the task
421 * @param task_cls closure of @a task
422 * @return unique task identifier for the job
423 * only valid until "task" is started!
425 struct GNUNET_SCHEDULER_Task *
426 GNUNET_SCHEDULER_add_net_with_priority (struct GNUNET_TIME_Relative delay,
427 enum GNUNET_SCHEDULER_Priority priority,
428 struct GNUNET_NETWORK_Handle *fd,
431 GNUNET_SCHEDULER_TaskCallback task,
435 * Schedule a new task to be run with a specified delay or when the
436 * specified file descriptor is ready for reading. The delay can be
437 * used as a timeout on the socket being ready. The task will be
438 * scheduled for execution once either the delay has expired or the
439 * socket operation is ready. It will be run with the DEFAULT priority.
441 * * @param delay when should this operation time out?
442 * @param rfd read file-descriptor
443 * @param task main function of the task
444 * @param task_cls closure of @a task
445 * @return unique task identifier for the job
446 * only valid until "task" is started!
448 struct GNUNET_SCHEDULER_Task *
449 GNUNET_SCHEDULER_add_read_file (struct GNUNET_TIME_Relative delay,
450 const struct GNUNET_DISK_FileHandle *rfd,
451 GNUNET_SCHEDULER_TaskCallback task,
456 * Schedule a new task to be run with a specified delay or when the
457 * specified file descriptor is ready for writing. The delay can be
458 * used as a timeout on the socket being ready. The task will be
459 * scheduled for execution once either the delay has expired or the
460 * socket operation is ready. It will be run with the DEFAULT priority.
462 * * @param delay when should this operation time out?
463 * @param wfd write file-descriptor
464 * @param task main function of the task
465 * @param task_cls closure of @a task
466 * @return unique task identifier for the job
467 * only valid until @a task is started!
469 struct GNUNET_SCHEDULER_Task *
470 GNUNET_SCHEDULER_add_write_file (struct GNUNET_TIME_Relative delay,
471 const struct GNUNET_DISK_FileHandle *wfd,
472 GNUNET_SCHEDULER_TaskCallback task,
477 * Schedule a new task to be run with a specified delay or when the
478 * specified file descriptor is ready. The delay can be
479 * used as a timeout on the socket being ready. The task will be
480 * scheduled for execution once either the delay has expired or the
481 * socket operation is ready.
483 * @param delay when should this operation time out?
484 * @param priority priority of the task
485 * @param fd file-descriptor
486 * @param on_read whether to poll the file-descriptor for readability
487 * @param on_write whether to poll the file-descriptor for writability
488 * @param task main function of the task
489 * @param task_cls closure of @a task
490 * @return unique task identifier for the job
491 * only valid until @a task is started!
493 struct GNUNET_SCHEDULER_Task *
494 GNUNET_SCHEDULER_add_file_with_priority (struct GNUNET_TIME_Relative delay,
495 enum GNUNET_SCHEDULER_Priority priority,
496 const struct GNUNET_DISK_FileHandle *fd,
497 int on_read, int on_write,
498 GNUNET_SCHEDULER_TaskCallback task,
503 * Schedule a new task to be run with a specified delay or when any of
504 * the specified file descriptor sets is ready. The delay can be used
505 * as a timeout on the socket(s) being ready. The task will be
506 * scheduled for execution once either the delay has expired or any of
507 * the socket operations is ready. This is the most general
508 * function of the "add" family. Note that the "prerequisite_task"
509 * must be satisfied in addition to any of the other conditions. In
510 * other words, the task will be started when
518 * @param prio how important is this task?
519 * @param delay how long should we wait?
520 * @param rs set of file descriptors we want to read (can be NULL)
521 * @param ws set of file descriptors we want to write (can be NULL)
522 * @param task main function of the task
523 * @param task_cls closure of @a task
524 * @return unique task identifier for the job
525 * only valid until "task" is started!
527 struct GNUNET_SCHEDULER_Task *
528 GNUNET_SCHEDULER_add_select (enum GNUNET_SCHEDULER_Priority prio,
529 struct GNUNET_TIME_Relative delay,
530 const struct GNUNET_NETWORK_FDSet *rs,
531 const struct GNUNET_NETWORK_FDSet *ws,
532 GNUNET_SCHEDULER_TaskCallback task,
536 * Sets the select function to use in the scheduler (scheduler_select).
538 * @param new_select new select function to use (NULL to reset to default)
539 * @param new_select_cls closure for 'new_select'
542 GNUNET_SCHEDULER_set_select (GNUNET_SCHEDULER_select new_select,
543 void *new_select_cls);
546 #if 0 /* keep Emacsens' auto-indent happy */
555 /** @} */ /* end of group scheduler */