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