2 * Copyright (C) 2006-2011 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file sched.c Paraslash's scheduling functions. */
23 static struct list_head pre_select_list, post_select_list;
24 static int initialized;
26 static struct timeval now_struct;
27 struct timeval *now = &now_struct;
30 * Remove a task from the scheduler.
32 * \param t The task to remove.
34 * If the pre_select pointer of \a t is not \p NULL, it is removed from
35 * the pre_select list of the scheduler. Same goes for \a post_select.
37 static void unregister_task(struct task *t)
42 PARA_INFO_LOG("unregistering %s (%s)\n", t->status,
43 para_strerror(-t->error));
45 list_del(&t->pre_select_node);
47 list_del(&t->post_select_node);
50 static inline bool timeout_is_zero(struct sched *s)
52 struct timeval *tv = &s->select_timeout;
53 return tv->tv_sec == 0 && tv->tv_usec == 0;
56 static void sched_preselect(struct sched *s)
59 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
67 if (timeout_is_zero(s))
72 //#define SCHED_DEBUG 1
73 static inline void call_post_select(struct sched *s, struct task *t)
78 struct timeval t1, t2, diff;
81 gettimeofday(&t1, NULL);
83 gettimeofday(&t2, NULL);
84 tv_diff(&t1, &t2, &diff);
87 PARA_WARNING_LOG("%s: post_select time: %lums\n",
92 static void sched_post_select(struct sched *s)
96 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
98 call_post_select(s, t);
99 // PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
103 * We have to check whether the list is empty because the call
104 * to ->post_select() might have called sched_shutdown(). In
105 * this case t has been unregistered already, so we must not
106 * unregister it again.
108 if (list_empty(&post_select_list))
115 * The core function for all paraslash programs.
117 * \param s Pointer to the scheduler struct.
119 * This function updates the global \a now pointer, calls all registered
120 * pre_select hooks which may set the timeout and add any file descriptors to
121 * the fd sets of \a s. Next, it calls para_select() and makes the result available
122 * to the registered tasks by calling their post_select hook.
124 * \return Zero if no more tasks are left in either of the two lists, negative
125 * if para_select returned an error.
129 int schedule(struct sched *s)
134 return -E_NOT_INITIALIZED;
135 if (!s->select_function)
136 s->select_function = para_select;
140 s->select_timeout = s->default_timeout;
142 gettimeofday(now, NULL);
144 if (list_empty(&pre_select_list) && list_empty(&post_select_list))
146 if (!timeout_is_zero(s)) {
147 ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds,
153 * APUE: Be careful not to check the descriptor sets on return
154 * unless the return value is greater than zero. The return
155 * state of the descriptor sets is implementation dependent if
156 * either a signal is caught or the timer expires.
161 gettimeofday(now, NULL);
166 sched_post_select(s);
167 if (list_empty(&pre_select_list) && list_empty(&post_select_list))
173 * Initialize the paraslash scheduler.
175 static void init_sched(void)
177 PARA_INFO_LOG("initializing scheduler\n");
178 INIT_LIST_HEAD(&pre_select_list);
179 INIT_LIST_HEAD(&post_select_list);
184 * Add a task to the scheduler.
186 * \param t the task to add
188 * If the pre_select pointer of \a t is not \p NULL, it is added to
189 * the pre_select list of the scheduler. Same goes for post_select.
191 * \sa task::pre_select, task::post_select
193 void register_task(struct task *t)
197 PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
199 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
200 list_add_tail(&t->pre_select_node, &pre_select_list);
202 if (t->post_select) {
203 PARA_DEBUG_LOG("post_select: %p\n", &t->post_select);
204 list_add_tail(&t->post_select_node, &post_select_list);
209 * Unregister all tasks.
211 * This will cause \a schedule() to return immediately because both the
212 * \a pre_select_list and the \a post_select_list are empty. This function
213 * must be called from the post_select (rather than the pre_select) method.
215 void sched_shutdown(void)
217 struct task *t, *tmp;
221 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
222 t->error = -E_SCHED_SHUTDOWN;
225 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
226 t->error = -E_SCHED_SHUTDOWN;
233 * Get the list of all registered tasks.
235 * \return The task list.
237 * Each entry of the list contains an identifier which is simply a hex number.
238 * The result is dynamically allocated and must be freed by the caller.
240 char *get_task_list(void)
242 struct task *t, *tmp;
247 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
249 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
253 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
255 // if (t->pre_select)
257 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
261 //PARA_DEBUG_LOG("task list:\n%s", msg);
266 * Set the select timeout to the minimal possible value.
268 * \param s Pointer to the scheduler struct.
270 * This causes the next select() call to return immediately.
272 void sched_min_delay(struct sched *s)
274 s->select_timeout.tv_sec = s->select_timeout.tv_usec = 0;
278 * Impose an upper bound for the timeout of the next select() call.
280 * \param to Maximal allowed timeout.
281 * \param s Pointer to the scheduler struct.
283 * If the current scheduler timeout is already smaller than \a to, this
284 * function does nothing. Otherwise the timeout for the next select() call is
285 * set to the given value.
287 * \sa sched_request_timeout_ms().
289 void sched_request_timeout(struct timeval *to, struct sched *s)
291 if (tv_diff(&s->select_timeout, to, NULL) > 0)
292 s->select_timeout = *to;
296 * Force the next select() call to return before the given amount of milliseconds.
298 * \param ms The maximal allowed timeout in milliseconds.
299 * \param s Pointer to the scheduler struct.
301 * Like sched_request_timeout() this imposes an upper bound on the timeout
302 * value for the next select() call.
304 void sched_request_timeout_ms(long unsigned ms, struct sched *s)
308 sched_request_timeout(&tv, s);
312 * Force the next select() call to return before the given future time.
314 * \param barrier Absolute time before select() should return.
315 * \param s Pointer to the scheduler struct.
317 * \return If \a barrier is in the past, this function does nothing and returns
318 * zero. Otherwise it returns one.
320 * \sa sched_request_barrier_or_min_delay().
322 int sched_request_barrier(struct timeval *barrier, struct sched *s)
326 if (tv_diff(now, barrier, &diff) > 0)
328 sched_request_timeout(&diff, s);
333 * Force the next select() call to return before the given time.
335 * \param barrier Absolute time before select() should return.
336 * \param s Pointer to the scheduler struct.
338 * \return If \a barrier is in the past, this function requests a minimal
339 * timeout and returns zero. Otherwise it returns one.
341 * \sa sched_min_delay(), sched_request_barrier().
343 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s)
347 if (tv_diff(now, barrier, &diff) > 0) {
351 sched_request_timeout(&diff, s);