vss: Check the afs socket either for reading or for writing.
[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 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)
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         gettimeofday(now, NULL);
109 again:
110         FD_ZERO(&s->rfds);
111         FD_ZERO(&s->wfds);
112         s->timeout = s->default_timeout;
113         s->max_fileno = -1;
114         sched_preselect(s);
115         if (list_empty(&pre_select_list) && list_empty(&post_select_list))
116                 return 0;
117         ret = para_select(s->max_fileno + 1, &s->rfds, &s->wfds, &s->timeout);
118         if (ret < 0)
119                 return ret;
120         gettimeofday(now, NULL);
121         sched_post_select(s);
122         if (list_empty(&pre_select_list) && list_empty(&post_select_list))
123                 return 0;
124         goto again;
125 }
126
127 /**
128  * Initialize the paraslash scheduler.
129  */
130 static void init_sched(void)
131 {
132         PARA_INFO_LOG("initializing scheduler\n");
133         INIT_LIST_HEAD(&pre_select_list);
134         INIT_LIST_HEAD(&post_select_list);
135         initialized = 1;
136 };
137
138 /**
139  * Add a task to the scheduler.
140  *
141  * \param t the task to add
142  *
143  * If the pre_select pointer of \a t is not \p NULL, it is added to
144  * the pre_select list of the scheduler. Same goes for post_select.
145  *
146  * \sa task::pre_select, task::post_select
147  */
148 void register_task(struct task *t)
149 {
150         if (!initialized)
151                 init_sched();
152         PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
153         if (t->pre_select) {
154                 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
155                 list_add_tail(&t->pre_select_node, &pre_select_list);
156         }
157         if (t->post_select) {
158                 PARA_DEBUG_LOG("post_select: %p\n", &t->pre_select);
159                 list_add_tail(&t->post_select_node, &post_select_list);
160         }
161 }
162
163 /**
164  * Unregister all tasks.
165  *
166  * This will cause \a schedule() to return immediately because both the
167  * \a pre_select_list and the \a post_select_list are empty.
168  */
169 void sched_shutdown(void)
170 {
171         struct task *t, *tmp;
172
173         if (!initialized)
174                 return;
175         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node)
176                 unregister_task(t);
177         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node)
178                 unregister_task(t);
179         initialized = 0;
180 };
181
182 /**
183  * Get the list of all registered tasks.
184  *
185  * \return The task list.
186  *
187  * Each entry of the list contains an identifier which is simply a hex number
188  * that may be used in \a kill_task() to terminate the task.
189  * The result ist dynamically allocated and must be freed by the caller.
190  */
191 char *get_task_list(void)
192 {
193         struct task *t, *tmp;
194         char *msg = NULL;
195
196         if (!initialized)
197                 return NULL;
198         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
199                 char *tmp_msg;
200                 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
201                 free(msg);
202                 msg = tmp_msg;
203         }
204         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
205                 char *tmp_msg;
206 //              if (t->pre_select)
207 //                      continue;
208                 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
209                 free(msg);
210                 msg = tmp_msg;
211         }
212         //PARA_DEBUG_LOG("task list:\n%s", msg);
213         return msg;
214 }
215
216 /**
217  * Simulate an error for the given task.
218  *
219  * \param id The task identifier.
220  *
221  * Find the task identified by \a id, set the tasks' error value to
222  * \p -E_TASK_KILLED and unregister the task.
223  *
224  * \return Positive on success, negative on errors (e.g. if \a id does not
225  * correspond to a registered task).
226  */
227 int kill_task(char *id)
228 {
229         struct task *t, *tmp;
230         char buf[20];
231
232         if (!initialized)
233                 return -E_NOT_INITIALIZED;
234         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
235                 sprintf(buf, "%p", t);
236                 if (strcmp(id, buf))
237                         continue;
238                 t->error = -E_TASK_KILLED;
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                 return 1;
247         }
248         return -E_NO_SUCH_TASK;
249 }