2 * Copyright (C) 2006-2013 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;
25 * Remove a task from the scheduler.
27 * \param t The task to remove.
29 * If the pre_select pointer of \a t is not \p NULL, it is removed from
30 * the pre_select list of the scheduler. Same goes for \a post_select.
32 static void unregister_task(struct task *t)
35 PARA_INFO_LOG("unregistering %s (%s)\n", t->status,
36 para_strerror(-t->error));
38 list_del(&t->pre_select_node);
39 if (t->new_post_select || t->post_select)
40 list_del(&t->post_select_node);
43 static inline bool timeout_is_zero(struct sched *s)
45 struct timeval *tv = &s->select_timeout;
46 return tv->tv_sec == 0 && tv->tv_usec == 0;
49 static void sched_preselect(struct sched *s)
53 list_for_each_entry_safe(t, tmp, &s->pre_select_list, pre_select_node) {
54 if (t->notification != 0)
61 //#define SCHED_DEBUG 1
62 static inline void call_post_select(struct sched *s, struct task *t)
65 if (t->new_post_select) {
66 t->error = t->new_post_select(s, t);
69 return t->post_select(s, t);
71 struct timeval t1, t2, diff;
74 clock_get_realtime(&t1);
75 if (t->new_post_select)
76 t->error = t->new_post_select(s, t);
79 clock_get_realtime(&t2);
80 tv_diff(&t1, &t2, &diff);
83 PARA_WARNING_LOG("%s: post_select time: %lums\n",
88 static void sched_post_select(struct sched *s)
92 list_for_each_entry_safe(t, tmp, &s->post_select_list, post_select_node) {
94 call_post_select(s, t);
95 // PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
104 * The core function for all paraslash programs.
106 * \param s Pointer to the scheduler struct.
108 * This function updates the global \a now pointer, calls all registered
109 * pre_select hooks which may set the timeout and add any file descriptors to
110 * the fd sets of \a s. Next, it calls para_select() and makes the result available
111 * to the registered tasks by calling their post_select hook.
113 * \return Zero if no more tasks are left in either of the two lists, negative
114 * if para_select returned an error.
118 int schedule(struct sched *s)
122 if (!s->select_function)
123 s->select_function = para_select;
127 s->select_timeout = s->default_timeout;
129 clock_get_realtime(now);
131 ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds,
137 * APUE: Be careful not to check the descriptor sets on return
138 * unless the return value is greater than zero. The return
139 * state of the descriptor sets is implementation dependent if
140 * either a signal is caught or the timer expires.
145 clock_get_realtime(now);
146 sched_post_select(s);
147 if (list_empty(&s->pre_select_list) && list_empty(&s->post_select_list))
153 * Add a task to the scheduler.
155 * \param t The task to add.
156 * \param s The scheduler instance to add the task to.
158 * If the pre_select pointer of \a t is not \p NULL, it is added to
159 * the pre_select list of the scheduler. Same goes for post_select.
161 * \sa task::pre_select, task::post_select
163 void register_task(struct sched *s, struct task *t)
165 PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
167 if (!s->pre_select_list.next)
168 INIT_LIST_HEAD(&s->pre_select_list);
169 if (!s->post_select_list.next)
170 INIT_LIST_HEAD(&s->post_select_list);
172 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
173 list_add_tail(&t->pre_select_node, &s->pre_select_list);
175 if (t->new_post_select) {
176 PARA_DEBUG_LOG("post_select: %p\n", &t->new_post_select);
177 list_add_tail(&t->post_select_node, &s->post_select_list);
178 } else if ((t->post_select)) {
179 PARA_DEBUG_LOG("post_select: %p\n", &t->post_select);
180 list_add_tail(&t->post_select_node, &s->post_select_list);
185 * Get the list of all registered tasks.
187 * \param s The scheduler instance to get the task list from.
189 * \return The task list.
191 * Each entry of the list contains an identifier which is simply a hex number.
192 * The result is dynamically allocated and must be freed by the caller.
194 char *get_task_list(struct sched *s)
196 struct task *t, *tmp;
199 list_for_each_entry_safe(t, tmp, &s->pre_select_list, pre_select_node) {
201 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
205 list_for_each_entry_safe(t, tmp, &s->post_select_list, post_select_node) {
207 // if (t->pre_select)
209 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
213 //PARA_DEBUG_LOG("task list:\n%s", msg);
218 * Set the notification value of a task.
220 * \param t The task to notify.
221 * \param err A positive error code.
223 * Tasks which honor notifications are supposed to call \ref
224 * task_get_notification() in their post_select function and act on the
225 * returned notification value.
227 * If the scheduler detects during its pre_select loop that at least one task
228 * has been notified, the loop terminates, and the post_select methods of all
229 * taks are immediately called again.
231 * The notification for a task is reset after the call to its post_select
234 * \sa \ref task_get_notification().
236 void task_notify(struct task *t, int err)
239 if (t->notification == -err) /* ignore subsequent notifications */
241 PARA_INFO_LOG("notifying task %s: %s\n", t->status, para_strerror(err));
242 t->notification = -err;
246 * Return the notification value of a task.
248 * \param t The task to get the notification value from.
250 * \return The notification value. If this is negative, the task has been
251 * notified by another task. Tasks are supposed to check for notifications by
252 * calling this function from their post_select method.
254 * \sa \ref task_notify().
256 int task_get_notification(struct task *t)
258 return t->notification;
262 * Set the notification value of all tasks of a scheduler instance.
264 * \param s The scheduler instance whose tasks should be notified.
265 * \param err A positive error code.
267 * This simply iterates over all existing tasks of \a s and sets each
268 * task's notification value to \p -err.
270 void task_notify_all(struct sched *s, int err)
274 list_for_each_entry(t, &s->pre_select_list, pre_select_node)
276 list_for_each_entry(t, &s->post_select_list, post_select_node)
281 * Set the select timeout to the minimal possible value.
283 * \param s Pointer to the scheduler struct.
285 * This causes the next select() call to return immediately.
287 void sched_min_delay(struct sched *s)
289 s->select_timeout.tv_sec = s->select_timeout.tv_usec = 0;
293 * Impose an upper bound for the timeout of the next select() call.
295 * \param to Maximal allowed timeout.
296 * \param s Pointer to the scheduler struct.
298 * If the current scheduler timeout is already smaller than \a to, this
299 * function does nothing. Otherwise the timeout for the next select() call is
300 * set to the given value.
302 * \sa sched_request_timeout_ms().
304 void sched_request_timeout(struct timeval *to, struct sched *s)
306 if (tv_diff(&s->select_timeout, to, NULL) > 0)
307 s->select_timeout = *to;
311 * Force the next select() call to return before the given amount of milliseconds.
313 * \param ms The maximal allowed timeout in milliseconds.
314 * \param s Pointer to the scheduler struct.
316 * Like sched_request_timeout() this imposes an upper bound on the timeout
317 * value for the next select() call.
319 void sched_request_timeout_ms(long unsigned ms, struct sched *s)
323 sched_request_timeout(&tv, s);
327 * Force the next select() call to return before the given future time.
329 * \param barrier Absolute time before select() should return.
330 * \param s Pointer to the scheduler struct.
332 * \return If \a barrier is in the past, this function does nothing and returns
333 * zero. Otherwise it returns one.
335 * \sa sched_request_barrier_or_min_delay().
337 int sched_request_barrier(struct timeval *barrier, struct sched *s)
341 if (tv_diff(now, barrier, &diff) > 0)
343 sched_request_timeout(&diff, s);
348 * Force the next select() call to return before the given time.
350 * \param barrier Absolute time before select() should return.
351 * \param s Pointer to the scheduler struct.
353 * \return If \a barrier is in the past, this function requests a minimal
354 * timeout and returns zero. Otherwise it returns one.
356 * \sa sched_min_delay(), sched_request_barrier().
358 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s)
362 if (tv_diff(now, barrier, &diff) > 0) {
366 sched_request_timeout(&diff, s);