2 * Copyright (C) 2006-2009 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file sched.c Paraslash's scheduling functions. */
9 #include <dirent.h> /* readdir() */
21 static struct list_head pre_select_list, post_select_list;
22 static int initialized;
24 static struct timeval now_struct;
25 struct timeval *now = &now_struct;
28 * Remove a task from the scheduler.
30 * \param t The task to remove.
32 * If the pre_select pointer of \a t is not \p NULL, it is removed from
33 * the pre_select list of the scheduler. Same goes for \a post_select.
35 static void unregister_task(struct task *t)
39 PARA_INFO_LOG("unregistering %s (%s)\n", t->status,
40 t->error <0? para_strerror(-t->error) : "shutdown");
42 list_del(&t->pre_select_node);
44 list_del(&t->post_select_node);
45 t->error = -E_TASK_UNREGISTERED;
49 static void sched_preselect(struct sched *s)
52 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
53 if (t->error >= 0 && t->pre_select)
55 // PARA_INFO_LOG("%s \n", t->status);
59 * We have to check whether the list is empty because the call
60 * to ->pre_select() might have called sched_shutdown(). In
61 * this case t has been unregistered already, so we must not
62 * unregister it again.
64 if (list_empty(&pre_select_list))
70 static void sched_post_select(struct sched *s)
74 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
77 // PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
80 /* nec., see sched_preselect() */
81 if (list_empty(&post_select_list))
88 * The core function for all paraslash programs.
90 * \param s Pointer to the scheduler struct.
92 * This function updates the global \a now pointer, calls all registered
93 * pre_select hooks which may set the timeout and add any file descriptors to
94 * the fd sets of \a s. Next, it calls para_select() and makes the result available
95 * to the registered tasks by calling their post_select hook.
97 * \return Zero if no more tasks are left in either of the two lists, negative
98 * if para_select returned an error.
102 int schedule(struct sched *s)
107 return -E_NOT_INITIALIZED;
108 if (!s->select_function)
109 s->select_function = para_select;
110 gettimeofday(now, NULL);
114 s->timeout = s->default_timeout;
117 if (list_empty(&pre_select_list) && list_empty(&post_select_list))
119 ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds, &s->timeout);
122 gettimeofday(now, NULL);
123 sched_post_select(s);
124 if (list_empty(&pre_select_list) && list_empty(&post_select_list))
130 * Initialize the paraslash scheduler.
132 static void init_sched(void)
134 PARA_INFO_LOG("initializing scheduler\n");
135 INIT_LIST_HEAD(&pre_select_list);
136 INIT_LIST_HEAD(&post_select_list);
141 * Add a task to the scheduler.
143 * \param t the task to add
145 * If the pre_select pointer of \a t is not \p NULL, it is added to
146 * the pre_select list of the scheduler. Same goes for post_select.
148 * \sa task::pre_select, task::post_select
150 void register_task(struct task *t)
154 PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
156 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
157 list_add_tail(&t->pre_select_node, &pre_select_list);
159 if (t->post_select) {
160 PARA_DEBUG_LOG("post_select: %p\n", &t->post_select);
161 list_add_tail(&t->post_select_node, &post_select_list);
166 * Unregister all tasks.
168 * This will cause \a schedule() to return immediately because both the
169 * \a pre_select_list and the \a post_select_list are empty.
171 void sched_shutdown(void)
173 struct task *t, *tmp;
177 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node)
179 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node)
185 * Get the list of all registered tasks.
187 * \return The task list.
189 * Each entry of the list contains an identifier which is simply a hex number
190 * that may be used in \a kill_task() to terminate the task.
191 * The result ist dynamically allocated and must be freed by the caller.
193 char *get_task_list(void)
195 struct task *t, *tmp;
200 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
202 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
206 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
208 // if (t->pre_select)
210 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
214 //PARA_DEBUG_LOG("task list:\n%s", msg);
219 * Simulate an error for the given task.
221 * \param id The task identifier.
223 * Find the task identified by \a id, set the tasks' error value to
224 * \p -E_TASK_KILLED and unregister the task.
226 * \return Positive on success, negative on errors (e.g. if \a id does not
227 * correspond to a registered task).
229 int kill_task(char *id)
231 struct task *t, *tmp;
235 return -E_NOT_INITIALIZED;
236 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
237 sprintf(buf, "%p", t);
240 t->error = -E_TASK_KILLED;
243 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
244 sprintf(buf, "%p", t);
247 t->error = -E_TASK_KILLED;
250 return -E_NO_SUCH_TASK;