cosmetics
[paraslash.git] / sched.c
1 #include <sys/time.h>
2 #include "para.h"
3 #include "ipc.h"
4 #include "fd.h"
5 #include "list.h"
6 #include "sched.h"
7 #include "string.h"
8 #include "error.h"
9
10 struct list_head pre_select_list;
11 struct list_head post_select_list;
12
13 static void sched_preselect(struct sched *s)
14 {
15         struct task *t, *tmp;
16 again:
17         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
18                 t->pre_select(s, t);
19                 if (t->ret > 0 || !t->event_handler)
20                         continue;
21                 t->event_handler(t);
22                 goto again;
23         }
24 }
25
26 static void sched_post_select(struct sched *s)
27 {
28         struct task *t, *tmp;
29
30         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
31                 t->post_select(s, t);
32                 if (t->ret > 0 || !t->event_handler)
33                         continue;
34                 t->event_handler(t);
35         }
36 }
37
38 int sched(struct sched *s)
39 {
40
41         gettimeofday(&s->now, NULL);
42 again:
43         FD_ZERO(&s->rfds);
44         FD_ZERO(&s->wfds);
45         s->timeout = s->default_timeout;
46         s->max_fileno = -1;
47         sched_preselect(s);
48         s->select_ret = para_select(s->max_fileno + 1, &s->rfds,
49                 &s->wfds, &s->timeout);
50         if (s->select_ret < 0)
51                 return s->select_ret;
52         gettimeofday(&s->now, NULL);
53         sched_post_select(s);
54         if (list_empty(&pre_select_list) && list_empty(&post_select_list))
55                 return 0;
56         goto again;
57 }
58
59 void *register_task(struct task *t)
60 {
61         PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
62         if (t->pre_select) {
63                 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
64                 if (t->flags & PRE_ADD_TAIL)
65                         list_add_tail(&t->pre_select_node, &pre_select_list);
66                 else
67                         list_add(&t->pre_select_node, &pre_select_list);
68         }
69         if (t->post_select) {
70                 PARA_DEBUG_LOG("post_select: %p\n", &t->pre_select);
71                 if (t->flags & POST_ADD_TAIL)
72                         list_add_tail(&t->post_select_node, &post_select_list);
73                 else
74                         list_add(&t->post_select_node, &post_select_list);
75         }
76         return t;
77 }
78
79 void unregister_task(struct task *t)
80 {
81         PARA_INFO_LOG("unregistering %s (%p)\n", t->status, t);
82         if (t->pre_select)
83                 list_del(&t->pre_select_node);
84         if (t->post_select)
85                 list_del(&t->post_select_node);
86 };
87
88 void init_sched(void)
89 {
90         INIT_LIST_HEAD(&pre_select_list);
91         INIT_LIST_HEAD(&post_select_list);
92 };
93
94 void sched_shutdown(void)
95 {
96         struct task *t, *tmp;
97
98         list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node)
99                 unregister_task(t);
100         /* remove tasks which do not have a pre_select hook */
101         list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node)
102                 unregister_task(t);
103 };
104
105
106 //char *get_tast_list();