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 task name supplied when the task was registered(). */
24 /** Copied from the task_info struct during task_register(). */
25 void (*pre_select)(struct sched *s, struct task *t);
26 /** Copied from the task_info struct during task_register(). */
27 int (*post_select)(struct sched *s, struct task *t);
28 /** Whether this task is active (>=0) or in error state (<0). */
30 /** Position of the task in the task list of the scheduler. */
31 struct list_head node;
32 /** If less than zero, the task was notified by another task. */
34 /** True if task is in error state and exit status has been queried. */
36 /** Usually a pointer to the struct containing this task. */
40 static struct timeval now_struct;
41 struct timeval *now = &now_struct;
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->task_list, node) {
56 if (t->notification != 0)
63 static void unlink_and_free_task(struct task *t)
65 PARA_INFO_LOG("freeing task %s\n", t->name);
71 //#define SCHED_DEBUG 1
72 static inline void call_post_select(struct sched *s, struct task *t)
75 t->status = t->post_select(s, t);
77 struct timeval t1, t2, diff;
80 clock_get_realtime(&t1);
81 t->status = t->post_select(s, t);
82 clock_get_realtime(&t2);
83 tv_diff(&t1, &t2, &diff);
86 PARA_WARNING_LOG("%s: post_select time: %lums\n",
91 static unsigned sched_post_select(struct sched *s)
94 unsigned num_running_tasks = 0;
96 list_for_each_entry_safe(t, tmp, &s->task_list, node) {
98 if (t->dead) /* task has been reaped */
99 unlink_and_free_task(t);
102 call_post_select(s, t);
107 return num_running_tasks;
111 * The core function of all paraslash programs.
113 * \param s Pointer to the scheduler struct.
115 * This function updates the global \a now pointer, calls all registered
116 * pre_select hooks which may set the timeout and add any file descriptors to
117 * the fd sets of \a s. Next, it calls para_select() and makes the result available
118 * to the registered tasks by calling their post_select hook.
120 * \return Zero if no more tasks are left in the task list, negative if the
121 * select function returned an error.
123 * \sa \ref task, \ref now.
125 int schedule(struct sched *s)
128 unsigned num_running_tasks;
130 if (!s->select_function)
131 s->select_function = para_select;
135 s->select_timeout = s->default_timeout;
137 clock_get_realtime(now);
139 ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds,
145 * APUE: Be careful not to check the descriptor sets on return
146 * unless the return value is greater than zero. The return
147 * state of the descriptor sets is implementation dependent if
148 * either a signal is caught or the timer expires.
153 clock_get_realtime(now);
154 num_running_tasks = sched_post_select(s);
155 if (num_running_tasks == 0)
161 * Obtain the error status of a task and deallocate its resources.
163 * \param tptr Identifies the task to reap.
165 * This function is similar to wait(2) in that it returns information about a
166 * terminated task and allows to release the resources associated with the
167 * task. Until this function is called, the terminated task remains in a zombie
170 * \return If \a tptr is \p NULL, or \a *tptr is \p NULL, the function does
171 * nothing and returns zero. Otherwise, it is checked whether the task
172 * identified by \a tptr is still running. If it is, the function returns zero
173 * and again, no action is taken. Otherwise the (negative) error code of the
174 * terminated task is returned and \a *tptr is set to \p NULL. The task will
175 * then be removed removed from the scheduler task list.
177 * \sa \ref sched_shutdown(), wait(2).
179 int task_reap(struct task **tptr)
190 if (t->dead) /* will be freed in sched_post_select() */
193 * With list_for_each_entry_safe() it is only safe to remove the
194 * _current_ list item. Since we are being called from the loop in
195 * schedule() via some task's ->post_select() function, freeing the
196 * given task here would result in use-after-free bugs in schedule().
197 * So we only set t->dead which tells schedule() to free the task in
198 * the next iteration of its loop.
206 * Deallocate all resources of all tasks of a scheduler instance.
208 * \param s The scheduler instance.
210 * This should only be called after \ref schedule() has returned.
212 void sched_shutdown(struct sched *s)
214 struct task *t, *tmp;
216 list_for_each_entry_safe(t, tmp, &s->task_list, node) {
218 /* The task list should contain only terminated tasks. */
219 PARA_WARNING_LOG("shutting down running task %s\n",
221 unlink_and_free_task(t);
226 * Add a task to the scheduler task list.
228 * \param info Task information supplied by the caller.
229 * \param s The scheduler instance.
231 * \return A pointer to a newly allocated task structure. It will be
232 * freed by sched_shutdown().
234 struct task *task_register(struct task_info *info, struct sched *s)
236 struct task *t = para_malloc(sizeof(*t));
238 assert(info->post_select);
240 if (!s->task_list.next)
241 INIT_LIST_HEAD(&s->task_list);
243 t->name = para_strdup(info->name);
247 t->pre_select = info->pre_select;
248 t->post_select = info->post_select;
249 t->context = info->context;
250 list_add_tail(&t->node, &s->task_list);
255 * Obtain the context pointer of a task.
257 * \param t Return this task's context pointer.
259 * \return A pointer to the memory location specified previously as \a
260 * task_info->context when the task was registered with \ref task_register().
262 void *task_context(struct task *t)
268 * Get the list of all registered tasks.
270 * \param s The scheduler instance to get the task list from.
272 * \return The task list.
274 * Each entry of the list contains an identifier which is simply a hex number.
275 * The result is dynamically allocated and must be freed by the caller.
277 char *get_task_list(struct sched *s)
279 struct task *t, *tmp;
282 list_for_each_entry_safe(t, tmp, &s->task_list, node) {
284 tmp_msg = make_message("%s%p\t%s\t%s\n", msg? msg : "", t,
285 t->status < 0? (t->dead? "dead" : "zombie") : "running",
294 * Set the notification value of a task.
296 * \param t The task to notify.
297 * \param err A positive error code.
299 * Tasks which honor notifications are supposed to call \ref
300 * task_get_notification() in their post_select function and act on the
301 * returned notification value.
303 * If the scheduler detects during its pre_select loop that at least one task
304 * has been notified, the loop terminates, and the post_select methods of all
305 * taks are immediately called again.
307 * The notification for a task is reset after the call to its post_select
310 * \sa \ref task_get_notification().
312 void task_notify(struct task *t, int err)
315 if (t->notification == -err) /* ignore subsequent notifications */
317 PARA_INFO_LOG("notifying task %s: %s\n", t->name, para_strerror(err));
318 t->notification = -err;
322 * Return the notification value of a task.
324 * \param t The task to get the notification value from.
326 * \return The notification value. If this is negative, the task has been
327 * notified by another task. Tasks are supposed to check for notifications by
328 * calling this function from their post_select method.
330 * \sa \ref task_notify().
332 int task_get_notification(const struct task *t)
334 return t->notification;
338 * Return the status value of a task.
340 * \param t The task to get the status value from.
342 * \return Zero if task does not exist, one if task is running, negative error
343 * code if task has terminated.
345 int task_status(const struct task *t)
357 * Set the notification value of all tasks of a scheduler instance.
359 * \param s The scheduler instance whose tasks should be notified.
360 * \param err A positive error code.
362 * This simply iterates over all existing tasks of \a s and sets each
363 * task's notification value to \p -err.
365 void task_notify_all(struct sched *s, int err)
369 list_for_each_entry(t, &s->task_list, node)
374 * Set the select timeout to the minimal possible value.
376 * \param s Pointer to the scheduler struct.
378 * This causes the next select() call to return immediately.
380 void sched_min_delay(struct sched *s)
382 s->select_timeout.tv_sec = s->select_timeout.tv_usec = 0;
386 * Impose an upper bound for the timeout of the next select() call.
388 * \param to Maximal allowed timeout.
389 * \param s Pointer to the scheduler struct.
391 * If the current scheduler timeout is already smaller than \a to, this
392 * function does nothing. Otherwise the timeout for the next select() call is
393 * set to the given value.
395 * \sa sched_request_timeout_ms().
397 void sched_request_timeout(struct timeval *to, struct sched *s)
399 if (tv_diff(&s->select_timeout, to, NULL) > 0)
400 s->select_timeout = *to;
404 * Force the next select() call to return before the given amount of milliseconds.
406 * \param ms The maximal allowed timeout in milliseconds.
407 * \param s Pointer to the scheduler struct.
409 * Like sched_request_timeout() this imposes an upper bound on the timeout
410 * value for the next select() call.
412 void sched_request_timeout_ms(long unsigned ms, struct sched *s)
416 sched_request_timeout(&tv, s);
420 * Force the next select() call to return before the given future time.
422 * \param barrier Absolute time before select() should return.
423 * \param s Pointer to the scheduler struct.
425 * \return If \a barrier is in the past, this function does nothing and returns
426 * zero. Otherwise it returns one.
428 * \sa sched_request_barrier_or_min_delay().
430 int sched_request_barrier(struct timeval *barrier, struct sched *s)
434 if (tv_diff(now, barrier, &diff) > 0)
436 sched_request_timeout(&diff, s);
441 * Force the next select() call to return before the given time.
443 * \param barrier Absolute time before select() should return.
444 * \param s Pointer to the scheduler struct.
446 * \return If \a barrier is in the past, this function requests a minimal
447 * timeout and returns zero. Otherwise it returns one.
449 * \sa sched_min_delay(), sched_request_barrier().
451 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s)
455 if (tv_diff(now, barrier, &diff) > 0) {
459 sched_request_timeout(&diff, s);