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