2 * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file sched.c paraslash's scheduling functions */
18 static struct list_head pre_select_list
, post_select_list
;
19 static int initialized
;
21 static struct timeval now_struct
;
22 struct timeval
*now
= &now_struct
;
24 static void sched_preselect(struct sched
*s
)
28 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
) {
30 // PARA_INFO_LOG("%s \n", t->status);
33 if (!t
->event_handler
)
40 static void sched_post_select(struct sched
*s
)
44 list_for_each_entry_safe(t
, tmp
, &post_select_list
, post_select_node
) {
46 // PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
47 if (t
->ret
> 0 || !t
->event_handler
)
54 * the core function for all paraslash programs
56 * \param s pointer to the scheduler struct
58 * This function updates the global \a now pointer, calls all registered
59 * pre_select hooks which may set the timeout and add any file descriptors to
60 * the fd sets of \a s. Next, it calls para_select() and makes the result available
61 * to the registered tasks by calling their post_select hook.
63 * \return Zero if no more tasks are left in either of the two lists, negative
64 * if para_select returned an error.
68 int sched(struct sched
*s
)
71 return -E_NOT_INITIALIZED
;
72 gettimeofday(now
, NULL
);
76 s
->timeout
= s
->default_timeout
;
79 s
->select_ret
= para_select(s
->max_fileno
+ 1, &s
->rfds
,
80 &s
->wfds
, &s
->timeout
);
81 if (s
->select_ret
< 0)
83 gettimeofday(now
, NULL
);
85 if (list_empty(&pre_select_list
) && list_empty(&post_select_list
))
91 * initialize the paraslash scheduler
93 static void init_sched(void)
95 PARA_INFO_LOG("%s", "initializing scheduler\n");
96 INIT_LIST_HEAD(&pre_select_list
);
97 INIT_LIST_HEAD(&post_select_list
);
102 * add a task to the scheduler
104 * \param t the task to add
106 * If the pre_select pointer of \a t is not \p NULL, it is added to
107 * the pre_select list of the scheduler. Same goes for post_select.
109 * \sa task::pre_select, task::post_select
111 void register_task(struct task
*t
)
115 PARA_INFO_LOG("registering %s (%p)\n", t
->status
, t
);
117 PARA_DEBUG_LOG("pre_select: %p\n", &t
->pre_select
);
118 para_list_add(&t
->pre_select_node
, &pre_select_list
);
120 if (t
->post_select
) {
121 PARA_DEBUG_LOG("post_select: %p\n", &t
->pre_select
);
122 para_list_add(&t
->post_select_node
, &post_select_list
);
127 * remove a task from the scheduler
129 * \param t the task to remove
131 * If the pre_select pointer of \a t is not \p NULL, it is removed from
132 * the pre_select list of the scheduler. Same goes for \a post_select.
134 void unregister_task(struct task
*t
)
138 PARA_INFO_LOG("unregistering %s (%p)\n", t
->status
, t
);
140 list_del(&t
->pre_select_node
);
142 list_del(&t
->post_select_node
);
146 * unregister all tasks
148 * This will cause \a sched() to return immediately because both the
149 * \a pre_select_list and the \a post_select_list are empty.
151 void sched_shutdown(void)
153 struct task
*t
, *tmp
;
157 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
)
159 /* remove tasks which do not have a pre_select hook */
160 list_for_each_entry_safe(t
, tmp
, &post_select_list
, post_select_node
)
166 * get the list of all registered tasks.
168 * \return the task list
170 * Each entry of the list contains an identifier which is simply a hex number
171 * that may be used in \a kill_task() to terminate the task.
172 * The result ist dynamically allocated and must be freed by the caller.
174 char *get_task_list(void)
176 struct task
*t
, *tmp
;
181 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
) {
183 tmp_msg
= make_message("%s%p\tpre\t%s\n", msg
? msg
: "", t
, t
->status
);
187 list_for_each_entry_safe(t
, tmp
, &post_select_list
, post_select_node
) {
189 // if (t->pre_select)
191 tmp_msg
= make_message("%s%p\tpost\t%s\n", msg
? msg
: "", t
, t
->status
);
195 //PARA_DEBUG_LOG("task list:\n%s", msg);
200 * simulate an error for the given task
202 * \param id the task identifier
204 * Find the task identified by \a id, set the tasks' return value to
205 * \p -E_TASK_KILLED and call the event handler of the task.
207 * \return Positive on success, negative on errors (e.g. if \a id does not
208 * correspond to a registered task).
210 int kill_task(char *id
)
212 struct task
*t
, *tmp
;
216 return -E_NOT_INITIALIZED
;
217 list_for_each_entry_safe(t
, tmp
, &pre_select_list
, pre_select_node
) {
218 sprintf(buf
, "%p", t
);
221 t
->ret
= -E_TASK_KILLED
;
222 if (t
->event_handler
)
226 list_for_each_entry_safe(t
, tmp
, &post_select_list
, post_select_node
) {
227 sprintf(buf
, "%p", t
);
230 t
->ret
= -E_TASK_KILLED
;
231 if (t
->event_handler
)
235 return -E_NO_SUCH_TASK
;