gcrypt: Fix return value of pad_oaep().
[paraslash.git] / sched.c
1 /*
2 * Copyright (C) 2006-2011 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 <regex.h>
10 #include <assert.h>
11 #include <sys/time.h>
12
13 #include "para.h"
14 #include "ipc.h"
15 #include "fd.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "string.h"
19 #include "time.h"
20 #include "error.h"
21
22 static struct list_head pre_select_list, post_select_list;
23 static int initialized;
24
25 static struct timeval now_struct;
26 struct timeval *now = &now_struct;
27
28 /*
29 * Remove a task from the scheduler.
30 *
31 * \param t The task to remove.
32 *
33 * If the pre_select pointer of \a t is not \p NULL, it is removed from
34 * the pre_select list of the scheduler. Same goes for \a post_select.
35 */
36 static void unregister_task(struct task *t)
37 {
38 if (!initialized)
39 return;
40 assert(t->error < 0);
41 PARA_INFO_LOG("unregistering %s (%s)\n", t->status,
42 para_strerror(-t->error));
43 if (t->pre_select)
44 list_del(&t->pre_select_node);
45 if (t->post_select)
46 list_del(&t->post_select_node);
47 }
48
49 static void sched_preselect(struct sched *s)
50 {
51 struct task *t, *tmp;
52 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
53 if (t->pre_select)
54 t->pre_select(s, t);
55 // PARA_INFO_LOG("%s \n", t->status);
56 if (t->error >= 0)
57 continue;
58 /*
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.
63 */
64 if (list_empty(&pre_select_list))
65 return;
66 unregister_task(t);
67 }
68 }
69
70 //#define SCHED_DEBUG 1
71 static inline void call_post_select(struct sched *s, struct task *t)
72 {
73 #ifndef SCHED_DEBUG
74 t->post_select(s, t);
75 #else
76 struct timeval t1, t2, diff;
77 unsigned long pst;
78
79 gettimeofday(&t1, NULL);
80 t->post_select(s, t);
81 gettimeofday(&t2, NULL);
82 tv_diff(&t1, &t2, &diff);
83 pst = tv2ms(&diff);
84 if (pst > 50)
85 PARA_WARNING_LOG("%s: post_select time: %lums\n",
86 t->status, pst);
87 #endif
88 }
89
90 static void sched_post_select(struct sched *s)
91 {
92 struct task *t, *tmp;
93
94 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
95 if (t->error >= 0)
96 call_post_select(s, t);
97 // PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
98 if (t->error >= 0)
99 continue;
100 /* nec., see sched_preselect() */
101 if (list_empty(&post_select_list))
102 return;
103 unregister_task(t);
104 }
105 }
106
107 /**
108 * The core function for all paraslash programs.
109 *
110 * \param s Pointer to the scheduler struct.
111 *
112 * This function updates the global \a now pointer, calls all registered
113 * pre_select hooks which may set the timeout and add any file descriptors to
114 * the fd sets of \a s. Next, it calls para_select() and makes the result available
115 * to the registered tasks by calling their post_select hook.
116 *
117 * \return Zero if no more tasks are left in either of the two lists, negative
118 * if para_select returned an error.
119 *
120 * \sa task, now.
121 */
122 int schedule(struct sched *s)
123 {
124 int ret;
125
126 if (!initialized)
127 return -E_NOT_INITIALIZED;
128 if (!s->select_function)
129 s->select_function = para_select;
130 again:
131 FD_ZERO(&s->rfds);
132 FD_ZERO(&s->wfds);
133 s->select_timeout = s->default_timeout;
134 s->max_fileno = -1;
135 gettimeofday(now, NULL);
136 sched_preselect(s);
137 if (list_empty(&pre_select_list) && list_empty(&post_select_list))
138 return 0;
139 ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds,
140 &s->select_timeout);
141 if (ret < 0)
142 return ret;
143 if (ret == 0) {
144 /*
145 * APUE: Be careful not to check the descriptor sets on return
146 * unless the return value is greater than zero. The return
147 * state of the descriptor sets is implementation dependent if
148 * either a signal is caught or the timer expires.
149 */
150 FD_ZERO(&s->rfds);
151 FD_ZERO(&s->wfds);
152 }
153 gettimeofday(now, NULL);
154 sched_post_select(s);
155 if (list_empty(&pre_select_list) && list_empty(&post_select_list))
156 return 0;
157 goto again;
158 }
159
160 /*
161 * Initialize the paraslash scheduler.
162 */
163 static void init_sched(void)
164 {
165 PARA_INFO_LOG("initializing scheduler\n");
166 INIT_LIST_HEAD(&pre_select_list);
167 INIT_LIST_HEAD(&post_select_list);
168 initialized = 1;
169 }
170
171 /**
172 * Add a task to the scheduler.
173 *
174 * \param t the task to add
175 *
176 * If the pre_select pointer of \a t is not \p NULL, it is added to
177 * the pre_select list of the scheduler. Same goes for post_select.
178 *
179 * \sa task::pre_select, task::post_select
180 */
181 void register_task(struct task *t)
182 {
183 if (!initialized)
184 init_sched();
185 PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
186 if (t->pre_select) {
187 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
188 list_add_tail(&t->pre_select_node, &pre_select_list);
189 }
190 if (t->post_select) {
191 PARA_DEBUG_LOG("post_select: %p\n", &t->post_select);
192 list_add_tail(&t->post_select_node, &post_select_list);
193 }
194 }
195
196 /**
197 * Unregister all tasks.
198 *
199 * This will cause \a schedule() to return immediately because both the
200 * \a pre_select_list and the \a post_select_list are empty.
201 */
202 void sched_shutdown(void)
203 {
204 struct task *t, *tmp;
205
206 if (!initialized)
207 return;
208 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
209 t->error = -E_SCHED_SHUTDOWN;
210 unregister_task(t);
211 }
212 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
213 t->error = -E_SCHED_SHUTDOWN;
214 unregister_task(t);
215 }
216 initialized = 0;
217 }
218
219 /**
220 * Get the list of all registered tasks.
221 *
222 * \return The task list.
223 *
224 * Each entry of the list contains an identifier which is simply a hex number.
225 * The result is dynamically allocated and must be freed by the caller.
226 */
227 char *get_task_list(void)
228 {
229 struct task *t, *tmp;
230 char *msg = NULL;
231
232 if (!initialized)
233 return NULL;
234 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
235 char *tmp_msg;
236 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
237 free(msg);
238 msg = tmp_msg;
239 }
240 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
241 char *tmp_msg;
242 // if (t->pre_select)
243 // continue;
244 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
245 free(msg);
246 msg = tmp_msg;
247 }
248 //PARA_DEBUG_LOG("task list:\n%s", msg);
249 return msg;
250 }
251
252 /**
253 * Set the select timeout to the minimal possible value.
254 *
255 * \param s Pointer to the scheduler struct.
256 *
257 * This causes the next select() call to return immediately.
258 */
259 void sched_min_delay(struct sched *s)
260 {
261 s->select_timeout.tv_sec = 0;
262 s->select_timeout.tv_usec = 1;
263 }
264
265 /**
266 * Impose an upper bound for the timeout of the next select() call.
267 *
268 * \param to Maximal allowed timeout.
269 * \param s Pointer to the scheduler struct.
270 *
271 * If the current scheduler timeout is already smaller than \a to, this
272 * function does nothing. Otherwise the timeout for the next select() call is
273 * set to the given value.
274 *
275 * \sa sched_request_timeout_ms().
276 */
277 void sched_request_timeout(struct timeval *to, struct sched *s)
278 {
279 if (tv_diff(&s->select_timeout, to, NULL) > 0)
280 s->select_timeout = *to;
281 }
282
283 /**
284 * Force the next select() call to return before the given amount of milliseconds.
285 *
286 * \param ms The maximal allowed timeout in milliseconds.
287 * \param s Pointer to the scheduler struct.
288 *
289 * Like sched_request_timeout() this imposes an upper bound on the timeout
290 * value for the next select() call.
291 */
292 void sched_request_timeout_ms(long unsigned ms, struct sched *s)
293 {
294 struct timeval tv;
295 ms2tv(ms, &tv);
296 sched_request_timeout(&tv, s);
297 }
298
299 /**
300 * Force the next select() call to return before the given future time.
301 *
302 * \param barrier Absolute time before select() should return.
303 * \param s Pointer to the scheduler struct.
304 *
305 * If \a barrier is in the past, this function does nothing.
306 *
307 * \sa sched_request_barrier_or_min_delay().
308 */
309 void sched_request_barrier(struct timeval *barrier, struct sched *s)
310 {
311 struct timeval diff;
312
313 if (tv_diff(now, barrier, &diff) > 0)
314 return;
315 sched_request_timeout(&diff, s);
316 }
317
318 /**
319 * Force the next select() call to return before the given time.
320 *
321 * \param barrier Absolute time before select() should return.
322 * \param s Pointer to the scheduler struct.
323 *
324 * If \a barrier is in the past, this function requests a minimal timeout.
325 *
326 * \sa sched_min_delay(), sched_request_barrier().
327 */
328 void sched_request_barrier_or_min_delay(struct timeval *barrier, struct sched *s)
329 {
330 struct timeval diff;
331
332 if (tv_diff(now, barrier, &diff) > 0)
333 return sched_min_delay(s);
334 sched_request_timeout(&diff, s);
335 }