rc4: Round up output buffer size.
[paraslash.git] / sched.c
1 /*
2 * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6
7 /** \file sched.c paraslash's scheduling functions */
8
9 #include <sys/time.h>
10 #include "para.h"
11 #include "ipc.h"
12 #include "fd.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "string.h"
16 #include "error.h"
17
18 static struct list_head pre_select_list, post_select_list;
19 static int initialized;
20
21 static struct timeval now_struct;
22 struct timeval *now = &now_struct;
23
24 static void sched_preselect(struct sched *s)
25 {
26 struct task *t, *tmp;
27 again:
28 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
29 t->pre_select(s, t);
30 // PARA_INFO_LOG("%s \n", t->status);
31 if (t->ret > 0)
32 continue;
33 if (!t->event_handler)
34 continue;
35 t->event_handler(t);
36 goto again;
37 }
38 }
39
40 static void sched_post_select(struct sched *s)
41 {
42 struct task *t, *tmp;
43
44 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
45 t->post_select(s, t);
46 // PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
47 if (t->ret > 0 || !t->event_handler)
48 continue;
49 t->event_handler(t);
50 }
51 }
52
53 /**
54 * the core function for all paraslash programs
55 *
56 * \param s pointer to the scheduler struct
57 *
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.
62 *
63 * \return Zero if no more tasks are left in either of the two lists, negative
64 * if para_select returned an error.
65 *
66 * \sa task, now.
67 */
68 int sched(struct sched *s)
69 {
70 if (!initialized)
71 return -E_NOT_INITIALIZED;
72 gettimeofday(now, NULL);
73 again:
74 FD_ZERO(&s->rfds);
75 FD_ZERO(&s->wfds);
76 s->timeout = s->default_timeout;
77 s->max_fileno = -1;
78 sched_preselect(s);
79 s->select_ret = para_select(s->max_fileno + 1, &s->rfds,
80 &s->wfds, &s->timeout);
81 if (s->select_ret < 0)
82 return s->select_ret;
83 gettimeofday(now, NULL);
84 sched_post_select(s);
85 if (list_empty(&pre_select_list) && list_empty(&post_select_list))
86 return 0;
87 goto again;
88 }
89
90 /**
91 * initialize the paraslash scheduler
92 */
93 static void init_sched(void)
94 {
95 PARA_INFO_LOG("%s", "initializing scheduler\n");
96 INIT_LIST_HEAD(&pre_select_list);
97 INIT_LIST_HEAD(&post_select_list);
98 initialized = 1;
99 };
100
101 /**
102 * add a task to the scheduler
103 *
104 * \param t the task to add
105 *
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.
108 *
109 * \sa task::pre_select, task::post_select
110 */
111 void register_task(struct task *t)
112 {
113 if (!initialized)
114 init_sched();
115 PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
116 if (t->pre_select) {
117 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
118 para_list_add(&t->pre_select_node, &pre_select_list);
119 }
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);
123 }
124 }
125
126 /**
127 * remove a task from the scheduler
128 *
129 * \param t the task to remove
130 *
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.
133 */
134 void unregister_task(struct task *t)
135 {
136 if (!initialized)
137 return;
138 PARA_INFO_LOG("unregistering %s (%p)\n", t->status, t);
139 if (t->pre_select)
140 list_del(&t->pre_select_node);
141 if (t->post_select)
142 list_del(&t->post_select_node);
143 };
144
145 /**
146 * unregister all tasks
147 *
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.
150 */
151 void sched_shutdown(void)
152 {
153 struct task *t, *tmp;
154
155 if (!initialized)
156 return;
157 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node)
158 unregister_task(t);
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)
161 unregister_task(t);
162 initialized = 0;
163 };
164
165 /**
166 * get the list of all registered tasks.
167 *
168 * \return the task list
169 *
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.
173 */
174 char *get_task_list(void)
175 {
176 struct task *t, *tmp;
177 char *msg = NULL;
178
179 if (!initialized)
180 return NULL;
181 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
182 char *tmp_msg;
183 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
184 free(msg);
185 msg = tmp_msg;
186 }
187 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
188 char *tmp_msg;
189 // if (t->pre_select)
190 // continue;
191 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
192 free(msg);
193 msg = tmp_msg;
194 }
195 //PARA_DEBUG_LOG("task list:\n%s", msg);
196 return msg;
197 }
198
199 /**
200 * simulate an error for the given task
201 *
202 * \param id the task identifier
203 *
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.
206 *
207 * \return Positive on success, negative on errors (e.g. if \a id does not
208 * correspond to a registered task).
209 */
210 int kill_task(char *id)
211 {
212 struct task *t, *tmp;
213 char buf[20];
214
215 if (!initialized)
216 return -E_NOT_INITIALIZED;
217 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
218 sprintf(buf, "%p", t);
219 if (strcmp(id, buf))
220 continue;
221 t->ret = -E_TASK_KILLED;
222 if (t->event_handler)
223 t->event_handler(t);
224 return 1;
225 }
226 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
227 sprintf(buf, "%p", t);
228 if (strcmp(id, buf))
229 continue;
230 t->ret = -E_TASK_KILLED;
231 if (t->event_handler)
232 t->event_handler(t);
233 return 1;
234 }
235 return -E_NO_SUCH_TASK;
236 }