Rev 1427 | Rev 1614 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1427 | Rev 1610 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2006 Ondrej Palkovsky |
2 | * Copyright (C) 2006 Ondrej Palkovsky |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | #include <libadt/list.h> |
29 | #include <libadt/list.h> |
30 | #include <psthread.h> |
30 | #include <psthread.h> |
31 | #include <malloc.h> |
31 | #include <malloc.h> |
32 | #include <unistd.h> |
32 | #include <unistd.h> |
33 | #include <thread.h> |
33 | #include <thread.h> |
34 | #include <stdio.h> |
34 | #include <stdio.h> |
35 | #include <kernel/arch/faddr.h> |
35 | #include <kernel/arch/faddr.h> |
36 | #include <futex.h> |
36 | #include <futex.h> |
37 | #include <assert.h> |
37 | #include <assert.h> |
38 | #include <async.h> |
38 | #include <async.h> |
39 | 39 | ||
40 | #ifndef PSTHREAD_INITIAL_STACK_PAGES_NO |
40 | #ifndef PSTHREAD_INITIAL_STACK_PAGES_NO |
41 | #define PSTHREAD_INITIAL_STACK_PAGES_NO 1 |
41 | #define PSTHREAD_INITIAL_STACK_PAGES_NO 1 |
42 | #endif |
42 | #endif |
43 | 43 | ||
44 | static LIST_INITIALIZE(ready_list); |
44 | static LIST_INITIALIZE(ready_list); |
- | 45 | static LIST_INITIALIZE(serialized_list); |
|
45 | static LIST_INITIALIZE(manager_list); |
46 | static LIST_INITIALIZE(manager_list); |
46 | 47 | ||
47 | static void psthread_exit(void) __attribute__ ((noinline)); |
48 | static void psthread_exit(void) __attribute__ ((noinline)); |
48 | static void psthread_main(void); |
49 | static void psthread_main(void); |
49 | 50 | ||
50 | static atomic_t psthread_futex = FUTEX_INITIALIZER; |
51 | static atomic_t psthread_futex = FUTEX_INITIALIZER; |
- | 52 | /** Count of real threads that are in async_serialized mode */ |
|
- | 53 | static int serialized_threads; /* Protected by async_futex */ |
|
- | 54 | /** Thread-local count of serialization. If >0, we must not preempt */ |
|
- | 55 | static __thread serialization_count; |
|
- | 56 | /** Counter of threads residing in async_manager */ |
|
- | 57 | static int threads_in_manager; |
|
51 | 58 | ||
52 | /** Setup PSthread information into TCB structure */ |
59 | /** Setup PSthread information into TCB structure */ |
53 | psthread_data_t * psthread_setup() |
60 | psthread_data_t * psthread_setup() |
54 | { |
61 | { |
55 | psthread_data_t *pt; |
62 | psthread_data_t *pt; |
56 | tcb_t *tcb; |
63 | tcb_t *tcb; |
57 | 64 | ||
58 | tcb = __make_tls(); |
65 | tcb = __make_tls(); |
59 | if (!tcb) |
66 | if (!tcb) |
60 | return NULL; |
67 | return NULL; |
61 | 68 | ||
62 | pt = malloc(sizeof(*pt)); |
69 | pt = malloc(sizeof(*pt)); |
63 | if (!pt) { |
70 | if (!pt) { |
64 | __free_tls(tcb); |
71 | __free_tls(tcb); |
65 | return NULL; |
72 | return NULL; |
66 | } |
73 | } |
67 | 74 | ||
68 | tcb->pst_data = pt; |
75 | tcb->pst_data = pt; |
69 | pt->tcb = tcb; |
76 | pt->tcb = tcb; |
70 | 77 | ||
71 | return pt; |
78 | return pt; |
72 | } |
79 | } |
73 | 80 | ||
74 | void psthread_teardown(psthread_data_t *pt) |
81 | void psthread_teardown(psthread_data_t *pt) |
75 | { |
82 | { |
76 | __free_tls(pt->tcb); |
83 | __free_tls(pt->tcb); |
77 | free(pt); |
84 | free(pt); |
78 | } |
85 | } |
79 | 86 | ||
80 | /** Function to preempt to other pseudo thread without adding |
- | |
81 | * currently running pseudo thread to ready_list. |
- | |
82 | */ |
- | |
83 | void psthread_exit(void) |
- | |
84 | { |
- | |
85 | psthread_data_t *pt; |
- | |
86 | - | ||
87 | futex_down(&psthread_futex); |
- | |
88 | - | ||
89 | if (!list_empty(&ready_list)) |
- | |
90 | pt = list_get_instance(ready_list.next, psthread_data_t, link); |
- | |
91 | else if (!list_empty(&manager_list)) |
- | |
92 | pt = list_get_instance(manager_list.next, psthread_data_t, link); |
- | |
93 | else { |
- | |
94 | printf("Cannot find suitable psthread to run.\n"); |
- | |
95 | _exit(0); |
- | |
96 | } |
- | |
97 | list_remove(&pt->link); |
- | |
98 | futex_up(&psthread_futex); |
- | |
99 | - | ||
100 | context_restore(&pt->ctx); |
- | |
101 | /* Never reached */ |
- | |
102 | } |
- | |
103 | - | ||
104 | /** Function that is called on entry to new uspace thread */ |
87 | /** Function that is called on entry to new uspace thread */ |
105 | void psthread_main(void) |
88 | void psthread_main(void) |
106 | { |
89 | { |
107 | psthread_data_t *pt = __tcb_get()->pst_data; |
90 | psthread_data_t *pt = __tcb_get()->pst_data; |
108 | 91 | ||
- | 92 | serialization_count = 0; // TODO: WHY HERE? |
|
109 | pt->retval = pt->func(pt->arg); |
93 | pt->retval = pt->func(pt->arg); |
110 | 94 | ||
111 | pt->finished = 1; |
95 | pt->finished = 1; |
112 | if (pt->waiter) |
96 | if (pt->waiter) |
113 | list_append(&pt->waiter->link, &ready_list); |
97 | list_append(&pt->waiter->link, &ready_list); |
114 | 98 | ||
115 | psthread_exit(); |
99 | psthread_schedule_next_adv(PS_FROM_DEAD); |
116 | } |
100 | } |
117 | 101 | ||
118 | /** Schedule next userspace pseudo thread. |
102 | /** Schedule next userspace pseudo thread. |
119 | * |
103 | * |
120 | * If calling with PS_TO_MANAGER parameter, the async_futex should be |
104 | * If calling with PS_TO_MANAGER parameter, the async_futex should be |
121 | * held. |
105 | * held. |
122 | * |
106 | * |
123 | * @param tomanager If true, we are switching to next ready manager thread |
107 | * @param tomanager If true, we are switching to next ready manager thread |
124 | * (if none is found, thread is exited) |
108 | * (if none is found, thread is exited) |
125 | * @param frommanager If true, we are switching from manager thread |
109 | * @param frommanager If true, we are switching from manager thread |
126 | * @return 0 if there is no ready pseudo thread, 1 otherwise. |
110 | * @return 0 if there is no ready pseudo thread, 1 otherwise. |
127 | */ |
111 | */ |
128 | int psthread_schedule_next_adv(pschange_type ctype) |
112 | int psthread_schedule_next_adv(pschange_type ctype) |
129 | { |
113 | { |
130 | psthread_data_t *pt; |
114 | psthread_data_t *srcpt, *dstpt; |
131 | int retval = 0; |
115 | int retval = 0; |
132 | 116 | ||
133 | futex_down(&psthread_futex); |
117 | futex_down(&psthread_futex); |
134 | 118 | ||
135 | if (ctype == PS_PREEMPT && list_empty(&ready_list)) |
119 | if (ctype == PS_PREEMPT && list_empty(&ready_list)) |
136 | goto ret_0; |
120 | goto ret_0; |
137 | 121 | ||
- | 122 | if (ctype == PS_FROM_MANAGER) { |
|
138 | if (ctype == PS_FROM_MANAGER && list_empty(&ready_list)) { |
123 | if (list_empty(&ready_list) && list_empty(&serialized_list)) |
- | 124 | goto ret_0; |
|
- | 125 | /* Do not preempt if there is not sufficient count of thread managers */ |
|
- | 126 | if (list_empty(&serialized_list) && threads_in_manager <= serialized_threads) { |
|
139 | goto ret_0; |
127 | goto ret_0; |
- | 128 | } |
|
140 | } |
129 | } |
141 | /* If we are going to manager and none exists, create it */ |
130 | /* If we are going to manager and none exists, create it */ |
142 | while (ctype == PS_TO_MANAGER && list_empty(&manager_list)) { |
131 | if (ctype == PS_TO_MANAGER || ctype == PS_FROM_DEAD) { |
- | 132 | while (list_empty(&manager_list)) { |
|
143 | futex_up(&psthread_futex); |
133 | futex_up(&psthread_futex); |
144 | async_create_manager(); |
134 | async_create_manager(); |
145 | futex_down(&psthread_futex); |
135 | futex_down(&psthread_futex); |
- | 136 | } |
|
146 | } |
137 | } |
- | 138 | ||
- | 139 | if (ctype != PS_FROM_DEAD) { |
|
- | 140 | /* Save current state */ |
|
- | 141 | srcpt = __tcb_get()->pst_data; |
|
- | 142 | if (!context_save(&srcpt->ctx)) { |
|
- | 143 | if (serialization_count) |
|
- | 144 | srcpt->flags &= ~PSTHREAD_SERIALIZED; |
|
- | 145 | return 1; // futex_up already done here |
|
- | 146 | } |
|
147 | 147 | ||
148 | pt = __tcb_get()->pst_data; |
148 | /* Save myself to correct run list */ |
149 | if (!context_save(&pt->ctx)) |
149 | if (ctype == PS_PREEMPT) |
150 | return 1; // futex_up already done here |
150 | list_append(&srcpt->link, &ready_list); |
- | 151 | else if (ctype == PS_FROM_MANAGER) { |
|
- | 152 | list_append(&srcpt->link, &manager_list); |
|
- | 153 | threads_in_manager--; |
|
- | 154 | } /* If ctype == PS_TO_MANAGER, don't save ourselves to any list, we should |
|
- | 155 | * already be somewhere, or we will be lost */ |
|
- | 156 | } |
|
151 | 157 | ||
- | 158 | /* Choose new thread to run */ |
|
152 | if (ctype == PS_PREEMPT) |
159 | if (ctype == PS_TO_MANAGER || ctype == PS_FROM_DEAD) { |
153 | list_append(&pt->link, &ready_list); |
160 | dstpt = list_get_instance(manager_list.next,psthread_data_t, link); |
154 | else if (ctype == PS_FROM_MANAGER) |
161 | if (serialization_count && ctype == PS_TO_MANAGER) { |
- | 162 | serialized_threads++; |
|
155 | list_append(&pt->link, &manager_list); |
163 | srcpt->flags |= PSTHREAD_SERIALIZED; |
156 | 164 | } |
|
- | 165 | threads_in_manager++; |
|
- | 166 | } else { |
|
157 | if (ctype == PS_TO_MANAGER) |
167 | if (!list_empty(&serialized_list)) { |
158 | pt = list_get_instance(manager_list.next,psthread_data_t, link); |
168 | dstpt = list_get_instance(serialized_list.next, psthread_data_t, link); |
- | 169 | serialized_threads--; |
|
159 | else |
170 | } else |
160 | pt = list_get_instance(ready_list.next, psthread_data_t, link); |
171 | dstpt = list_get_instance(ready_list.next, psthread_data_t, link); |
- | 172 | } |
|
161 | list_remove(&pt->link); |
173 | list_remove(&dstpt->link); |
162 | 174 | ||
163 | futex_up(&psthread_futex); |
175 | futex_up(&psthread_futex); |
164 | context_restore(&pt->ctx); |
176 | context_restore(&dstpt->ctx); |
165 | 177 | ||
166 | ret_0: |
178 | ret_0: |
167 | futex_up(&psthread_futex); |
179 | futex_up(&psthread_futex); |
168 | return retval; |
180 | return retval; |
169 | } |
181 | } |
170 | 182 | ||
171 | /** Wait for uspace pseudo thread to finish. |
183 | /** Wait for uspace pseudo thread to finish. |
172 | * |
184 | * |
173 | * @param psthrid Pseudo thread to wait for. |
185 | * @param psthrid Pseudo thread to wait for. |
174 | * |
186 | * |
175 | * @return Value returned by the finished thread. |
187 | * @return Value returned by the finished thread. |
176 | */ |
188 | */ |
177 | int psthread_join(pstid_t psthrid) |
189 | int psthread_join(pstid_t psthrid) |
178 | { |
190 | { |
179 | volatile psthread_data_t *pt, *mypt; |
191 | volatile psthread_data_t *pt, *mypt; |
180 | volatile int retval; |
192 | volatile int retval; |
181 | 193 | ||
182 | /* Handle psthrid = Kernel address -> it is wait for call */ |
194 | /* Handle psthrid = Kernel address -> it is wait for call */ |
183 | pt = (psthread_data_t *) psthrid; |
195 | pt = (psthread_data_t *) psthrid; |
184 | 196 | ||
185 | if (!pt->finished) { |
197 | /* TODO */ |
186 | mypt = __tcb_get()->pst_data; |
198 | printf("join unsupported\n"); |
187 | if (context_save(&((psthread_data_t *) mypt)->ctx)) { |
- | |
188 | pt->waiter = (psthread_data_t *) mypt; |
- | |
189 | psthread_exit(); |
199 | _exit(1); |
190 | } |
- | |
191 | } |
200 | |
192 | retval = pt->retval; |
201 | retval = pt->retval; |
193 | 202 | ||
194 | free(pt->stack); |
203 | free(pt->stack); |
195 | psthread_teardown((void *)pt); |
204 | psthread_teardown((void *)pt); |
196 | 205 | ||
197 | return retval; |
206 | return retval; |
198 | } |
207 | } |
199 | 208 | ||
200 | /** |
209 | /** |
201 | * Create a userspace thread |
210 | * Create a userspace thread |
202 | * |
211 | * |
203 | * @param func Pseudo thread function. |
212 | * @param func Pseudo thread function. |
204 | * @param arg Argument to pass to func. |
213 | * @param arg Argument to pass to func. |
205 | * |
214 | * |
206 | * @return 0 on failure, TLS of the new pseudo thread. |
215 | * @return 0 on failure, TLS of the new pseudo thread. |
207 | */ |
216 | */ |
208 | pstid_t psthread_create(int (*func)(void *), void *arg) |
217 | pstid_t psthread_create(int (*func)(void *), void *arg) |
209 | { |
218 | { |
210 | psthread_data_t *pt; |
219 | psthread_data_t *pt; |
211 | 220 | ||
212 | pt = psthread_setup(); |
221 | pt = psthread_setup(); |
213 | if (!pt) |
222 | if (!pt) |
214 | return 0; |
223 | return 0; |
215 | pt->stack = (char *) malloc(PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize()); |
224 | pt->stack = (char *) malloc(PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize()); |
216 | 225 | ||
217 | if (!pt->stack) { |
226 | if (!pt->stack) { |
218 | psthread_teardown(pt); |
227 | psthread_teardown(pt); |
219 | return 0; |
228 | return 0; |
220 | } |
229 | } |
221 | 230 | ||
222 | pt->arg= arg; |
231 | pt->arg= arg; |
223 | pt->func = func; |
232 | pt->func = func; |
224 | pt->finished = 0; |
233 | pt->finished = 0; |
225 | pt->waiter = NULL; |
234 | pt->waiter = NULL; |
- | 235 | pt->flags = 0; |
|
226 | 236 | ||
227 | context_save(&pt->ctx); |
237 | context_save(&pt->ctx); |
228 | context_set(&pt->ctx, FADDR(psthread_main), pt->stack, PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize(), |
238 | context_set(&pt->ctx, FADDR(psthread_main), pt->stack, PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize(), |
229 | pt->tcb); |
239 | pt->tcb); |
230 | 240 | ||
231 | return (pstid_t )pt; |
241 | return (pstid_t )pt; |
232 | } |
242 | } |
233 | 243 | ||
234 | /** Add a thread to ready list */ |
244 | /** Add a thread to ready list */ |
235 | void psthread_add_ready(pstid_t psthrid) |
245 | void psthread_add_ready(pstid_t psthrid) |
236 | { |
246 | { |
237 | psthread_data_t *pt; |
247 | psthread_data_t *pt; |
238 | 248 | ||
239 | pt = (psthread_data_t *) psthrid; |
249 | pt = (psthread_data_t *) psthrid; |
240 | futex_down(&psthread_futex); |
250 | futex_down(&psthread_futex); |
- | 251 | if ((pt->flags & PSTHREAD_SERIALIZED)) |
|
- | 252 | list_append(&pt->link, &serialized_list); |
|
- | 253 | else |
|
241 | list_append(&pt->link, &ready_list); |
254 | list_append(&pt->link, &ready_list); |
242 | futex_up(&psthread_futex); |
255 | futex_up(&psthread_futex); |
243 | } |
256 | } |
244 | 257 | ||
245 | /** Add a thread to manager list */ |
258 | /** Add a thread to manager list */ |
246 | void psthread_add_manager(pstid_t psthrid) |
259 | void psthread_add_manager(pstid_t psthrid) |
247 | { |
260 | { |
248 | psthread_data_t *pt; |
261 | psthread_data_t *pt; |
249 | 262 | ||
250 | pt = (psthread_data_t *) psthrid; |
263 | pt = (psthread_data_t *) psthrid; |
251 | 264 | ||
252 | futex_down(&psthread_futex); |
265 | futex_down(&psthread_futex); |
253 | list_append(&pt->link, &manager_list); |
266 | list_append(&pt->link, &manager_list); |
254 | futex_up(&psthread_futex); |
267 | futex_up(&psthread_futex); |
255 | } |
268 | } |
256 | 269 | ||
257 | /** Remove one manager from manager list */ |
270 | /** Remove one manager from manager list */ |
258 | void psthread_remove_manager() |
271 | void psthread_remove_manager() |
259 | { |
272 | { |
260 | futex_down(&psthread_futex); |
273 | futex_down(&psthread_futex); |
261 | if (list_empty(&manager_list)) { |
274 | if (list_empty(&manager_list)) { |
262 | futex_up(&psthread_futex); |
275 | futex_up(&psthread_futex); |
263 | return; |
276 | return; |
264 | } |
277 | } |
265 | list_remove(manager_list.next); |
278 | list_remove(manager_list.next); |
266 | futex_up(&psthread_futex); |
279 | futex_up(&psthread_futex); |
267 | } |
280 | } |
268 | 281 | ||
269 | /** Return thread id of current running thread */ |
282 | /** Return thread id of current running thread */ |
270 | pstid_t psthread_get_id(void) |
283 | pstid_t psthread_get_id(void) |
271 | { |
284 | { |
272 | return (pstid_t)__tcb_get()->pst_data; |
285 | return (pstid_t)__tcb_get()->pst_data; |
273 | } |
286 | } |
- | 287 | ||
- | 288 | /** Disable preemption |
|
- | 289 | * |
|
- | 290 | * If the thread wants to send several message in row and does not want |
|
- | 291 | * to be preempted, it should start async_serialize_start() in the beginning |
|
- | 292 | * of communication and async_serialize_end() in the end. If it is a |
|
- | 293 | * true multithreaded application, it should protect the communication channel |
|
- | 294 | * by a futex as well. Interrupt messages will can still be preempted. |
|
- | 295 | */ |
|
- | 296 | void psthread_inc_sercount(void) |
|
- | 297 | { |
|
- | 298 | serialization_count++; |
|
- | 299 | } |
|
- | 300 | ||
- | 301 | void psthread_dec_sercount(void) |
|
- | 302 | { |
|
- | 303 | serialization_count--; |
|
- | 304 | } |
|
274 | 305 |