ogg_afh.c: add documentation of struct ogg_datasource
[paraslash.git] / sched.c
1 /*
2  * Copyright (C) 2006-2007 Andre Noll <maan@systemlinux.org>
3  *
4  *     This program is free software; you can redistribute it and/or modify
5  *     it under the terms of the GNU General Public License as published by
6  *     the Free Software Foundation; either version 2 of the License, or
7  *     (at your option) any later version.
8  *
9  *     This program is distributed in the hope that it will be useful,
10  *     but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *     GNU General Public License for more details.
13  *
14  *     You should have received a copy of the GNU General Public License
15  *     along with this program; if not, write to the Free Software
16  *     Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
17  */
18
19 /** \file sched.c paraslash's scheduling functions */
20
21 #include <sys/time.h>
22 #include "para.h"
23 #include "ipc.h"
24 #include "fd.h"
25 #include "list.h"
26 #include "sched.h"
27 #include "string.h"
28 #include "error.h"
29
30 static struct list_head pre_select_list, post_select_list;
31 static int initialized;
32
33 static struct timeval now_struct;
34 struct timeval *now = &now_struct;
35
36 static void sched_preselect(struct sched *s)
37 {
38         struct task *t, *tmp;
39 again:
40         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
41                 t->pre_select(s, t);
42 //              PARA_INFO_LOG("%s \n", t->status);
43                 if (t->ret > 0)
44                         continue;
45                 if (!t->event_handler)
46                         continue;
47                 t->event_handler(t);
48                 goto again;
49         }
50 }
51
52 static void sched_post_select(struct sched *s)
53 {
54         struct task *t, *tmp;
55
56         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
57                 t->post_select(s, t);
58 //              PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
59                 if (t->ret > 0 || !t->event_handler)
60                         continue;
61                 t->event_handler(t);
62         }
63 }
64
65 /**
66  * the core function for all paraslash programs
67  *
68  * \param s pointer to the scheduler struct
69  *
70  * This function updates the global \a now pointer, calls all registered
71  * pre_select hooks which may set the timeout and add any file descriptors to
72  * the fd sets of \a s.  Next, it calls para_select() and makes the result available
73  * to the registered tasks by calling their post_select hook.
74  *
75  * \return Zero if no more tasks are left in either of the two lists, negative
76  * if para_select returned an error.
77  *
78  * \sa task, now.
79  */
80 int sched(struct sched *s)
81 {
82         if (!initialized)
83                 return -E_NOT_INITIALIZED;
84         gettimeofday(now, NULL);
85 again:
86         FD_ZERO(&s->rfds);
87         FD_ZERO(&s->wfds);
88         s->timeout = s->default_timeout;
89         s->max_fileno = -1;
90         sched_preselect(s);
91         s->select_ret = para_select(s->max_fileno + 1, &s->rfds,
92                 &s->wfds, &s->timeout);
93         if (s->select_ret < 0)
94                 return s->select_ret;
95         gettimeofday(now, NULL);
96         sched_post_select(s);
97         if (list_empty(&pre_select_list) && list_empty(&post_select_list))
98                 return 0;
99         goto again;
100 }
101
102 /**
103  * initialize the paraslash scheduler
104  */
105 static void init_sched(void)
106 {
107         PARA_INFO_LOG("%s", "initializing scheduler\n");
108         INIT_LIST_HEAD(&pre_select_list);
109         INIT_LIST_HEAD(&post_select_list);
110         initialized = 1;
111 };
112
113 /**
114  * add a task to the scheduler
115  *
116  * \param t the task to add
117  *
118  * If the pre_select pointer of \a t is not \p NULL, it is added to
119  * the pre_select list of the scheduler. Same goes for post_select.
120  *
121  * \sa task::pre_select, task::post_select
122  */
123 void register_task(struct task *t)
124 {
125         if (!initialized)
126                 init_sched();
127         PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
128         if (t->pre_select) {
129                 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
130                 para_list_add(&t->pre_select_node, &pre_select_list);
131         }
132         if (t->post_select) {
133                 PARA_DEBUG_LOG("post_select: %p\n", &t->pre_select);
134                 para_list_add(&t->post_select_node, &post_select_list);
135         }
136 }
137
138 /**
139  * remove a task from the scheduler
140  *
141  * \param t the task to remove
142  *
143  * If the pre_select pointer of \a t is not \p NULL, it is removed from
144  * the pre_select list of the scheduler. Same goes for \a post_select.
145  */
146 void unregister_task(struct task *t)
147 {
148         if (!initialized)
149                 return;
150         PARA_INFO_LOG("unregistering %s (%p)\n", t->status, t);
151         if (t->pre_select)
152                 list_del(&t->pre_select_node);
153         if (t->post_select)
154                 list_del(&t->post_select_node);
155 };
156
157 /**
158  * unregister all tasks
159  *
160  * This will cause \a sched() to return immediately because both the
161  * \a pre_select_list and the \a post_select_list are empty.
162  */
163 void sched_shutdown(void)
164 {
165         struct task *t, *tmp;
166
167         if (!initialized)
168                 return;
169         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node)
170                 unregister_task(t);
171         /* remove tasks which do not have a pre_select hook */
172         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node)
173                 unregister_task(t);
174         initialized = 0;
175 };
176
177 /**
178  * get the list of all registered tasks.
179  *
180  * \return the task list
181  *
182  * Each entry of the list contains an identifier which is simply a hex number
183  * that may be used in \a kill_task() to terminate the task.
184  * The result ist dynamically allocated and must be freed by the caller.
185  */
186 char *get_task_list(void)
187 {
188         struct task *t, *tmp;
189         char *msg = NULL;
190
191         if (!initialized)
192                 return NULL;
193         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
194                 char *tmp_msg;
195                 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
196                 free(msg);
197                 msg = tmp_msg;
198         }
199         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
200                 char *tmp_msg;
201 //              if (t->pre_select)
202 //                      continue;
203                 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
204                 free(msg);
205                 msg = tmp_msg;
206         }
207         //PARA_DEBUG_LOG("task list:\n%s", msg);
208         return msg;
209 }
210
211 /**
212  * simulate an error for the given task
213  *
214  * \param id the task identifier
215  *
216  * Find the task identified by \a id, set the tasks' return value to
217  * \p -E_TASK_KILLED and call the event handler of the task.
218  *
219  * \return Positive on success, negative on errors (e.g. if \a id does not
220  * correspond to a registered task).
221  */
222 int kill_task(char *id)
223 {
224         struct task *t, *tmp;
225         char buf[20];
226
227         if (!initialized)
228                 return -E_NOT_INITIALIZED;
229         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
230                 sprintf(buf, "%p", t);
231                 if (strcmp(id, buf))
232                         continue;
233                 t->ret = -E_TASK_KILLED;
234                 if (t->event_handler)
235                         t->event_handler(t);
236                 return 1;
237         }
238         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
239                 sprintf(buf, "%p", t);
240                 if (strcmp(id, buf))
241                         continue;
242                 t->ret = -E_TASK_KILLED;
243                 if (t->event_handler)
244                         t->event_handler(t);
245                 return 1;
246         }
247         return -E_NO_SUCH_TASK;
248 }