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. */
10 #include <dirent.h> /* readdir() */
22 static struct list_head pre_select_list, post_select_list;
23 static int initialized;
25 static struct timeval now_struct;
26 struct timeval *now = &now_struct;
29 * Remove a task from the scheduler.
31 * \param t The task to remove.
33 * If the pre_select pointer of \a t is not \p NULL, it is removed from
34 * the pre_select list of the scheduler. Same goes for \a post_select.
36 static void unregister_task(struct task *t)
40 PARA_INFO_LOG("unregistering %s (%s)\n", t->status,
41 t->error <0? para_strerror(-t->error) : "shutdown");
43 list_del(&t->pre_select_node);
45 list_del(&t->post_select_node);
46 t->error = -E_TASK_UNREGISTERED;
50 static void sched_preselect(struct sched *s)
53 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
54 if (t->error >= 0 && t->pre_select)
56 // PARA_INFO_LOG("%s \n", t->status);
60 * We have to check whether the list is empty because the call
61 * to ->pre_select() might have called sched_shutdown(). In
62 * this case t has been unregistered already, so we must not
63 * unregister it again.
65 if (list_empty(&pre_select_list))
71 static void sched_post_select(struct sched *s)
75 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
78 // PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
81 /* nec., see sched_preselect() */
82 if (list_empty(&post_select_list))
89 * The core function for all paraslash programs.
91 * \param s Pointer to the scheduler struct.
93 * This function updates the global \a now pointer, calls all registered
94 * pre_select hooks which may set the timeout and add any file descriptors to
95 * the fd sets of \a s. Next, it calls para_select() and makes the result available
96 * to the registered tasks by calling their post_select hook.
98 * \return Zero if no more tasks are left in either of the two lists, negative
99 * if para_select returned an error.
103 int schedule(struct sched *s)
108 return -E_NOT_INITIALIZED;
109 if (!s->select_function)
110 s->select_function = para_select;
111 gettimeofday(now, NULL);
115 s->timeout = s->default_timeout;
118 if (list_empty(&pre_select_list) && list_empty(&post_select_list))
120 ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds, &s->timeout);
123 gettimeofday(now, NULL);
124 sched_post_select(s);
125 if (list_empty(&pre_select_list) && list_empty(&post_select_list))
131 * Initialize the paraslash scheduler.
133 static void init_sched(void)
135 PARA_INFO_LOG("initializing scheduler\n");
136 INIT_LIST_HEAD(&pre_select_list);
137 INIT_LIST_HEAD(&post_select_list);
142 * Add a task to the scheduler.
144 * \param t the task to add
146 * If the pre_select pointer of \a t is not \p NULL, it is added to
147 * the pre_select list of the scheduler. Same goes for post_select.
149 * \sa task::pre_select, task::post_select
151 void register_task(struct task *t)
155 PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
157 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
158 list_add_tail(&t->pre_select_node, &pre_select_list);
160 if (t->post_select) {
161 PARA_DEBUG_LOG("post_select: %p\n", &t->post_select);
162 list_add_tail(&t->post_select_node, &post_select_list);
167 * Unregister all tasks.
169 * This will cause \a schedule() to return immediately because both the
170 * \a pre_select_list and the \a post_select_list are empty.
172 void sched_shutdown(void)
174 struct task *t, *tmp;
178 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node)
180 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node)
186 * Get the list of all registered tasks.
188 * \return The task list.
190 * Each entry of the list contains an identifier which is simply a hex number
191 * that may be used in \a kill_task() to terminate the task.
192 * The result ist dynamically allocated and must be freed by the caller.
194 char *get_task_list(void)
196 struct task *t, *tmp;
201 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
203 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
207 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
209 // if (t->pre_select)
211 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
215 //PARA_DEBUG_LOG("task list:\n%s", msg);
220 * Simulate an error for the given task.
222 * \param id The task identifier.
224 * Find the task identified by \a id, set the tasks' error value to
225 * \p -E_TASK_KILLED and unregister the task.
227 * \return Positive on success, negative on errors (e.g. if \a id does not
228 * correspond to a registered task).
230 int kill_task(char *id)
232 struct task *t, *tmp;
236 return -E_NOT_INITIALIZED;
237 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
238 sprintf(buf, "%p", t);
241 t->error = -E_TASK_KILLED;
244 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
245 sprintf(buf, "%p", t);
248 t->error = -E_TASK_KILLED;
251 return -E_NO_SUCH_TASK;