2 * Copyright (C) 2006-2014 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file sched.c Paraslash's scheduling functions. */
21 static struct timeval now_struct;
22 struct timeval *now = &now_struct;
24 static inline bool timeout_is_zero(struct sched *s)
26 struct timeval *tv = &s->select_timeout;
27 return tv->tv_sec == 0 && tv->tv_usec == 0;
30 static void sched_preselect(struct sched *s)
34 list_for_each_entry_safe(t, tmp, &s->task_list, node) {
37 if (t->notification != 0)
44 //#define SCHED_DEBUG 1
45 static inline void call_post_select(struct sched *s, struct task *t)
48 t->error = t->post_select(s, t);
50 struct timeval t1, t2, diff;
53 clock_get_realtime(&t1);
54 t->error = t->post_select(s, t);
55 clock_get_realtime(&t2);
56 tv_diff(&t1, &t2, &diff);
59 PARA_WARNING_LOG("%s: post_select time: %lums\n",
64 static void sched_post_select(struct sched *s)
68 list_for_each_entry_safe(t, tmp, &s->task_list, node) {
69 call_post_select(s, t);
77 * The core function of all paraslash programs.
79 * \param s Pointer to the scheduler struct.
81 * This function updates the global \a now pointer, calls all registered
82 * pre_select hooks which may set the timeout and add any file descriptors to
83 * the fd sets of \a s. Next, it calls para_select() and makes the result available
84 * to the registered tasks by calling their post_select hook.
86 * \return Zero if no more tasks are left in the task list, negative if the
87 * select function returned an error.
89 * \sa \ref task, \ref now.
91 int schedule(struct sched *s)
95 if (!s->select_function)
96 s->select_function = para_select;
100 s->select_timeout = s->default_timeout;
102 clock_get_realtime(now);
104 ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds,
110 * APUE: Be careful not to check the descriptor sets on return
111 * unless the return value is greater than zero. The return
112 * state of the descriptor sets is implementation dependent if
113 * either a signal is caught or the timer expires.
118 clock_get_realtime(now);
119 sched_post_select(s);
120 if (list_empty(&s->task_list))
126 * Add a task to the scheduler.
128 * \param t The task to add.
129 * \param s The scheduler instance to add the task to.
131 * \sa task::pre_select, task::post_select
133 void register_task(struct sched *s, struct task *t)
135 PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
136 assert(t->post_select);
138 if (!s->task_list.next)
139 INIT_LIST_HEAD(&s->task_list);
140 list_add_tail(&t->node, &s->task_list);
144 * Get the list of all registered tasks.
146 * \param s The scheduler instance to get the task list from.
148 * \return The task list.
150 * Each entry of the list contains an identifier which is simply a hex number.
151 * The result is dynamically allocated and must be freed by the caller.
153 char *get_task_list(struct sched *s)
155 struct task *t, *tmp;
158 list_for_each_entry_safe(t, tmp, &s->task_list, node) {
160 tmp_msg = make_message("%s%p\t%s\n", msg? msg : "", t, t->status);
168 * Set the notification value of a task.
170 * \param t The task to notify.
171 * \param err A positive error code.
173 * Tasks which honor notifications are supposed to call \ref
174 * task_get_notification() in their post_select function and act on the
175 * returned notification value.
177 * If the scheduler detects during its pre_select loop that at least one task
178 * has been notified, the loop terminates, and the post_select methods of all
179 * taks are immediately called again.
181 * The notification for a task is reset after the call to its post_select
184 * \sa \ref task_get_notification().
186 void task_notify(struct task *t, int err)
189 if (t->notification == -err) /* ignore subsequent notifications */
191 PARA_INFO_LOG("notifying task %s: %s\n", t->status, para_strerror(err));
192 t->notification = -err;
196 * Return the notification value of a task.
198 * \param t The task to get the notification value from.
200 * \return The notification value. If this is negative, the task has been
201 * notified by another task. Tasks are supposed to check for notifications by
202 * calling this function from their post_select method.
204 * \sa \ref task_notify().
206 int task_get_notification(struct task *t)
208 return t->notification;
212 * Set the notification value of all tasks of a scheduler instance.
214 * \param s The scheduler instance whose tasks should be notified.
215 * \param err A positive error code.
217 * This simply iterates over all existing tasks of \a s and sets each
218 * task's notification value to \p -err.
220 void task_notify_all(struct sched *s, int err)
224 list_for_each_entry(t, &s->task_list, node)
229 * Set the select timeout to the minimal possible value.
231 * \param s Pointer to the scheduler struct.
233 * This causes the next select() call to return immediately.
235 void sched_min_delay(struct sched *s)
237 s->select_timeout.tv_sec = s->select_timeout.tv_usec = 0;
241 * Impose an upper bound for the timeout of the next select() call.
243 * \param to Maximal allowed timeout.
244 * \param s Pointer to the scheduler struct.
246 * If the current scheduler timeout is already smaller than \a to, this
247 * function does nothing. Otherwise the timeout for the next select() call is
248 * set to the given value.
250 * \sa sched_request_timeout_ms().
252 void sched_request_timeout(struct timeval *to, struct sched *s)
254 if (tv_diff(&s->select_timeout, to, NULL) > 0)
255 s->select_timeout = *to;
259 * Force the next select() call to return before the given amount of milliseconds.
261 * \param ms The maximal allowed timeout in milliseconds.
262 * \param s Pointer to the scheduler struct.
264 * Like sched_request_timeout() this imposes an upper bound on the timeout
265 * value for the next select() call.
267 void sched_request_timeout_ms(long unsigned ms, struct sched *s)
271 sched_request_timeout(&tv, s);
275 * Force the next select() call to return before the given future time.
277 * \param barrier Absolute time before select() should return.
278 * \param s Pointer to the scheduler struct.
280 * \return If \a barrier is in the past, this function does nothing and returns
281 * zero. Otherwise it returns one.
283 * \sa sched_request_barrier_or_min_delay().
285 int sched_request_barrier(struct timeval *barrier, struct sched *s)
289 if (tv_diff(now, barrier, &diff) > 0)
291 sched_request_timeout(&diff, s);
296 * Force the next select() call to return before the given time.
298 * \param barrier Absolute time before select() should return.
299 * \param s Pointer to the scheduler struct.
301 * \return If \a barrier is in the past, this function requests a minimal
302 * timeout and returns zero. Otherwise it returns one.
304 * \sa sched_min_delay(), sched_request_barrier().
306 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s)
310 if (tv_diff(now, barrier, &diff) > 0) {
314 sched_request_timeout(&diff, s);