1 /* Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
3 /** \file sched.h Sched and task structures and exported symbols from sched.c. */
7 * Paraslash's scheduler.
9 * Designed with KISS in mind. It maintains a list of task structures which is
10 * extended when a new task is registered. Each task may define a pre_select
11 * function which is called from the scheduler main loop before it calls
12 * select(). Similarly, each task must define a post_select function which is
13 * called after the select call.
15 * \sa select(2), poll(2).
18 /** Initial value (in milliseconds) before any pre_select call. */
20 /** The timeout (also in milliseconds) for the next select call. */
22 /** fds that should be watched for readability. */
24 /** fds that should be watched for writability. */
26 /** Highest numbered file descriptor in any of the above fd sets. */
28 /** If non-NULL, use this function instead of para_select. */
29 int (*select_function)(int, fd_set *, fd_set *, int timeout);
30 /** Tasks which have been registered to the scheduler. */
31 struct list_head task_list;
36 /** Information that must be supplied by callers of \ref task_register(). */
38 /** Used for log messages and by \ref get_task_list(). */
41 * Configure watch fds and impose an upper bound on the I/O timeout.
43 * If this is not NULL, the function is called at each iteration of the
44 * scheduler's main loop. Its purpose is to tell the scheduler that
45 * certain file descriptors should be monitored for readiness for I/O.
46 * The function may also lower the scheduler's timeout value (but shall
47 * never increase it) to impose an upper bound on the waiting time in
48 * case no file descriptors happen to be ready.
52 void (*pre_select)(struct sched *s, void *context);
54 * Perform I/O on file descriptors which are ready for I/O.
56 * This mandatory hook is called after the system call which monitors
57 * file descriptors returns. The function should perform non-blocking
58 * I/O on those file descriptors which are reported as being ready.
60 * If this function returns a negative value, the scheduler unregisters
63 int (*post_select)(struct sched *s, void *context);
65 * This pointer is saved when the task is registered. It is passed to
66 * ->pre_select() and ->post_select(). Usually this is a pointer to the
67 * struct owned by the caller which contains the task pointer.
73 * This is set by the scheduler at the beginning of its main loop. It may be
74 * used (read-only) from everywhere. As none of the functions called by the
75 * scheduler are allowed to block, this value should be accurate enough so that
76 * there is no need to call clock_gettime() directly.
78 extern const struct timeval *now;
80 struct task *task_register(struct task_info *info, struct sched *s);
81 int schedule(struct sched *s);
82 void sched_shutdown(struct sched *s);
83 char *get_task_list(struct sched *s);
84 void task_notify(struct task *t, int err);
85 void task_notify_all(struct sched *s, int err);
86 int task_get_notification(const struct task *t);
87 int task_status(const struct task *t);
88 int task_reap(struct task **tptr);
89 void sched_min_delay(struct sched *s);
90 void sched_request_timeout(struct timeval *to, struct sched *s);
91 void sched_request_timeout_ms(long unsigned ms, struct sched *s);
92 int sched_request_barrier(struct timeval *barrier, struct sched *s);
93 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s);
94 void sched_monitor_readfd(int fd, struct sched *s);
95 void sched_monitor_writefd(int fd, struct sched *s);
97 static inline bool sched_read_ok(int fd, const struct sched *s)
99 return FD_ISSET(fd, &s->rfds);
102 static inline bool sched_write_ok(int fd, const struct sched *s)
104 return FD_ISSET(fd, &s->wfds);