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
);
44 t
->error
= -E_TASK_UNREGISTERED
;
48 static void sched_preselect(struct sched
*s
)
51 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
) {
54 // PARA_INFO_LOG("%s \n", t->status);
58 * We have to check whether the list is empty because the call
59 * to ->pre_select() might have called sched_shutdown(). In
60 * this case t has been unregistered already, so we must not
61 * unregister it again.
63 if (list_empty(&pre_select_list
))
69 static void sched_post_select(struct sched
*s
)
73 list_for_each_entry_safe(t
, tmp
, &post_select_list
, post_select_node
) {
76 // PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
79 /* nec., see sched_preselect() */
80 if (list_empty(&post_select_list
))
87 * The core function for all paraslash programs.
89 * \param s Pointer to the scheduler struct.
91 * This function updates the global \a now pointer, calls all registered
92 * pre_select hooks which may set the timeout and add any file descriptors to
93 * the fd sets of \a s. Next, it calls para_select() and makes the result available
94 * to the registered tasks by calling their post_select hook.
96 * \return Zero if no more tasks are left in either of the two lists, negative
97 * if para_select returned an error.
101 int schedule(struct sched
*s
)
106 return -E_NOT_INITIALIZED
;
107 gettimeofday(now
, NULL
);
111 s
->timeout
= s
->default_timeout
;
114 if (list_empty(&pre_select_list
) && list_empty(&post_select_list
))
116 ret
= para_select(s
->max_fileno
+ 1, &s
->rfds
, &s
->wfds
, &s
->timeout
);
119 gettimeofday(now
, NULL
);
120 sched_post_select(s
);
121 if (list_empty(&pre_select_list
) && list_empty(&post_select_list
))
127 * Initialize the paraslash scheduler.
129 static void init_sched(void)
131 PARA_INFO_LOG("initializing scheduler\n");
132 INIT_LIST_HEAD(&pre_select_list
);
133 INIT_LIST_HEAD(&post_select_list
);
138 * Add a task to the scheduler.
140 * \param t the task to add
142 * If the pre_select pointer of \a t is not \p NULL, it is added to
143 * the pre_select list of the scheduler. Same goes for post_select.
145 * \sa task::pre_select, task::post_select
147 void register_task(struct task
*t
)
151 PARA_INFO_LOG("registering %s (%p)\n", t
->status
, t
);
153 PARA_DEBUG_LOG("pre_select: %p\n", &t
->pre_select
);
154 list_add_tail(&t
->pre_select_node
, &pre_select_list
);
156 if (t
->post_select
) {
157 PARA_DEBUG_LOG("post_select: %p\n", &t
->pre_select
);
158 list_add_tail(&t
->post_select_node
, &post_select_list
);
163 * Unregister all tasks.
165 * This will cause \a schedule() to return immediately because both the
166 * \a pre_select_list and the \a post_select_list are empty.
168 void sched_shutdown(void)
170 struct task
*t
, *tmp
;
174 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
)
176 list_for_each_entry_safe(t
, tmp
, &post_select_list
, post_select_node
)
182 * Get the list of all registered tasks.
184 * \return The task list.
186 * Each entry of the list contains an identifier which is simply a hex number
187 * that may be used in \a kill_task() to terminate the task.
188 * The result ist dynamically allocated and must be freed by the caller.
190 char *get_task_list(void)
192 struct task
*t
, *tmp
;
197 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
) {
199 tmp_msg
= make_message("%s%p\tpre\t%s\n", msg
? msg
: "", t
, t
->status
);
203 list_for_each_entry_safe(t
, tmp
, &post_select_list
, post_select_node
) {
205 // if (t->pre_select)
207 tmp_msg
= make_message("%s%p\tpost\t%s\n", msg
? msg
: "", t
, t
->status
);
211 //PARA_DEBUG_LOG("task list:\n%s", msg);
216 * Simulate an error for the given task.
218 * \param id The task identifier.
220 * Find the task identified by \a id, set the tasks' error value to
221 * \p -E_TASK_KILLED and unregister the task.
223 * \return Positive on success, negative on errors (e.g. if \a id does not
224 * correspond to a registered task).
226 int kill_task(char *id
)
228 struct task
*t
, *tmp
;
232 return -E_NOT_INITIALIZED
;
233 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
) {
234 sprintf(buf
, "%p", t
);
237 t
->error
= -E_TASK_KILLED
;
240 list_for_each_entry_safe(t
, tmp
, &post_select_list
, post_select_node
) {
241 sprintf(buf
, "%p", t
);
244 t
->error
= -E_TASK_KILLED
;
247 return -E_NO_SUCH_TASK
;