2 * Copyright (C) 2006-2010 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file sched.c Paraslash's scheduling functions. */
10 #include <dirent.h> /* readdir() */
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
)
41 PARA_INFO_LOG("unregistering %s (%s)\n", t
->status
,
42 t
->error
<0? para_strerror(-t
->error
) : "shutdown");
44 list_del(&t
->pre_select_node
);
46 list_del(&t
->post_select_node
);
47 t
->error
= -E_TASK_UNREGISTERED
;
51 static void sched_preselect(struct sched
*s
)
54 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
) {
55 if (t
->error
>= 0 && t
->pre_select
)
57 // PARA_INFO_LOG("%s \n", t->status);
61 * We have to check whether the list is empty because the call
62 * to ->pre_select() might have called sched_shutdown(). In
63 * this case t has been unregistered already, so we must not
64 * unregister it again.
66 if (list_empty(&pre_select_list
))
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);
102 /* nec., see sched_preselect() */
103 if (list_empty(&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
)
129 return -E_NOT_INITIALIZED
;
130 if (!s
->select_function
)
131 s
->select_function
= para_select
;
135 s
->timeout
= s
->default_timeout
;
137 gettimeofday(now
, NULL
);
139 if (list_empty(&pre_select_list
) && list_empty(&post_select_list
))
141 ret
= s
->select_function(s
->max_fileno
+ 1, &s
->rfds
, &s
->wfds
, &s
->timeout
);
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
);
155 sched_post_select(s
);
156 if (list_empty(&pre_select_list
) && list_empty(&post_select_list
))
162 * Initialize the paraslash scheduler.
164 static void init_sched(void)
166 PARA_INFO_LOG("initializing scheduler\n");
167 INIT_LIST_HEAD(&pre_select_list
);
168 INIT_LIST_HEAD(&post_select_list
);
173 * Add a task to the scheduler.
175 * \param t the task to add
177 * If the pre_select pointer of \a t is not \p NULL, it is added to
178 * the pre_select list of the scheduler. Same goes for post_select.
180 * \sa task::pre_select, task::post_select
182 void register_task(struct task
*t
)
186 PARA_INFO_LOG("registering %s (%p)\n", t
->status
, t
);
188 PARA_DEBUG_LOG("pre_select: %p\n", &t
->pre_select
);
189 list_add_tail(&t
->pre_select_node
, &pre_select_list
);
191 if (t
->post_select
) {
192 PARA_DEBUG_LOG("post_select: %p\n", &t
->post_select
);
193 list_add_tail(&t
->post_select_node
, &post_select_list
);
198 * Unregister all tasks.
200 * This will cause \a schedule() to return immediately because both the
201 * \a pre_select_list and the \a post_select_list are empty.
203 void sched_shutdown(void)
205 struct task
*t
, *tmp
;
209 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
)
211 list_for_each_entry_safe(t
, tmp
, &post_select_list
, post_select_node
)
217 * Get the list of all registered tasks.
219 * \return The task list.
221 * Each entry of the list contains an identifier which is simply a hex number
222 * that may be used in \a kill_task() to terminate the task.
223 * The result is dynamically allocated and must be freed by the caller.
225 char *get_task_list(void)
227 struct task
*t
, *tmp
;
232 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
) {
234 tmp_msg
= make_message("%s%p\tpre\t%s\n", msg
? msg
: "", t
, t
->status
);
238 list_for_each_entry_safe(t
, tmp
, &post_select_list
, post_select_node
) {
240 // if (t->pre_select)
242 tmp_msg
= make_message("%s%p\tpost\t%s\n", msg
? msg
: "", t
, t
->status
);
246 //PARA_DEBUG_LOG("task list:\n%s", msg);
251 * Simulate an error for the given task.
253 * \param id The task identifier.
255 * Find the task identified by \a id, set the tasks' error value to
256 * \p -E_TASK_KILLED and unregister the task.
258 * \return Positive on success, negative on errors (e.g. if \a id does not
259 * correspond to a registered task).
261 int kill_task(char *id
)
263 struct task
*t
, *tmp
;
267 return -E_NOT_INITIALIZED
;
268 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
) {
269 sprintf(buf
, "%p", t
);
272 t
->error
= -E_TASK_KILLED
;
275 list_for_each_entry_safe(t
, tmp
, &post_select_list
, post_select_node
) {
276 sprintf(buf
, "%p", t
);
279 t
->error
= -E_TASK_KILLED
;
282 return -E_NO_SUCH_TASK
;
286 * Set the select timeout to the minimal possible value.
288 * \param s Pointer to the scheduler struct.
290 * This causes the next select() call to return immediately.
292 void sched_min_delay(struct sched
*s
)
294 s
->timeout
.tv_sec
= 0;
295 s
->timeout
.tv_usec
= 1;
299 * Impose an upper bound for the timeout of the next select() call.
301 * \param timeout Maximal allowed timeout.
302 * \param s Pointer to the scheduler struct.
304 * If the current scheduler timeout is already smaller than \a timeout, this
305 * function does nothing. Otherwise the timeout for the next select() call is
306 * set to the given value.
308 * \sa sched_request_timeout_ms().
310 void sched_request_timeout(struct timeval
*timeout
, struct sched
*s
)
312 if (tv_diff(&s
->timeout
, timeout
, NULL
) > 0)
313 s
->timeout
= *timeout
;
317 * Force the next select() call to return before the given amount of milliseconds.
319 * \param ms The maximal allowed timeout in milliseconds.
320 * \param s Pointer to the scheduler struct.
322 * Like sched_request_timeout() this imposes an upper bound on the timeout
323 * value for the next select() call.
325 void sched_request_timeout_ms(long unsigned ms
, struct sched
*s
)
329 sched_request_timeout(&tv
, s
);
333 * Force the next select() call to return before the given future time.
335 * \param barrier Absolute time before select() should return.
336 * \param s Pointer to the scheduler struct.
338 * If \a barrier is in the past, this function does nothing.
340 * \sa sched_request_barrier_or_min_delay().
342 void sched_request_barrier(struct timeval
*barrier
, struct sched
*s
)
346 if (tv_diff(now
, barrier
, &diff
) > 0)
348 sched_request_timeout(&diff
, s
);
352 * Force the next select() call to return before the given time.
354 * \param barrier Absolute time before select() should return.
355 * \param s Pointer to the scheduler struct.
357 * If \a barrier is in the past, this function requests a minimal timeout.
359 * \sa sched_min_delay(), sched_request_barrier().
361 void sched_request_barrier_or_min_delay(struct timeval
*barrier
, struct sched
*s
)
365 if (tv_diff(now
, barrier
, &diff
) > 0)
366 return sched_min_delay(s
);
367 sched_request_timeout(&diff
, s
);