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 task list.
214 * \param info Task information supplied by the caller.
215 * \param s The scheduler instance.
217 * \return A pointer to a newly allocated task structure. It will be
218 * freed by sched_shutdown().
220 struct task *task_register(struct task_info *info, struct sched *s)
222 struct task *t = para_malloc(sizeof(*t));
224 assert(info->post_select);
226 if (!s->task_list.next)
227 INIT_LIST_HEAD(&s->task_list);
229 snprintf(t->status, sizeof(t->status) - 1, "%s", info->name);
230 t->status[sizeof(t->status) - 1] = '\0';
234 t->pre_select = info->pre_select;
235 t->post_select = info->post_select;
236 t->context = info->context;
237 t->owned_by_sched = true;
238 list_add_tail(&t->node, &s->task_list);
243 * Obtain the context pointer of a task.
245 * \param t Return this task's context pointer.
247 * \return A pointer to the memory location specified previously as \a
248 * task_info->context when the task was registered with \ref task_register().
250 void *task_context(struct task *t)
252 assert(t->owned_by_sched);
257 * Get the list of all registered tasks.
259 * \param s The scheduler instance to get the task list from.
261 * \return The task list.
263 * Each entry of the list contains an identifier which is simply a hex number.
264 * The result is dynamically allocated and must be freed by the caller.
266 char *get_task_list(struct sched *s)
268 struct task *t, *tmp;
271 list_for_each_entry_safe(t, tmp, &s->task_list, node) {
273 tmp_msg = make_message("%s%p\t%s\t%s\n", msg? msg : "", t,
274 t->error < 0? (t->dead? "dead" : "zombie") : "running",
283 * Set the notification value of a task.
285 * \param t The task to notify.
286 * \param err A positive error code.
288 * Tasks which honor notifications are supposed to call \ref
289 * task_get_notification() in their post_select function and act on the
290 * returned notification value.
292 * If the scheduler detects during its pre_select loop that at least one task
293 * has been notified, the loop terminates, and the post_select methods of all
294 * taks are immediately called again.
296 * The notification for a task is reset after the call to its post_select
299 * \sa \ref task_get_notification().
301 void task_notify(struct task *t, int err)
304 if (t->notification == -err) /* ignore subsequent notifications */
306 PARA_INFO_LOG("notifying task %s: %s\n", t->status, para_strerror(err));
307 t->notification = -err;
311 * Return the notification value of a task.
313 * \param t The task to get the notification value from.
315 * \return The notification value. If this is negative, the task has been
316 * notified by another task. Tasks are supposed to check for notifications by
317 * calling this function from their post_select method.
319 * \sa \ref task_notify().
321 int task_get_notification(const struct task *t)
323 return t->notification;
327 * Set the notification value of all tasks of a scheduler instance.
329 * \param s The scheduler instance whose tasks should be notified.
330 * \param err A positive error code.
332 * This simply iterates over all existing tasks of \a s and sets each
333 * task's notification value to \p -err.
335 void task_notify_all(struct sched *s, int err)
339 list_for_each_entry(t, &s->task_list, node)
344 * Set the select timeout to the minimal possible value.
346 * \param s Pointer to the scheduler struct.
348 * This causes the next select() call to return immediately.
350 void sched_min_delay(struct sched *s)
352 s->select_timeout.tv_sec = s->select_timeout.tv_usec = 0;
356 * Impose an upper bound for the timeout of the next select() call.
358 * \param to Maximal allowed timeout.
359 * \param s Pointer to the scheduler struct.
361 * If the current scheduler timeout is already smaller than \a to, this
362 * function does nothing. Otherwise the timeout for the next select() call is
363 * set to the given value.
365 * \sa sched_request_timeout_ms().
367 void sched_request_timeout(struct timeval *to, struct sched *s)
369 if (tv_diff(&s->select_timeout, to, NULL) > 0)
370 s->select_timeout = *to;
374 * Force the next select() call to return before the given amount of milliseconds.
376 * \param ms The maximal allowed timeout in milliseconds.
377 * \param s Pointer to the scheduler struct.
379 * Like sched_request_timeout() this imposes an upper bound on the timeout
380 * value for the next select() call.
382 void sched_request_timeout_ms(long unsigned ms, struct sched *s)
386 sched_request_timeout(&tv, s);
390 * Force the next select() call to return before the given future time.
392 * \param barrier Absolute time before select() should return.
393 * \param s Pointer to the scheduler struct.
395 * \return If \a barrier is in the past, this function does nothing and returns
396 * zero. Otherwise it returns one.
398 * \sa sched_request_barrier_or_min_delay().
400 int sched_request_barrier(struct timeval *barrier, struct sched *s)
404 if (tv_diff(now, barrier, &diff) > 0)
406 sched_request_timeout(&diff, s);
411 * Force the next select() call to return before the given time.
413 * \param barrier Absolute time before select() should return.
414 * \param s Pointer to the scheduler struct.
416 * \return If \a barrier is in the past, this function requests a minimal
417 * timeout and returns zero. Otherwise it returns one.
419 * \sa sched_min_delay(), sched_request_barrier().
421 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s)
425 if (tv_diff(now, barrier, &diff) > 0) {
429 sched_request_timeout(&diff, s);