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
);
45 t
->error
= -E_TASK_KILLED
;
49 static void sched_preselect(struct sched
*s
)
52 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
) {
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 gettimeofday(now
, NULL
);
112 s
->timeout
= s
->default_timeout
;
115 if (list_empty(&pre_select_list
) && list_empty(&post_select_list
))
117 ret
= para_select(s
->max_fileno
+ 1, &s
->rfds
, &s
->wfds
, &s
->timeout
);
120 gettimeofday(now
, NULL
);
121 sched_post_select(s
);
122 if (list_empty(&pre_select_list
) && list_empty(&post_select_list
))
128 * Initialize the paraslash scheduler.
130 static void init_sched(void)
132 PARA_INFO_LOG("initializing scheduler\n");
133 INIT_LIST_HEAD(&pre_select_list
);
134 INIT_LIST_HEAD(&post_select_list
);
139 * Add a task to the scheduler.
141 * \param t the task to add
143 * If the pre_select pointer of \a t is not \p NULL, it is added to
144 * the pre_select list of the scheduler. Same goes for post_select.
146 * \sa task::pre_select, task::post_select
148 void register_task(struct task
*t
)
152 PARA_INFO_LOG("registering %s (%p)\n", t
->status
, t
);
154 PARA_DEBUG_LOG("pre_select: %p\n", &t
->pre_select
);
155 list_add_tail(&t
->pre_select_node
, &pre_select_list
);
157 if (t
->post_select
) {
158 PARA_DEBUG_LOG("post_select: %p\n", &t
->pre_select
);
159 list_add_tail(&t
->post_select_node
, &post_select_list
);
164 * Unregister all tasks.
166 * This will cause \a schedule() to return immediately because both the
167 * \a pre_select_list and the \a post_select_list are empty.
169 void sched_shutdown(void)
171 struct task
*t
, *tmp
;
175 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
)
177 list_for_each_entry_safe(t
, tmp
, &post_select_list
, post_select_node
)
183 * Get the list of all registered tasks.
185 * \return The task list.
187 * Each entry of the list contains an identifier which is simply a hex number
188 * that may be used in \a kill_task() to terminate the task.
189 * The result ist dynamically allocated and must be freed by the caller.
191 char *get_task_list(void)
193 struct task
*t
, *tmp
;
198 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
) {
200 tmp_msg
= make_message("%s%p\tpre\t%s\n", msg
? msg
: "", t
, t
->status
);
204 list_for_each_entry_safe(t
, tmp
, &post_select_list
, post_select_node
) {
206 // if (t->pre_select)
208 tmp_msg
= make_message("%s%p\tpost\t%s\n", msg
? msg
: "", t
, t
->status
);
212 //PARA_DEBUG_LOG("task list:\n%s", msg);
217 * Simulate an error for the given task.
219 * \param id The task identifier.
221 * Find the task identified by \a id, set the tasks' error value to
222 * \p -E_TASK_KILLED and unregister the task.
224 * \return Positive on success, negative on errors (e.g. if \a id does not
225 * correspond to a registered task).
227 int kill_task(char *id
)
229 struct task
*t
, *tmp
;
233 return -E_NOT_INITIALIZED
;
234 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
) {
235 sprintf(buf
, "%p", t
);
238 t
->error
= -E_TASK_KILLED
;
242 list_for_each_entry_safe(t
, tmp
, &post_select_list
, post_select_node
) {
243 sprintf(buf
, "%p", t
);
246 t
->error
= -E_TASK_KILLED
;
250 return -E_NO_SUCH_TASK
;