2 * Copyright (C) 2006-2008 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 void unregister_task(struct task *t)
39 PARA_INFO_LOG("unregistering %s (%p)\n", t->status, t);
41 list_del(&t->pre_select_node);
43 list_del(&t->post_select_node);
47 static void sched_preselect(struct sched *s)
50 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
52 // PARA_INFO_LOG("%s \n", t->status);
59 static void sched_post_select(struct sched *s)
63 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
65 // PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
73 * the core function for all paraslash programs
75 * \param s pointer to the scheduler struct
77 * This function updates the global \a now pointer, calls all registered
78 * pre_select hooks which may set the timeout and add any file descriptors to
79 * the fd sets of \a s. Next, it calls para_select() and makes the result available
80 * to the registered tasks by calling their post_select hook.
82 * \return Zero if no more tasks are left in either of the two lists, negative
83 * if para_select returned an error.
87 int schedule(struct sched *s)
92 return -E_NOT_INITIALIZED;
93 gettimeofday(now, NULL);
97 s->timeout = s->default_timeout;
100 ret = para_select(s->max_fileno + 1, &s->rfds, &s->wfds, &s->timeout);
103 gettimeofday(now, NULL);
104 sched_post_select(s);
105 if (list_empty(&pre_select_list) && list_empty(&post_select_list))
111 * initialize the paraslash scheduler
113 static void init_sched(void)
115 PARA_INFO_LOG("initializing scheduler\n");
116 INIT_LIST_HEAD(&pre_select_list);
117 INIT_LIST_HEAD(&post_select_list);
122 * add a task to the scheduler
124 * \param t the task to add
126 * If the pre_select pointer of \a t is not \p NULL, it is added to
127 * the pre_select list of the scheduler. Same goes for post_select.
129 * \sa task::pre_select, task::post_select
131 void register_task(struct task *t)
135 PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
137 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
138 list_add_tail(&t->pre_select_node, &pre_select_list);
140 if (t->post_select) {
141 PARA_DEBUG_LOG("post_select: %p\n", &t->pre_select);
142 list_add_tail(&t->post_select_node, &post_select_list);
147 * unregister all tasks
149 * This will cause \a schedule() to return immediately because both the
150 * \a pre_select_list and the \a post_select_list are empty.
152 void sched_shutdown(void)
154 struct task *t, *tmp;
158 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node)
160 /* remove tasks which do not have a pre_select hook */
161 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node)
167 * get the list of all registered tasks.
169 * \return the task list
171 * Each entry of the list contains an identifier which is simply a hex number
172 * that may be used in \a kill_task() to terminate the task.
173 * The result ist dynamically allocated and must be freed by the caller.
175 char *get_task_list(void)
177 struct task *t, *tmp;
182 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
184 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
188 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
190 // if (t->pre_select)
192 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
196 //PARA_DEBUG_LOG("task list:\n%s", msg);
201 * simulate an error for the given task
203 * \param id the task identifier
205 * Find the task identified by \a id, set the tasks' error value to
206 * \p -E_TASK_KILLED and unregister the task.
208 * \return Positive on success, negative on errors (e.g. if \a id does not
209 * correspond to a registered task).
211 int kill_task(char *id)
213 struct task *t, *tmp;
217 return -E_NOT_INITIALIZED;
218 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
219 sprintf(buf, "%p", t);
222 t->error = -E_TASK_KILLED;
226 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
227 sprintf(buf, "%p", t);
230 t->error = -E_TASK_KILLED;
234 return -E_NO_SUCH_TASK;