sched.c: Add checks for empty lists.
[paraslash.git] / sched.c
1 /*
2  * Copyright (C) 2006-2008 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 void unregister_task(struct task *t)
36 {
37         if (!initialized)
38                 return;
39         PARA_INFO_LOG("unregistering %s (%p)\n", t->status, t);
40         if (t->pre_select)
41                 list_del(&t->pre_select_node);
42         if (t->post_select)
43                 list_del(&t->post_select_node);
44         if (t->error >= 0)
45                 t->error = -E_TASK_KILLED;
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                 t->pre_select(s, t);
54 //              PARA_INFO_LOG("%s \n", t->status);
55                 if (t->error >= 0)
56                         continue;
57                 /*
58                  * We have to check whether the list is empty because the call
59                  * to ->pre_select() might have called sched_shutdown(). In
60                  * this case t has been unregistered already, so we must not
61                  * unregister it again.
62                  */
63                 if (list_empty(&pre_select_list))
64                         return;
65                 unregister_task(t);
66         }
67 }
68
69 static void sched_post_select(struct sched *s)
70 {
71         struct task *t, *tmp;
72
73         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
74                 t->post_select(s, t);
75 //              PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
76                 if (t->error >= 0)
77                         continue;
78                 /* nec., see sched_preselect() */
79                 if (list_empty(&post_select_list))
80                         return;
81                 unregister_task(t);
82         }
83 }
84
85 /**
86  * the core function for all paraslash programs
87  *
88  * \param s pointer to the scheduler struct
89  *
90  * This function updates the global \a now pointer, calls all registered
91  * pre_select hooks which may set the timeout and add any file descriptors to
92  * the fd sets of \a s.  Next, it calls para_select() and makes the result available
93  * to the registered tasks by calling their post_select hook.
94  *
95  * \return Zero if no more tasks are left in either of the two lists, negative
96  * if para_select returned an error.
97  *
98  * \sa task, now.
99  */
100 int schedule(struct sched *s)
101 {
102         int ret;
103
104         if (!initialized)
105                 return -E_NOT_INITIALIZED;
106         gettimeofday(now, NULL);
107 again:
108         FD_ZERO(&s->rfds);
109         FD_ZERO(&s->wfds);
110         s->timeout = s->default_timeout;
111         s->max_fileno = -1;
112         sched_preselect(s);
113         if (list_empty(&pre_select_list) && list_empty(&post_select_list))
114                 return 0;
115         ret = para_select(s->max_fileno + 1, &s->rfds, &s->wfds, &s->timeout);
116         if (ret < 0)
117                 return ret;
118         gettimeofday(now, NULL);
119         sched_post_select(s);
120         if (list_empty(&pre_select_list) && list_empty(&post_select_list))
121                 return 0;
122         goto again;
123 }
124
125 /**
126  * initialize the paraslash scheduler
127  */
128 static void init_sched(void)
129 {
130         PARA_INFO_LOG("initializing scheduler\n");
131         INIT_LIST_HEAD(&pre_select_list);
132         INIT_LIST_HEAD(&post_select_list);
133         initialized = 1;
134 };
135
136 /**
137  * add a task to the scheduler
138  *
139  * \param t the task to add
140  *
141  * If the pre_select pointer of \a t is not \p NULL, it is added to
142  * the pre_select list of the scheduler. Same goes for post_select.
143  *
144  * \sa task::pre_select, task::post_select
145  */
146 void register_task(struct task *t)
147 {
148         if (!initialized)
149                 init_sched();
150         PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
151         if (t->pre_select) {
152                 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
153                 list_add_tail(&t->pre_select_node, &pre_select_list);
154         }
155         if (t->post_select) {
156                 PARA_DEBUG_LOG("post_select: %p\n", &t->pre_select);
157                 list_add_tail(&t->post_select_node, &post_select_list);
158         }
159 }
160
161 /**
162  * unregister all tasks
163  *
164  * This will cause \a schedule() to return immediately because both the
165  * \a pre_select_list and the \a post_select_list are empty.
166  */
167 void sched_shutdown(void)
168 {
169         struct task *t, *tmp;
170
171         if (!initialized)
172                 return;
173         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node)
174                 unregister_task(t);
175         /* remove tasks which do not have a pre_select hook */
176         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node)
177                 unregister_task(t);
178         initialized = 0;
179 };
180
181 /**
182  * get the list of all registered tasks.
183  *
184  * \return the task list
185  *
186  * Each entry of the list contains an identifier which is simply a hex number
187  * that may be used in \a kill_task() to terminate the task.
188  * The result ist dynamically allocated and must be freed by the caller.
189  */
190 char *get_task_list(void)
191 {
192         struct task *t, *tmp;
193         char *msg = NULL;
194
195         if (!initialized)
196                 return NULL;
197         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
198                 char *tmp_msg;
199                 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
200                 free(msg);
201                 msg = tmp_msg;
202         }
203         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
204                 char *tmp_msg;
205 //              if (t->pre_select)
206 //                      continue;
207                 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
208                 free(msg);
209                 msg = tmp_msg;
210         }
211         //PARA_DEBUG_LOG("task list:\n%s", msg);
212         return msg;
213 }
214
215 /**
216  * simulate an error for the given task
217  *
218  * \param id the task identifier
219  *
220  * Find the task identified by \a id, set the tasks' error value to
221  * \p -E_TASK_KILLED and unregister the task.
222  *
223  * \return Positive on success, negative on errors (e.g. if \a id does not
224  * correspond to a registered task).
225  */
226 int kill_task(char *id)
227 {
228         struct task *t, *tmp;
229         char buf[20];
230
231         if (!initialized)
232                 return -E_NOT_INITIALIZED;
233         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
234                 sprintf(buf, "%p", t);
235                 if (strcmp(id, buf))
236                         continue;
237                 t->error = -E_TASK_KILLED;
238                 unregister_task(t);
239                 return 1;
240         }
241         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
242                 sprintf(buf, "%p", t);
243                 if (strcmp(id, buf))
244                         continue;
245                 t->error = -E_TASK_KILLED;
246                 unregister_task(t);
247                 return 1;
248         }
249         return -E_NO_SUCH_TASK;
250 }