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