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 static void unlink_and_free_task(struct task *t)
46 PARA_INFO_LOG("freeing task %s\n", t->status);
48 if (t->owned_by_sched)
52 //#define SCHED_DEBUG 1
53 static inline void call_post_select(struct sched *s, struct task *t)
56 t->error = t->post_select(s, t);
58 struct timeval t1, t2, diff;
61 clock_get_realtime(&t1);
62 t->error = t->post_select(s, t);
63 clock_get_realtime(&t2);
64 tv_diff(&t1, &t2, &diff);
67 PARA_WARNING_LOG("%s: post_select time: %lums\n",
72 static unsigned sched_post_select(struct sched *s)
75 unsigned num_running_tasks = 0;
77 list_for_each_entry_safe(t, tmp, &s->task_list, node) {
79 if (t->dead) /* task has been reaped */
80 unlink_and_free_task(t);
83 call_post_select(s, t);
86 if (!t->owned_by_sched)
91 return num_running_tasks;
95 * The core function of all paraslash programs.
97 * \param s Pointer to the scheduler struct.
99 * This function updates the global \a now pointer, calls all registered
100 * pre_select hooks which may set the timeout and add any file descriptors to
101 * the fd sets of \a s. Next, it calls para_select() and makes the result available
102 * to the registered tasks by calling their post_select hook.
104 * \return Zero if no more tasks are left in the task list, negative if the
105 * select function returned an error.
107 * \sa \ref task, \ref now.
109 int schedule(struct sched *s)
112 unsigned num_running_tasks;
114 if (!s->select_function)
115 s->select_function = para_select;
119 s->select_timeout = s->default_timeout;
121 clock_get_realtime(now);
123 ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds,
129 * APUE: Be careful not to check the descriptor sets on return
130 * unless the return value is greater than zero. The return
131 * state of the descriptor sets is implementation dependent if
132 * either a signal is caught or the timer expires.
137 clock_get_realtime(now);
138 num_running_tasks = sched_post_select(s);
139 if (num_running_tasks == 0)
145 * Obtain the error status of a task and deallocate its resources.
147 * \param tptr Identifies the task to reap.
149 * This function is similar to wait(2) in that it returns information about a
150 * terminated task and allows to release the resources associated with the
151 * task. Until this function is called, the terminated task remains in a zombie
154 * \return If \a tptr is \p NULL, or \a *tptr is \p NULL, the function does
155 * nothing and returns zero. Otherwise, it is checked whether the task
156 * identified by \a tptr is still running. If it is, the function returns zero
157 * and again, no action is taken. Otherwise the (negative) error code of the
158 * terminated task is returned and \a *tptr is set to \p NULL. The task will
159 * then be removed removed from the scheduler task list.
161 * \sa \ref sched_shutdown(), wait(2).
163 int task_reap(struct task **tptr)
172 if (!t->owned_by_sched)
176 if (t->dead) /* will be freed in sched_post_select() */
179 * With list_for_each_entry_safe() it is only safe to remove the
180 * _current_ list item. Since we are being called from the loop in
181 * schedule() via some task's ->post_select() function, freeing the
182 * given task here would result in use-after-free bugs in schedule().
183 * So we only set t->dead which tells schedule() to free the task in
184 * the next iteration of its loop.
192 * Deallocate all resources of all tasks of a scheduler instance.
194 * \param s The scheduler instance.
196 * This should only be called after \ref schedule() has returned.
198 void sched_shutdown(struct sched *s)
200 struct task *t, *tmp;
202 list_for_each_entry_safe(t, tmp, &s->task_list, node) {
204 /* The task list should contain only terminated tasks. */
205 PARA_WARNING_LOG("shutting down running task %s\n",
207 unlink_and_free_task(t);
212 * Add a task to the scheduler. Deprecated.
214 * \param t The task to add.
215 * \param s The scheduler instance to add the task to.
217 * \sa task::pre_select, task::post_select
219 void register_task(struct sched *s, struct task *t)
221 PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
222 assert(t->post_select);
224 t->owned_by_sched = false;
225 if (!s->task_list.next)
226 INIT_LIST_HEAD(&s->task_list);
227 list_add_tail(&t->node, &s->task_list);
231 * Add a task to the scheduler task list.
233 * \param info Task information supplied by the caller.
234 * \param s The scheduler instance.
236 * \return A pointer to a newly allocated task structure. It will be
237 * freed by sched_shutdown().
239 struct task *task_register(struct task_info *info, struct sched *s)
241 struct task *t = para_malloc(sizeof(*t));
243 assert(info->post_select);
245 if (!s->task_list.next)
246 INIT_LIST_HEAD(&s->task_list);
248 snprintf(t->status, sizeof(t->status) - 1, "%s", info->name);
249 t->status[sizeof(t->status) - 1] = '\0';
253 t->pre_select = info->pre_select;
254 t->post_select = info->post_select;
255 t->context = info->context;
256 t->owned_by_sched = true;
257 list_add_tail(&t->node, &s->task_list);
262 * Obtain the context pointer of a task.
264 * \param t Return this task's context pointer.
266 * \return A pointer to the memory location specified previously as \a
267 * task_info->context when the task was registered with \ref task_register().
269 void *task_context(struct task *t)
271 assert(t->owned_by_sched);
276 * Get the list of all registered tasks.
278 * \param s The scheduler instance to get the task list from.
280 * \return The task list.
282 * Each entry of the list contains an identifier which is simply a hex number.
283 * The result is dynamically allocated and must be freed by the caller.
285 char *get_task_list(struct sched *s)
287 struct task *t, *tmp;
290 list_for_each_entry_safe(t, tmp, &s->task_list, node) {
292 tmp_msg = make_message("%s%p\t%s\t%s\n", msg? msg : "", t,
293 t->error < 0? (t->dead? "dead" : "zombie") : "running",
302 * Set the notification value of a task.
304 * \param t The task to notify.
305 * \param err A positive error code.
307 * Tasks which honor notifications are supposed to call \ref
308 * task_get_notification() in their post_select function and act on the
309 * returned notification value.
311 * If the scheduler detects during its pre_select loop that at least one task
312 * has been notified, the loop terminates, and the post_select methods of all
313 * taks are immediately called again.
315 * The notification for a task is reset after the call to its post_select
318 * \sa \ref task_get_notification().
320 void task_notify(struct task *t, int err)
323 if (t->notification == -err) /* ignore subsequent notifications */
325 PARA_INFO_LOG("notifying task %s: %s\n", t->status, para_strerror(err));
326 t->notification = -err;
330 * Return the notification value of a task.
332 * \param t The task to get the notification value from.
334 * \return The notification value. If this is negative, the task has been
335 * notified by another task. Tasks are supposed to check for notifications by
336 * calling this function from their post_select method.
338 * \sa \ref task_notify().
340 int task_get_notification(const struct task *t)
342 return t->notification;
346 * Set the notification value of all tasks of a scheduler instance.
348 * \param s The scheduler instance whose tasks should be notified.
349 * \param err A positive error code.
351 * This simply iterates over all existing tasks of \a s and sets each
352 * task's notification value to \p -err.
354 void task_notify_all(struct sched *s, int err)
358 list_for_each_entry(t, &s->task_list, node)
363 * Set the select timeout to the minimal possible value.
365 * \param s Pointer to the scheduler struct.
367 * This causes the next select() call to return immediately.
369 void sched_min_delay(struct sched *s)
371 s->select_timeout.tv_sec = s->select_timeout.tv_usec = 0;
375 * Impose an upper bound for the timeout of the next select() call.
377 * \param to Maximal allowed timeout.
378 * \param s Pointer to the scheduler struct.
380 * If the current scheduler timeout is already smaller than \a to, this
381 * function does nothing. Otherwise the timeout for the next select() call is
382 * set to the given value.
384 * \sa sched_request_timeout_ms().
386 void sched_request_timeout(struct timeval *to, struct sched *s)
388 if (tv_diff(&s->select_timeout, to, NULL) > 0)
389 s->select_timeout = *to;
393 * Force the next select() call to return before the given amount of milliseconds.
395 * \param ms The maximal allowed timeout in milliseconds.
396 * \param s Pointer to the scheduler struct.
398 * Like sched_request_timeout() this imposes an upper bound on the timeout
399 * value for the next select() call.
401 void sched_request_timeout_ms(long unsigned ms, struct sched *s)
405 sched_request_timeout(&tv, s);
409 * Force the next select() call to return before the given future time.
411 * \param barrier Absolute time before select() should return.
412 * \param s Pointer to the scheduler struct.
414 * \return If \a barrier is in the past, this function does nothing and returns
415 * zero. Otherwise it returns one.
417 * \sa sched_request_barrier_or_min_delay().
419 int sched_request_barrier(struct timeval *barrier, struct sched *s)
423 if (tv_diff(now, barrier, &diff) > 0)
425 sched_request_timeout(&diff, s);
430 * Force the next select() call to return before the given time.
432 * \param barrier Absolute time before select() should return.
433 * \param s Pointer to the scheduler struct.
435 * \return If \a barrier is in the past, this function requests a minimal
436 * timeout and returns zero. Otherwise it returns one.
438 * \sa sched_min_delay(), sched_request_barrier().
440 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s)
444 if (tv_diff(now, barrier, &diff) > 0) {
448 sched_request_timeout(&diff, s);