Make autoconf-2.66 happy.
[paraslash.git] / sched.c
1 /*
2  * Copyright (C) 2006-2009 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 <dirent.h> /* readdir() */
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 "error.h"
20
21 static struct list_head pre_select_list, post_select_list;
22 static int initialized;
23
24 static struct timeval now_struct;
25 struct timeval *now = &now_struct;
26
27 /**
28  * Remove a task from the scheduler.
29  *
30  * \param t The task to remove.
31  *
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.
34  */
35 static void unregister_task(struct task *t)
36 {
37         if (!initialized)
38                 return;
39         PARA_INFO_LOG("unregistering %s (%s)\n", t->status,
40                 t->error <0? para_strerror(-t->error) : "shutdown");
41         if (t->pre_select)
42                 list_del(&t->pre_select_node);
43         if (t->post_select)
44                 list_del(&t->post_select_node);
45         t->error = -E_TASK_UNREGISTERED;
46 }
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->error >= 0 && 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 static void sched_post_select(struct sched *s)
71 {
72         struct task *t, *tmp;
73
74         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
75                 if (t->error >= 0)
76                         t->post_select(s, t);
77 //              PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
78                 if (t->error >= 0)
79                         continue;
80                 /* nec., see sched_preselect() */
81                 if (list_empty(&post_select_list))
82                         return;
83                 unregister_task(t);
84         }
85 }
86
87 /**
88  * The core function for all paraslash programs.
89  *
90  * \param s Pointer to the scheduler struct.
91  *
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.
96  *
97  * \return Zero if no more tasks are left in either of the two lists, negative
98  * if para_select returned an error.
99  *
100  * \sa task, now.
101  */
102 int schedule(struct sched *s)
103 {
104         int ret;
105
106         if (!initialized)
107                 return -E_NOT_INITIALIZED;
108         if (!s->select_function)
109                 s->select_function = para_select;
110         gettimeofday(now, NULL);
111 again:
112         FD_ZERO(&s->rfds);
113         FD_ZERO(&s->wfds);
114         s->timeout = s->default_timeout;
115         s->max_fileno = -1;
116         sched_preselect(s);
117         if (list_empty(&pre_select_list) && list_empty(&post_select_list))
118                 return 0;
119         ret = s->select_function(s->max_fileno + 1, &s->rfds, &s->wfds, &s->timeout);
120         if (ret < 0)
121                 return ret;
122         gettimeofday(now, NULL);
123         sched_post_select(s);
124         if (list_empty(&pre_select_list) && list_empty(&post_select_list))
125                 return 0;
126         goto again;
127 }
128
129 /**
130  * Initialize the paraslash scheduler.
131  */
132 static void init_sched(void)
133 {
134         PARA_INFO_LOG("initializing scheduler\n");
135         INIT_LIST_HEAD(&pre_select_list);
136         INIT_LIST_HEAD(&post_select_list);
137         initialized = 1;
138 }
139
140 /**
141  * Add a task to the scheduler.
142  *
143  * \param t the task to add
144  *
145  * If the pre_select pointer of \a t is not \p NULL, it is added to
146  * the pre_select list of the scheduler. Same goes for post_select.
147  *
148  * \sa task::pre_select, task::post_select
149  */
150 void register_task(struct task *t)
151 {
152         if (!initialized)
153                 init_sched();
154         PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
155         if (t->pre_select) {
156                 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
157                 list_add_tail(&t->pre_select_node, &pre_select_list);
158         }
159         if (t->post_select) {
160                 PARA_DEBUG_LOG("post_select: %p\n", &t->post_select);
161                 list_add_tail(&t->post_select_node, &post_select_list);
162         }
163 }
164
165 /**
166  * Unregister all tasks.
167  *
168  * This will cause \a schedule() to return immediately because both the
169  * \a pre_select_list and the \a post_select_list are empty.
170  */
171 void sched_shutdown(void)
172 {
173         struct task *t, *tmp;
174
175         if (!initialized)
176                 return;
177         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node)
178                 unregister_task(t);
179         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node)
180                 unregister_task(t);
181         initialized = 0;
182 }
183
184 /**
185  * Get the list of all registered tasks.
186  *
187  * \return The task list.
188  *
189  * Each entry of the list contains an identifier which is simply a hex number
190  * that may be used in \a kill_task() to terminate the task.
191  * The result ist dynamically allocated and must be freed by the caller.
192  */
193 char *get_task_list(void)
194 {
195         struct task *t, *tmp;
196         char *msg = NULL;
197
198         if (!initialized)
199                 return NULL;
200         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
201                 char *tmp_msg;
202                 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
203                 free(msg);
204                 msg = tmp_msg;
205         }
206         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
207                 char *tmp_msg;
208 //              if (t->pre_select)
209 //                      continue;
210                 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
211                 free(msg);
212                 msg = tmp_msg;
213         }
214         //PARA_DEBUG_LOG("task list:\n%s", msg);
215         return msg;
216 }
217
218 /**
219  * Simulate an error for the given task.
220  *
221  * \param id The task identifier.
222  *
223  * Find the task identified by \a id, set the tasks' error value to
224  * \p -E_TASK_KILLED and unregister the task.
225  *
226  * \return Positive on success, negative on errors (e.g. if \a id does not
227  * correspond to a registered task).
228  */
229 int kill_task(char *id)
230 {
231         struct task *t, *tmp;
232         char buf[20];
233
234         if (!initialized)
235                 return -E_NOT_INITIALIZED;
236         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
237                 sprintf(buf, "%p", t);
238                 if (strcmp(id, buf))
239                         continue;
240                 t->error = -E_TASK_KILLED;
241                 return 1;
242         }
243         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
244                 sprintf(buf, "%p", t);
245                 if (strcmp(id, buf))
246                         continue;
247                 t->error = -E_TASK_KILLED;
248                 return 1;
249         }
250         return -E_NO_SUCH_TASK;
251 }