first version of the universal paraslash scheduler
[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->error_handler)
20 continue;
21 if (t->ret < 0) {
22 t->error_handler(t);
23 goto again;
24 }
25 if (!(t->flags & PRE_EOF_IS_ERROR))
26 continue;
27 t->ret = -E_PRE_EOF;
28 t->error_handler(t);
29 goto again;
30 }
31 }
32
33 static void sched_post_select(struct sched *s)
34 {
35 struct task *t, *tmp;
36
37 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
38 t->post_select(s, t);
39 if (t->ret > 0 || !t->error_handler)
40 continue;
41 if (t->ret < 0) {
42 t->error_handler(t);
43 continue;
44 }
45 if (!(t->flags & POST_EOF_IS_ERROR))
46 continue;
47 t->ret = -E_POST_EOF;
48 t->error_handler(t);
49 }
50 }
51
52 int sched(struct sched *s)
53 {
54
55 gettimeofday(&s->now, NULL);
56 again:
57 FD_ZERO(&s->rfds);
58 FD_ZERO(&s->wfds);
59 s->timeout = s->default_timeout;
60 s->max_fileno = -1;
61 sched_preselect(s);
62 s->select_ret = para_select(s->max_fileno + 1, &s->rfds,
63 &s->wfds, &s->timeout);
64 if (s->select_ret < 0)
65 return s->select_ret;
66 gettimeofday(&s->now, NULL);
67 sched_post_select(s);
68 if (list_empty(&pre_select_list) && list_empty(&post_select_list))
69 return 0;
70 goto again;
71 }
72
73 void *register_task(struct task *t)
74 {
75 PARA_INFO_LOG("registering task %p\n", t);
76 if (t->pre_select) {
77 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
78 if (t->flags & PRE_ADD_TAIL)
79 list_add_tail(&t->pre_select_node, &pre_select_list);
80 else
81 list_add(&t->pre_select_node, &pre_select_list);
82 }
83 if (t->post_select) {
84 PARA_DEBUG_LOG("post_select: %p\n", &t->pre_select);
85 if (t->flags & POST_ADD_TAIL)
86 list_add_tail(&t->post_select_node, &post_select_list);
87 else
88 list_add(&t->post_select_node, &post_select_list);
89 }
90 return t;
91 }
92
93 void unregister_task(struct task *t)
94 {
95 PARA_INFO_LOG("unregistering task %p\n", t);
96 if (t->pre_select)
97 list_del(&t->pre_select_node);
98 if (t->post_select)
99 list_del(&t->post_select_node);
100 };
101
102 void init_sched(void)
103 {
104 INIT_LIST_HEAD(&pre_select_list);
105 INIT_LIST_HEAD(&post_select_list);
106 };
107
108 void sched_shutdown(void)
109 {
110 struct task *t, *tmp;
111
112 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node)
113 unregister_task(t);
114 /* remove tasks which do not have a pre_select hook */
115 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node)
116 unregister_task(t);
117 };
118
119
120 //char *get_tast_list();