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. */
22 * The possible states of a task.
24 * In addition to the states listed here, a task may also enter zombie state.
25 * This happens when its ->post_select function returns negative, the ->status
26 * field is then set to this return value. Such tasks are not scheduled any
27 * more (i.e. ->pre_select() and ->post_select() are no longer called), but
28 * they stay on the scheduler task list until \ref task_reap() or
29 * \ref sched_shutdown() is called.
32 /** Task has been reaped and may be removed from the task list. */
34 /** Task is active. */
39 /** A copy of the task name supplied when the task was registered. */
41 /** Copied during task_register(). */
42 struct task_info info;
43 /* TS_RUNNING, TS_DEAD, or zombie (negative value). */
45 /** Position of the task in the task list of the scheduler. */
46 struct list_head node;
47 /** If less than zero, the task was notified by another task. */
51 static struct timeval now_struct;
52 const struct timeval *now = &now_struct;
54 static inline bool timeout_is_zero(struct sched *s)
56 struct timeval *tv = &s->select_timeout;
57 return tv->tv_sec == 0 && tv->tv_usec == 0;
60 static void sched_preselect(struct sched *s)
64 list_for_each_entry_safe(t, tmp, &s->task_list, node) {
67 if (t->notification != 0)
69 if (t->info.pre_select)
70 t->info.pre_select(s, t->info.context);
74 static void unlink_and_free_task(struct task *t)
76 PARA_INFO_LOG("freeing task %s\n", t->name);
82 //#define SCHED_DEBUG 1
83 static inline void call_post_select(struct sched *s, struct task *t)
88 ret = t->info.post_select(s, t->info.context);
90 struct timeval t1, t2, diff;
93 clock_get_realtime(&t1);
94 ret = t->info.post_select(s, t->info.context);
95 clock_get_realtime(&t2);
96 tv_diff(&t1, &t2, &diff);
99 PARA_WARNING_LOG("%s: post_select time: %lums\n",
102 t->status = ret < 0? ret : TS_RUNNING;
105 static unsigned sched_post_select(struct sched *s)
107 struct task *t, *tmp;
108 unsigned num_running_tasks = 0;
110 list_for_each_entry_safe(t, tmp, &s->task_list, node) {
111 if (t->status == TS_DEAD) /* task has been reaped */
112 unlink_and_free_task(t);
113 else if (t->status == TS_RUNNING) {
114 call_post_select(s, t); /* sets t->status */
116 if (t->status == TS_RUNNING)
120 return num_running_tasks;
124 * The core function of all paraslash programs.
126 * \param s Pointer to the scheduler struct.
128 * This function updates the global \a now pointer, calls all registered
129 * pre_select hooks which may set the timeout and add any file descriptors to
130 * the fd sets of \a s. Next, it calls para_select() and makes the result available
131 * to the registered tasks by calling their post_select hook.
133 * \return Zero if no more tasks are left in the task list, negative if the
134 * select function returned an error.
138 int schedule(struct sched *s)
141 unsigned num_running_tasks;
143 if (!s->select_function)
144 s->select_function = para_select;
148 s->select_timeout = s->default_timeout;
150 clock_get_realtime(&now_struct);
152 ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds,
158 * APUE: Be careful not to check the descriptor sets on return
159 * unless the return value is greater than zero. The return
160 * state of the descriptor sets is implementation dependent if
161 * either a signal is caught or the timer expires.
166 clock_get_realtime(&now_struct);
167 num_running_tasks = sched_post_select(s);
168 if (num_running_tasks == 0)
174 * Obtain the error status of a task and deallocate its resources.
176 * \param tptr Identifies the task to reap.
178 * This function is similar to wait(2) in that it returns information about a
179 * terminated task and allows to release the resources associated with the
180 * task. Until this function is called, the terminated task remains in a zombie
183 * \return If \a tptr is \p NULL, or \a *tptr is \p NULL, the function does
184 * nothing and returns zero. Otherwise, it is checked whether the task
185 * identified by \a tptr is still running. If it is, the function returns zero
186 * and again, no action is taken. Otherwise the (negative) error code of the
187 * terminated task is returned and \a *tptr is set to \p NULL. The task will
188 * then be removed removed from the scheduler task list.
190 * \sa \ref sched_shutdown(), wait(2).
192 int task_reap(struct task **tptr)
206 * With list_for_each_entry_safe() it is only safe to remove the
207 * _current_ list item. Since we are being called from the loop in
208 * schedule() via some task's ->post_select() function, freeing the
209 * given task here would result in use-after-free bugs in schedule().
210 * So we only set the task status to TS_DEAD which tells schedule() to
211 * free the task in the next iteration of its loop.
220 * Deallocate all resources of all tasks of a scheduler instance.
222 * \param s The scheduler instance.
224 * This should only be called after \ref schedule() has returned.
226 void sched_shutdown(struct sched *s)
228 struct task *t, *tmp;
230 list_for_each_entry_safe(t, tmp, &s->task_list, node) {
231 if (t->status == TS_RUNNING)
232 /* The task list should contain only terminated tasks. */
233 PARA_WARNING_LOG("shutting down running task %s\n",
235 unlink_and_free_task(t);
240 * Add a task to the scheduler task list.
242 * \param info Task information supplied by the caller.
243 * \param s The scheduler instance.
245 * \return A pointer to a newly allocated task structure. It will be
246 * freed by sched_shutdown().
248 struct task *task_register(struct task_info *info, struct sched *s)
250 struct task *t = para_malloc(sizeof(*t));
252 assert(info->post_select);
254 if (!s->task_list.next)
255 INIT_LIST_HEAD(&s->task_list);
258 t->name = para_strdup(info->name);
260 t->status = TS_RUNNING;
261 list_add_tail(&t->node, &s->task_list);
266 * Get the list of all registered tasks.
268 * \param s The scheduler instance to get the task list from.
270 * \return The task list.
272 * Each entry of the list contains an identifier which is simply a hex number.
273 * The result is dynamically allocated and must be freed by the caller.
275 char *get_task_list(struct sched *s)
277 struct task *t, *tmp;
280 list_for_each_entry_safe(t, tmp, &s->task_list, node) {
282 tmp_msg = make_message("%s%p\t%s\t%s\n", msg? msg : "", t,
283 t->status == TS_DEAD? "dead" :
284 (t->status == TS_RUNNING? "running" : "zombie"),
293 * Set the notification value of a task.
295 * \param t The task to notify.
296 * \param err A positive error code.
298 * Tasks which honor notifications are supposed to call \ref
299 * task_get_notification() in their post_select function and act on the
300 * returned notification value.
302 * If the scheduler detects during its pre_select loop that at least one task
303 * has been notified, the loop terminates, and the post_select methods of all
304 * taks are immediately called again.
306 * The notification for a task is reset after the call to its post_select
309 * \sa \ref task_get_notification().
311 void task_notify(struct task *t, int err)
314 if (t->notification == -err) /* ignore subsequent notifications */
316 PARA_INFO_LOG("notifying task %s: %s\n", t->name, para_strerror(err));
317 t->notification = -err;
321 * Return the notification value of a task.
323 * \param t The task to get the notification value from.
325 * \return The notification value. If this is negative, the task has been
326 * notified by another task. Tasks are supposed to check for notifications by
327 * calling this function from their post_select method.
329 * \sa \ref task_notify().
331 int task_get_notification(const struct task *t)
333 return t->notification;
337 * Return the status value of a task.
339 * \param t The task to get the status value from.
341 * \return Zero if task does not exist, one if task is running, negative error
342 * code if task has terminated.
344 int task_status(const struct task *t)
348 if (t->status == TS_DEAD) /* pretend dead tasks don't exist */
350 if (t->status == TS_RUNNING)
356 * Set the notification value of all tasks of a scheduler instance.
358 * \param s The scheduler instance whose tasks should be notified.
359 * \param err A positive error code.
361 * This simply iterates over all existing tasks of \a s and sets each
362 * task's notification value to \p -err.
364 void task_notify_all(struct sched *s, int err)
368 list_for_each_entry(t, &s->task_list, node)
373 * Set the select timeout to the minimal possible value.
375 * \param s Pointer to the scheduler struct.
377 * This causes the next select() call to return immediately.
379 void sched_min_delay(struct sched *s)
381 s->select_timeout.tv_sec = s->select_timeout.tv_usec = 0;
385 * Impose an upper bound for the timeout of the next select() call.
387 * \param to Maximal allowed timeout.
388 * \param s Pointer to the scheduler struct.
390 * If the current scheduler timeout is already smaller than \a to, this
391 * function does nothing. Otherwise the timeout for the next select() call is
392 * set to the given value.
394 * \sa sched_request_timeout_ms().
396 void sched_request_timeout(struct timeval *to, struct sched *s)
398 if (tv_diff(&s->select_timeout, to, NULL) > 0)
399 s->select_timeout = *to;
403 * Force the next select() call to return before the given amount of milliseconds.
405 * \param ms The maximal allowed timeout in milliseconds.
406 * \param s Pointer to the scheduler struct.
408 * Like sched_request_timeout() this imposes an upper bound on the timeout
409 * value for the next select() call.
411 void sched_request_timeout_ms(long unsigned ms, struct sched *s)
415 sched_request_timeout(&tv, s);
419 * Force the next select() call to return before the given future time.
421 * \param barrier Absolute time before select() should return.
422 * \param s Pointer to the scheduler struct.
424 * \return If \a barrier is in the past, this function does nothing and returns
425 * zero. Otherwise it returns one.
427 * \sa sched_request_barrier_or_min_delay().
429 int sched_request_barrier(struct timeval *barrier, struct sched *s)
433 if (tv_diff(now, barrier, &diff) > 0)
435 sched_request_timeout(&diff, s);
440 * Force the next select() call to return before the given time.
442 * \param barrier Absolute time before select() should return.
443 * \param s Pointer to the scheduler struct.
445 * \return If \a barrier is in the past, this function requests a minimal
446 * timeout and returns zero. Otherwise it returns one.
448 * \sa sched_min_delay(), sched_request_barrier().
450 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s)
454 if (tv_diff(now, barrier, &diff) > 0) {
458 sched_request_timeout(&diff, s);