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 timeval now_struct;
24 struct timeval *now = &now_struct;
27 * Remove a task from the scheduler.
29 * \param t The task to remove.
31 * If the pre_select pointer of \a t is not \p NULL, it is removed from
32 * the pre_select list of the scheduler. Same goes for \a post_select.
34 static void unregister_task(struct task *t)
37 PARA_INFO_LOG("unregistering %s (%s)\n", t->status,
38 para_strerror(-t->error));
40 list_del(&t->pre_select_node);
42 list_del(&t->post_select_node);
45 static inline bool timeout_is_zero(struct sched *s)
47 struct timeval *tv = &s->select_timeout;
48 return tv->tv_sec == 0 && tv->tv_usec == 0;
51 static void sched_preselect(struct sched *s)
54 list_for_each_entry_safe(t, tmp, &s->pre_select_list, pre_select_node) {
62 if (timeout_is_zero(s))
67 //#define SCHED_DEBUG 1
68 static inline void call_post_select(struct sched *s, struct task *t)
73 struct timeval t1, t2, diff;
76 gettimeofday(&t1, NULL);
78 gettimeofday(&t2, NULL);
79 tv_diff(&t1, &t2, &diff);
82 PARA_WARNING_LOG("%s: post_select time: %lums\n",
87 static void sched_post_select(struct sched *s)
91 list_for_each_entry_safe(t, tmp, &s->post_select_list, post_select_node) {
93 call_post_select(s, t);
94 // PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
98 * We have to check whether the list is empty because the call
99 * to ->post_select() might have called sched_shutdown(). In
100 * this case t has been unregistered already, so we must not
101 * unregister it again.
103 if (list_empty(&s->post_select_list))
110 * The core function for all paraslash programs.
112 * \param s Pointer to the scheduler struct.
114 * This function updates the global \a now pointer, calls all registered
115 * pre_select hooks which may set the timeout and add any file descriptors to
116 * the fd sets of \a s. Next, it calls para_select() and makes the result available
117 * to the registered tasks by calling their post_select hook.
119 * \return Zero if no more tasks are left in either of the two lists, negative
120 * if para_select returned an error.
124 int schedule(struct sched *s)
128 if (!s->select_function)
129 s->select_function = para_select;
133 s->select_timeout = s->default_timeout;
135 gettimeofday(now, NULL);
137 if (list_empty(&s->pre_select_list) && list_empty(&s->post_select_list))
139 if (!timeout_is_zero(s)) {
140 ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds,
146 * APUE: Be careful not to check the descriptor sets on return
147 * unless the return value is greater than zero. The return
148 * state of the descriptor sets is implementation dependent if
149 * either a signal is caught or the timer expires.
154 gettimeofday(now, NULL);
159 sched_post_select(s);
160 if (list_empty(&s->pre_select_list) && list_empty(&s->post_select_list))
166 * Add a task to the scheduler.
168 * \param t The task to add.
169 * \param s The scheduler instance to add the task to.
171 * If the pre_select pointer of \a t is not \p NULL, it is added to
172 * the pre_select list of the scheduler. Same goes for post_select.
174 * \sa task::pre_select, task::post_select
176 void register_task(struct sched *s, struct task *t)
178 PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
179 if (!s->pre_select_list.next)
180 INIT_LIST_HEAD(&s->pre_select_list);
181 if (!s->post_select_list.next)
182 INIT_LIST_HEAD(&s->post_select_list);
184 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
185 list_add_tail(&t->pre_select_node, &s->pre_select_list);
187 if (t->post_select) {
188 PARA_DEBUG_LOG("post_select: %p\n", &t->post_select);
189 list_add_tail(&t->post_select_node, &s->post_select_list);
194 * Unregister all tasks.
196 * This will cause \a schedule() to return immediately because both the
197 * \a pre_select_list and the \a post_select_list are empty. This function
198 * must be called from the post_select (rather than the pre_select) method.
200 void sched_shutdown(struct sched *s)
202 struct task *t, *tmp;
204 list_for_each_entry_safe(t, tmp, &s->pre_select_list, pre_select_node) {
205 t->error = -E_SCHED_SHUTDOWN;
208 list_for_each_entry_safe(t, tmp, &s->post_select_list, post_select_node) {
209 t->error = -E_SCHED_SHUTDOWN;
215 * Get the list of all registered tasks.
217 * \return The task list.
219 * Each entry of the list contains an identifier which is simply a hex number.
220 * The result is dynamically allocated and must be freed by the caller.
222 char *get_task_list(struct sched *s)
224 struct task *t, *tmp;
227 list_for_each_entry_safe(t, tmp, &s->pre_select_list, pre_select_node) {
229 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
233 list_for_each_entry_safe(t, tmp, &s->post_select_list, post_select_node) {
235 // if (t->pre_select)
237 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
241 //PARA_DEBUG_LOG("task list:\n%s", msg);
246 * Set the select timeout to the minimal possible value.
248 * \param s Pointer to the scheduler struct.
250 * This causes the next select() call to return immediately.
252 void sched_min_delay(struct sched *s)
254 s->select_timeout.tv_sec = s->select_timeout.tv_usec = 0;
258 * Impose an upper bound for the timeout of the next select() call.
260 * \param to Maximal allowed timeout.
261 * \param s Pointer to the scheduler struct.
263 * If the current scheduler timeout is already smaller than \a to, this
264 * function does nothing. Otherwise the timeout for the next select() call is
265 * set to the given value.
267 * \sa sched_request_timeout_ms().
269 void sched_request_timeout(struct timeval *to, struct sched *s)
271 if (tv_diff(&s->select_timeout, to, NULL) > 0)
272 s->select_timeout = *to;
276 * Force the next select() call to return before the given amount of milliseconds.
278 * \param ms The maximal allowed timeout in milliseconds.
279 * \param s Pointer to the scheduler struct.
281 * Like sched_request_timeout() this imposes an upper bound on the timeout
282 * value for the next select() call.
284 void sched_request_timeout_ms(long unsigned ms, struct sched *s)
288 sched_request_timeout(&tv, s);
292 * Force the next select() call to return before the given future time.
294 * \param barrier Absolute time before select() should return.
295 * \param s Pointer to the scheduler struct.
297 * \return If \a barrier is in the past, this function does nothing and returns
298 * zero. Otherwise it returns one.
300 * \sa sched_request_barrier_or_min_delay().
302 int sched_request_barrier(struct timeval *barrier, struct sched *s)
306 if (tv_diff(now, barrier, &diff) > 0)
308 sched_request_timeout(&diff, s);
313 * Force the next select() call to return before the given time.
315 * \param barrier Absolute time before select() should return.
316 * \param s Pointer to the scheduler struct.
318 * \return If \a barrier is in the past, this function requests a minimal
319 * timeout and returns zero. Otherwise it returns one.
321 * \sa sched_min_delay(), sched_request_barrier().
323 int sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s)
327 if (tv_diff(now, barrier, &diff) > 0) {
331 sched_request_timeout(&diff, s);