27d71f67d40fd69614e7f8c19bd0f409fb175aca
[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 list_add(&t->pre_select_node, &pre_select_list);
69 }
70 if (t->post_select) {
71 PARA_DEBUG_LOG("post_select: %p\n", &t->pre_select);
72 list_add(&t->post_select_node, &post_select_list);
73 }
74 return t;
75 }
76
77 void unregister_task(struct task *t)
78 {
79 PARA_INFO_LOG("unregistering %s (%p)\n", t->status, t);
80 if (t->pre_select)
81 list_del(&t->pre_select_node);
82 if (t->post_select)
83 list_del(&t->post_select_node);
84 };
85
86 void init_sched(void)
87 {
88 INIT_LIST_HEAD(&pre_select_list);
89 INIT_LIST_HEAD(&post_select_list);
90 };
91
92 void sched_shutdown(void)
93 {
94 struct task *t, *tmp;
95
96 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node)
97 unregister_task(t);
98 /* remove tasks which do not have a pre_select hook */
99 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node)
100 unregister_task(t);
101 };
102
103 char *get_task_list(void)
104 {
105 struct task *t, *tmp;
106 char *msg = NULL;
107 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
108 char *tmp_msg;
109 tmp_msg = make_message("%s%p\tpre\t%s\n", msg? msg : "", t, t->status);
110 free(msg);
111 msg = tmp_msg;
112 }
113 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
114 char *tmp_msg;
115 // if (t->pre_select)
116 // continue;
117 tmp_msg = make_message("%s%p\tpost\t%s\n", msg? msg : "", t, t->status);
118 free(msg);
119 msg = tmp_msg;
120 }
121 //PARA_DEBUG_LOG("task list:\n%s", msg);
122 return msg;
123 }
124
125 int kill_task(char *id)
126 {
127 struct task *t, *tmp;
128 char buf[20];
129 list_for_each_entry_safe(t, tmp, &pre_select_list, pre_select_node) {
130 sprintf(buf, "%p", t);
131 if (strcmp(id, buf))
132 continue;
133 t->ret = -E_TASK_KILLED;
134 if (t->event_handler)
135 t->event_handler(t);
136 return 1;
137 }
138 list_for_each_entry_safe(t, tmp, &post_select_list, post_select_node) {
139 sprintf(buf, "%p", t);
140 if (strcmp(id, buf))
141 continue;
142 t->ret = -E_TASK_KILLED;
143 if (t->event_handler)
144 t->event_handler(t);
145 return 1;
146 }
147 return -E_NO_SUCH_TASK;
148 }