2 * Copyright (C) 2006-2012 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file sched.c Paraslash's scheduling functions. */
22 static struct timeval now_struct
;
23 struct timeval
*now
= &now_struct
;
26 * Remove a task from the scheduler.
28 * \param t The task to remove.
30 * If the pre_select pointer of \a t is not \p NULL, it is removed from
31 * the pre_select list of the scheduler. Same goes for \a post_select.
33 static void unregister_task(struct task
*t
)
36 PARA_INFO_LOG("unregistering %s (%s)\n", t
->status
,
37 para_strerror(-t
->error
));
39 list_del(&t
->pre_select_node
);
41 list_del(&t
->post_select_node
);
44 static inline bool timeout_is_zero(struct sched
*s
)
46 struct timeval
*tv
= &s
->select_timeout
;
47 return tv
->tv_sec
== 0 && tv
->tv_usec
== 0;
50 static void sched_preselect(struct sched
*s
)
53 list_for_each_entry_safe(t
, tmp
, &s
->pre_select_list
, pre_select_node
) {
61 if (timeout_is_zero(s
))
66 //#define SCHED_DEBUG 1
67 static inline void call_post_select(struct sched
*s
, struct task
*t
)
72 struct timeval t1
, t2
, diff
;
75 gettimeofday(&t1
, NULL
);
77 gettimeofday(&t2
, NULL
);
78 tv_diff(&t1
, &t2
, &diff
);
81 PARA_WARNING_LOG("%s: post_select time: %lums\n",
86 static void sched_post_select(struct sched
*s
)
90 list_for_each_entry_safe(t
, tmp
, &s
->post_select_list
, post_select_node
) {
92 call_post_select(s
, t
);
93 // PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
97 * We have to check whether the list is empty because the call
98 * to ->post_select() might have called sched_shutdown(). In
99 * this case t has been unregistered already, so we must not
100 * unregister it again.
102 if (list_empty(&s
->post_select_list
))
109 * The core function for all paraslash programs.
111 * \param s Pointer to the scheduler struct.
113 * This function updates the global \a now pointer, calls all registered
114 * pre_select hooks which may set the timeout and add any file descriptors to
115 * the fd sets of \a s. Next, it calls para_select() and makes the result available
116 * to the registered tasks by calling their post_select hook.
118 * \return Zero if no more tasks are left in either of the two lists, negative
119 * if para_select returned an error.
123 int schedule(struct sched
*s
)
127 if (!s
->select_function
)
128 s
->select_function
= para_select
;
132 s
->select_timeout
= s
->default_timeout
;
134 gettimeofday(now
, NULL
);
136 if (list_empty(&s
->pre_select_list
) && list_empty(&s
->post_select_list
))
138 if (!timeout_is_zero(s
)) {
139 ret
= s
->select_function(s
->max_fileno
+ 1, &s
->rfds
, &s
->wfds
,
145 * APUE: Be careful not to check the descriptor sets on return
146 * unless the return value is greater than zero. The return
147 * state of the descriptor sets is implementation dependent if
148 * either a signal is caught or the timer expires.
153 gettimeofday(now
, NULL
);
158 sched_post_select(s
);
159 if (list_empty(&s
->pre_select_list
) && list_empty(&s
->post_select_list
))
165 * Add a task to the scheduler.
167 * \param t The task to add.
168 * \param s The scheduler instance to add the task to.
170 * If the pre_select pointer of \a t is not \p NULL, it is added to
171 * the pre_select list of the scheduler. Same goes for post_select.
173 * \sa task::pre_select, task::post_select
175 void register_task(struct sched
*s
, struct task
*t
)
177 PARA_INFO_LOG("registering %s (%p)\n", t
->status
, t
);
178 if (!s
->pre_select_list
.next
)
179 INIT_LIST_HEAD(&s
->pre_select_list
);
180 if (!s
->post_select_list
.next
)
181 INIT_LIST_HEAD(&s
->post_select_list
);
183 PARA_DEBUG_LOG("pre_select: %p\n", &t
->pre_select
);
184 list_add_tail(&t
->pre_select_node
, &s
->pre_select_list
);
186 if (t
->post_select
) {
187 PARA_DEBUG_LOG("post_select: %p\n", &t
->post_select
);
188 list_add_tail(&t
->post_select_node
, &s
->post_select_list
);
193 * Unregister all tasks.
195 * This will cause \a schedule() to return immediately because both the
196 * \a pre_select_list and the \a post_select_list are empty. This function
197 * must be called from the post_select (rather than the pre_select) method.
199 void sched_shutdown(struct sched
*s
)
201 struct task
*t
, *tmp
;
203 list_for_each_entry_safe(t
, tmp
, &s
->pre_select_list
, pre_select_node
) {
204 t
->error
= -E_SCHED_SHUTDOWN
;
207 list_for_each_entry_safe(t
, tmp
, &s
->post_select_list
, post_select_node
) {
208 t
->error
= -E_SCHED_SHUTDOWN
;
214 * Get the list of all registered tasks.
216 * \return The task list.
218 * Each entry of the list contains an identifier which is simply a hex number.
219 * The result is dynamically allocated and must be freed by the caller.
221 char *get_task_list(struct sched
*s
)
223 struct task
*t
, *tmp
;
226 list_for_each_entry_safe(t
, tmp
, &s
->pre_select_list
, pre_select_node
) {
228 tmp_msg
= make_message("%s%p\tpre\t%s\n", msg
? msg
: "", t
, t
->status
);
232 list_for_each_entry_safe(t
, tmp
, &s
->post_select_list
, post_select_node
) {
234 // if (t->pre_select)
236 tmp_msg
= make_message("%s%p\tpost\t%s\n", msg
? msg
: "", t
, t
->status
);
240 //PARA_DEBUG_LOG("task list:\n%s", msg);
245 * Set the select timeout to the minimal possible value.
247 * \param s Pointer to the scheduler struct.
249 * This causes the next select() call to return immediately.
251 void sched_min_delay(struct sched
*s
)
253 s
->select_timeout
.tv_sec
= s
->select_timeout
.tv_usec
= 0;
257 * Impose an upper bound for the timeout of the next select() call.
259 * \param to Maximal allowed timeout.
260 * \param s Pointer to the scheduler struct.
262 * If the current scheduler timeout is already smaller than \a to, this
263 * function does nothing. Otherwise the timeout for the next select() call is
264 * set to the given value.
266 * \sa sched_request_timeout_ms().
268 void sched_request_timeout(struct timeval
*to
, struct sched
*s
)
270 if (tv_diff(&s
->select_timeout
, to
, NULL
) > 0)
271 s
->select_timeout
= *to
;
275 * Force the next select() call to return before the given amount of milliseconds.
277 * \param ms The maximal allowed timeout in milliseconds.
278 * \param s Pointer to the scheduler struct.
280 * Like sched_request_timeout() this imposes an upper bound on the timeout
281 * value for the next select() call.
283 void sched_request_timeout_ms(long unsigned ms
, struct sched
*s
)
287 sched_request_timeout(&tv
, s
);
291 * Force the next select() call to return before the given future time.
293 * \param barrier Absolute time before select() should return.
294 * \param s Pointer to the scheduler struct.
296 * \return If \a barrier is in the past, this function does nothing and returns
297 * zero. Otherwise it returns one.
299 * \sa sched_request_barrier_or_min_delay().
301 int sched_request_barrier(struct timeval
*barrier
, struct sched
*s
)
305 if (tv_diff(now
, barrier
, &diff
) > 0)
307 sched_request_timeout(&diff
, s
);
312 * Force the next select() call to return before the given 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 requests a minimal
318 * timeout and returns zero. Otherwise it returns one.
320 * \sa sched_min_delay(), sched_request_barrier().
322 int sched_request_barrier_or_min_delay(struct timeval
*barrier
, struct sched
*s
)
326 if (tv_diff(now
, barrier
, &diff
) > 0) {
330 sched_request_timeout(&diff
, s
);