c2a0e64b16326c5ce4f28b20cb4a048ae6322d30
[paraslash.git] / sched.c
1 /*
2 * Copyright (C) 2006 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file sched.c paraslash's scheduling functions */
20
21 #include <sys/time.h>
22 #include "para.h"
23 #include "ipc.h"
24 #include "fd.h"
25 #include "list.h"
26 #include "sched.h"
27 #include "string.h"
28 #include "error.h"
29
30 static struct list_head pre_select_list, post_select_list;
31 static int initialized;
32
33 static struct timeval now_struct;
34 struct timeval *now = &now_struct;
35
36 static void sched_preselect(struct sched *s)
37 {
38 struct task *t, *tmp;
39 again:
40 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
41 t->pre_select(s, t);
42 // PARA_INFO_LOG("%s \n", t->status);
43 if (t->ret > 0)
44 continue;
45 if (!t->event_handler)
46 continue;
47 t->event_handler(t);
48 goto again;
49 }
50 }
51
52 static void sched_post_select(struct sched *s)
53 {
54 struct task *t, *tmp;
55
56 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
57 t->post_select(s, t);
58 // PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
59 if (t->ret > 0 || !t->event_handler)
60 continue;
61 t->event_handler(t);
62 }
63 }
64
65 /**
66 * the core function for all paraslash programs
67 *
68 * Short and sweet. It updates the global \a now pointer, calls all registered
69 * pre_select hooks which may set the timeout and add any file descriptors to
70 * the fd sets of \a s. Next, it calls para_select() and makes the result available
71 * to the registered tasks by calling their post_select hook.
72 *
73 * \return Zero if no more tasks are left in either of the two lists, negative
74 * if para_select returned an error.
75 *
76 * \sa task, now.
77 */
78 int sched(struct sched *s)
79 {
80 if (!initialized)
81 return -E_NOT_INITIALIZED;
82 gettimeofday(now, NULL);
83 again:
84 FD_ZERO(&s->rfds);
85 FD_ZERO(&s->wfds);
86 s->timeout = s->default_timeout;
87 s->max_fileno = -1;
88 sched_preselect(s);
89 s->select_ret = para_select(s->max_fileno + 1, &s->rfds,
90 &s->wfds, &s->timeout);
91 if (s->select_ret < 0)
92 return s->select_ret;
93 gettimeofday(now, NULL);
94 sched_post_select(s);
95 if (list_empty(&pre_select_list) && list_empty(&post_select_list))
96 return 0;
97 goto again;
98 }
99
100 /**
101 * initialize the paraslash scheduler
102 */
103 static void init_sched(void)
104 {
105 PARA_INFO_LOG("%s", "initializing scheduler\n");
106 INIT_LIST_HEAD(&pre_select_list);
107 INIT_LIST_HEAD(&post_select_list);
108 initialized = 1;
109 };
110
111 /**
112 * add a task to the scheduler
113 *
114 * \param t the task to add
115 *
116 * If the pre_select pointer of \a t is not \p NULL, it is added to
117 * the pre_select list of the scheduler. Same goes for post_select.
118 *
119 * \sa task::pre_select, task::post_select
120 */
121 void register_task(struct task *t)
122 {
123 if (!initialized)
124 init_sched();
125 PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
126 if (t->pre_select) {
127 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
128 list_add(&t->pre_select_node, &pre_select_list);
129 }
130 if (t->post_select) {
131 PARA_DEBUG_LOG("post_select: %p\n", &t->pre_select);
132 list_add(&t->post_select_node, &post_select_list);
133 }
134 }
135
136 /**
137 * remove a task from the scheduler
138 *
139 * \param t the task to remove
140 *
141 * If the pre_select pointer of \a t is not \p NULL, it is removed from
142 * the pre_select list of the scheduler. Same goes for \a post_select.
143 */
144 void unregister_task(struct task *t)
145 {
146 if (!initialized)
147 return;
148 PARA_INFO_LOG("unregistering %s (%p)\n", t->status, t);
149 if (t->pre_select)
150 list_del(&t->pre_select_node);
151 if (t->post_select)
152 list_del(&t->post_select_node);
153 };
154
155 /**
156 * unregister all tasks
157 *
158 * This will cause \a sched() to return immediately because both the
159 * \a pre_select_list and the \a post_select_list are empty.
160 */
161 void sched_shutdown(void)
162 {
163 struct task *t, *tmp;
164
165 if (!initialized)
166 return;
167 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node)
168 unregister_task(t);
169 /* remove tasks which do not have a pre_select hook */
170 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node)
171 unregister_task(t);
172 initialized = 0;
173 };
174
175 /**
176 * get the list of all registered tasks.
177 *
178 * \return the task list
179 *
180 * Each entry of the list contains an identifier which is simply a hex number
181 * that may be used in \a kill_task() to terminate the task.
182 * The result ist dynamically allocated and must be freed by the caller.
183 */
184 char *get_task_list(void)
185 {
186 struct task *t, *tmp;
187 char *msg = NULL;
188
189 if (!initialized)
190 return NULL;
191 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
192 char *tmp_msg;
193 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
194 free(msg);
195 msg = tmp_msg;
196 }
197 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
198 char *tmp_msg;
199 // if (t->pre_select)
200 // continue;
201 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
202 free(msg);
203 msg = tmp_msg;
204 }
205 //PARA_DEBUG_LOG("task list:\n%s", msg);
206 return msg;
207 }
208
209 /**
210 * simulate an error for the given task
211 *
212 * \param id the task identifier
213 *
214 * Find the task identified by \a id, set the tasks' return value to
215 * \p -E_TASK_KILLED and call the event handler of the task.
216 *
217 * \return Positive on success, negative on errors (e.g. if \a id does not
218 * correspond to a registered task).
219 */
220 int kill_task(char *id)
221 {
222 struct task *t, *tmp;
223 char buf[20];
224
225 if (!initialized)
226 return -E_NOT_INITIALIZED;
227 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
228 sprintf(buf, "%p", t);
229 if (strcmp(id, buf))
230 continue;
231 t->ret = -E_TASK_KILLED;
232 if (t->event_handler)
233 t->event_handler(t);
234 return 1;
235 }
236 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
237 sprintf(buf, "%p", t);
238 if (strcmp(id, buf))
239 continue;
240 t->ret = -E_TASK_KILLED;
241 if (t->event_handler)
242 t->event_handler(t);
243 return 1;
244 }
245 return -E_NO_SUCH_TASK;
246 }