2 * Copyright (C) 2006 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file sched.c Paraslash's scheduling functions. */
21 * The possible states of a task.
23 * In addition to the states listed here, a task may also enter zombie state.
24 * This happens when its ->post_select function returns negative, the ->status
25 * field is then set to this return value. Such tasks are not scheduled any
26 * more (i.e. ->pre_select() and ->post_select() are no longer called), but
27 * they stay on the scheduler task list until \ref task_reap() or
28 * \ref sched_shutdown() is called.
31 /** Task has been reaped and may be removed from the task list. */
33 /** Task is active. */
38 /** A copy of the task name supplied when the task was registered. */
40 /** Copied during task_register(). */
41 struct task_info info
;
42 /* TS_RUNNING, TS_DEAD, or zombie (negative value). */
44 /** Position of the task in the task list of the scheduler. */
45 struct list_head node
;
46 /** If less than zero, the task was notified by another task. */
50 static struct timeval now_struct
;
51 const struct timeval
*now
= &now_struct
;
53 static void sched_preselect(struct sched
*s
)
57 list_for_each_entry_safe(t
, tmp
, &s
->task_list
, node
) {
60 if (t
->notification
!= 0)
62 if (t
->info
.pre_select
)
63 t
->info
.pre_select(s
, t
->info
.context
);
67 static void unlink_and_free_task(struct task
*t
)
69 PARA_INFO_LOG("freeing task %s\n", t
->name
);
75 //#define SCHED_DEBUG 1
76 static inline void call_post_select(struct sched
*s
, struct task
*t
)
81 ret
= t
->info
.post_select(s
, t
->info
.context
);
83 struct timeval t1
, t2
, diff
;
86 clock_get_realtime(&t1
);
87 ret
= t
->info
.post_select(s
, t
->info
.context
);
88 clock_get_realtime(&t2
);
89 tv_diff(&t1
, &t2
, &diff
);
92 PARA_WARNING_LOG("%s: post_select time: %lums\n",
95 t
->status
= ret
< 0? ret
: TS_RUNNING
;
98 static unsigned sched_post_select(struct sched
*s
)
100 struct task
*t
, *tmp
;
101 unsigned num_running_tasks
= 0;
103 list_for_each_entry_safe(t
, tmp
, &s
->task_list
, node
) {
104 if (t
->status
== TS_DEAD
) /* task has been reaped */
105 unlink_and_free_task(t
);
106 else if (t
->status
== TS_RUNNING
) {
107 call_post_select(s
, t
); /* sets t->status */
109 if (t
->status
== TS_RUNNING
)
113 return num_running_tasks
;
117 * The core function of all paraslash programs.
119 * \param s Pointer to the scheduler struct.
121 * This function updates the global \a now pointer, calls all registered
122 * pre_select hooks which may set the timeout and add any file descriptors to
123 * the fd sets of \a s. Next, it calls para_select() and makes the result available
124 * to the registered tasks by calling their post_select hook.
126 * \return Zero if no more tasks are left in the task list, negative if the
127 * select function returned an error.
131 int schedule(struct sched
*s
)
134 unsigned num_running_tasks
;
136 if (!s
->select_function
)
137 s
->select_function
= para_select
;
141 s
->select_timeout
= s
->default_timeout
;
143 clock_get_realtime(&now_struct
);
145 ret
= s
->select_function(s
->max_fileno
+ 1, &s
->rfds
, &s
->wfds
,
151 * APUE: Be careful not to check the descriptor sets on return
152 * unless the return value is greater than zero. The return
153 * state of the descriptor sets is implementation dependent if
154 * either a signal is caught or the timer expires.
159 clock_get_realtime(&now_struct
);
160 num_running_tasks
= sched_post_select(s
);
161 if (num_running_tasks
== 0)
167 * Obtain the error status of a task and deallocate its resources.
169 * \param tptr Identifies the task to reap.
171 * This function is similar to wait(2) in that it returns information about a
172 * terminated task and allows to release the resources associated with the
173 * task. Until this function is called, the terminated task remains in a zombie
176 * \return If \a tptr is \p NULL, or \a *tptr is \p NULL, the function does
177 * nothing and returns zero. Otherwise, it is checked whether the task
178 * identified by \a tptr is still running. If it is, the function returns zero
179 * and again, no action is taken. Otherwise the (negative) error code of the
180 * terminated task is returned and \a *tptr is set to \p NULL. The task will
181 * then be removed removed from the scheduler task list.
183 * \sa \ref sched_shutdown(), wait(2).
185 int task_reap(struct task
**tptr
)
199 * With list_for_each_entry_safe() it is only safe to remove the
200 * _current_ list item. Since we are being called from the loop in
201 * schedule() via some task's ->post_select() function, freeing the
202 * given task here would result in use-after-free bugs in schedule().
203 * So we only set the task status to TS_DEAD which tells schedule() to
204 * free the task in the next iteration of its loop.
213 * Deallocate all resources of all tasks of a scheduler instance.
215 * \param s The scheduler instance.
217 * This should only be called after \ref schedule() has returned.
219 void sched_shutdown(struct sched
*s
)
221 struct task
*t
, *tmp
;
223 list_for_each_entry_safe(t
, tmp
, &s
->task_list
, node
) {
224 if (t
->status
== TS_RUNNING
)
225 /* The task list should contain only terminated tasks. */
226 PARA_WARNING_LOG("shutting down running task %s\n",
228 unlink_and_free_task(t
);
233 * Add a task to the scheduler task list.
235 * \param info Task information supplied by the caller.
236 * \param s The scheduler instance.
238 * \return A pointer to a newly allocated task structure. It will be
239 * freed by sched_shutdown().
241 struct task
*task_register(struct task_info
*info
, struct sched
*s
)
243 struct task
*t
= para_malloc(sizeof(*t
));
245 assert(info
->post_select
);
247 if (!s
->task_list
.next
)
248 INIT_LIST_HEAD(&s
->task_list
);
251 t
->name
= para_strdup(info
->name
);
253 t
->status
= TS_RUNNING
;
254 list_add_tail(&t
->node
, &s
->task_list
);
259 * Get the list of all registered tasks.
261 * \param s The scheduler instance to get the task list from.
263 * \return The task list.
265 * Each entry of the list contains an identifier which is simply a hex number.
266 * The result is dynamically allocated and must be freed by the caller.
268 char *get_task_list(struct sched
*s
)
270 struct task
*t
, *tmp
;
273 list_for_each_entry_safe(t
, tmp
, &s
->task_list
, node
) {
275 tmp_msg
= make_message("%s%p\t%s\t%s\n", msg
? msg
: "", t
,
276 t
->status
== TS_DEAD
? "dead" :
277 (t
->status
== TS_RUNNING
? "running" : "zombie"),
286 * Set the notification value of a task.
288 * \param t The task to notify.
289 * \param err A positive error code.
291 * Tasks which honor notifications are supposed to call \ref
292 * task_get_notification() in their post_select function and act on the
293 * returned notification value.
295 * If the scheduler detects during its pre_select loop that at least one task
296 * has been notified, the loop terminates, and the post_select methods of all
297 * taks are immediately called again.
299 * The notification for a task is reset after the call to its post_select
302 * \sa \ref task_get_notification().
304 void task_notify(struct task
*t
, int err
)
307 if (t
->notification
== -err
) /* ignore subsequent notifications */
309 PARA_INFO_LOG("notifying task %s: %s\n", t
->name
, para_strerror(err
));
310 t
->notification
= -err
;
314 * Return the notification value of a task.
316 * \param t The task to get the notification value from.
318 * \return The notification value. If this is negative, the task has been
319 * notified by another task. Tasks are supposed to check for notifications by
320 * calling this function from their post_select method.
322 * \sa \ref task_notify().
324 int task_get_notification(const struct task
*t
)
326 return t
->notification
;
330 * Return the status value of a task.
332 * \param t The task to get the status value from.
334 * \return Zero if task does not exist, one if task is running, negative error
335 * code if task has terminated.
337 int task_status(const struct task
*t
)
341 if (t
->status
== TS_DEAD
) /* pretend dead tasks don't exist */
343 if (t
->status
== TS_RUNNING
)
349 * Set the notification value of all tasks of a scheduler instance.
351 * \param s The scheduler instance whose tasks should be notified.
352 * \param err A positive error code.
354 * This simply iterates over all existing tasks of \a s and sets each
355 * task's notification value to \p -err.
357 void task_notify_all(struct sched
*s
, int err
)
361 list_for_each_entry(t
, &s
->task_list
, node
)
366 * Set the select timeout to the minimal possible value.
368 * \param s Pointer to the scheduler struct.
370 * This causes the next select() call to return immediately.
372 void sched_min_delay(struct sched
*s
)
374 s
->select_timeout
.tv_sec
= s
->select_timeout
.tv_usec
= 0;
378 * Impose an upper bound for the timeout of the next select() call.
380 * \param to Maximal allowed timeout.
381 * \param s Pointer to the scheduler struct.
383 * If the current scheduler timeout is already smaller than \a to, this
384 * function does nothing. Otherwise the timeout for the next select() call is
385 * set to the given value.
387 * \sa sched_request_timeout_ms().
389 void sched_request_timeout(struct timeval
*to
, struct sched
*s
)
391 if (tv_diff(&s
->select_timeout
, to
, NULL
) > 0)
392 s
->select_timeout
= *to
;
396 * Force the next select() call to return before the given amount of milliseconds.
398 * \param ms The maximal allowed timeout in milliseconds.
399 * \param s Pointer to the scheduler struct.
401 * Like sched_request_timeout() this imposes an upper bound on the timeout
402 * value for the next select() call.
404 void sched_request_timeout_ms(long unsigned ms
, struct sched
*s
)
408 sched_request_timeout(&tv
, s
);
412 * Force the next select() call to return before the given future time.
414 * \param barrier Absolute time before select() should return.
415 * \param s Pointer to the scheduler struct.
417 * \return If \a barrier is in the past, this function does nothing and returns
418 * zero. Otherwise it returns one.
420 * \sa sched_request_barrier_or_min_delay().
422 int sched_request_barrier(struct timeval
*barrier
, struct sched
*s
)
426 if (tv_diff(now
, barrier
, &diff
) > 0)
428 sched_request_timeout(&diff
, s
);
433 * Force the next select() call to return before the given time.
435 * \param barrier Absolute time before select() should return.
436 * \param s Pointer to the scheduler struct.
438 * \return If \a barrier is in the past, this function requests a minimal
439 * timeout and returns zero. Otherwise it returns one.
441 * \sa sched_min_delay(), sched_request_barrier().
443 int sched_request_barrier_or_min_delay(struct timeval
*barrier
, struct sched
*s
)
447 if (tv_diff(now
, barrier
, &diff
) > 0) {
451 sched_request_timeout(&diff
, s
);