879ab80820e56742815044d158f25aec965359db
[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 // PARA_INFO_LOG("%s \n", t->status);
20 if (t->ret > 0)
21 continue;
22 if (!t->event_handler)
23 continue;
24 t->event_handler(t);
25 goto again;
26 }
27 }
28
29 static void sched_post_select(struct sched *s)
30 {
31 struct task *t, *tmp;
32
33 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
34 t->post_select(s, t);
35 // PARA_INFO_LOG("%s: %d\n", t->status, t->ret);
36 if (t->ret > 0 || !t->event_handler)
37 continue;
38 t->event_handler(t);
39 }
40 }
41
42 int sched(struct sched *s)
43 {
44
45 gettimeofday(&s->now, NULL);
46 again:
47 FD_ZERO(&s->rfds);
48 FD_ZERO(&s->wfds);
49 s->timeout = s->default_timeout;
50 s->max_fileno = -1;
51 sched_preselect(s);
52 s->select_ret = para_select(s->max_fileno + 1, &s->rfds,
53 &s->wfds, &s->timeout);
54 if (s->select_ret < 0)
55 return s->select_ret;
56 gettimeofday(&s->now, NULL);
57 sched_post_select(s);
58 if (list_empty(&pre_select_list) && list_empty(&post_select_list))
59 return 0;
60 goto again;
61 }
62
63 void *register_task(struct task *t)
64 {
65 PARA_INFO_LOG("registering %s (%p)\n", t->status, t);
66 if (t->pre_select) {
67 PARA_DEBUG_LOG("pre_select: %p\n", &t->pre_select);
68 if (t->flags & PRE_ADD_TAIL)
69 list_add_tail(&t->pre_select_node, &pre_select_list);
70 else
71 list_add(&t->pre_select_node, &pre_select_list);
72 }
73 if (t->post_select) {
74 PARA_DEBUG_LOG("post_select: %p\n", &t->pre_select);
75 if (t->flags & POST_ADD_TAIL)
76 list_add_tail(&t->post_select_node, &post_select_list);
77 else
78 list_add(&t->post_select_node, &post_select_list);
79 }
80 return t;
81 }
82
83 void unregister_task(struct task *t)
84 {
85 PARA_INFO_LOG("unregistering %s (%p)\n", t->status, t);
86 if (t->pre_select)
87 list_del(&t->pre_select_node);
88 if (t->post_select)
89 list_del(&t->post_select_node);
90 };
91
92 void init_sched(void)
93 {
94 INIT_LIST_HEAD(&pre_select_list);
95 INIT_LIST_HEAD(&post_select_list);
96 };
97
98 void sched_shutdown(void)
99 {
100 struct task *t, *tmp;
101
102 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node)
103 unregister_task(t);
104 /* remove tasks which do not have a pre_select hook */
105 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node)
106 unregister_task(t);
107 };
108
109 char *get_task_list(void)
110 {
111 struct task *t, *tmp;
112 char *msg = NULL;
113 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
114 char *tmp_msg;
115 tmp_msg = make_message("%s%p\t%s\n", msg? msg : "", t, t->status);
116 free(msg);
117 msg = tmp_msg;
118 }
119 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
120 char *tmp_msg;
121 if (t->pre_select)
122 continue;
123 tmp_msg = make_message("%s%p\t%s\n", msg? msg : "", t, t->status);
124 free(msg);
125 msg = tmp_msg;
126 }
127 //PARA_DEBUG_LOG("task list:\n%s", msg);
128 return msg;
129 }
130
131 int kill_task(char *id)
132 {
133 struct task *t, *tmp;
134 char buf[20];
135 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
136 sprintf(buf, "%p", t);
137 if (strcmp(id, buf))
138 continue;
139 t->ret = -E_TASK_KILLED;
140 if (t->event_handler)
141 t->event_handler(t);
142 return 1;
143 }
144 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
145 sprintf(buf, "%p", t);
146 if (strcmp(id, buf))
147 continue;
148 t->ret = -E_TASK_KILLED;
149 if (t->event_handler)
150 t->event_handler(t);
151 return 1;
152 }
153 return -E_NO_SUCH_TASK;
154 }