Rev 1407 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1407 | Rev 1427 | ||
---|---|---|---|
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(manager_list); |
45 | static LIST_INITIALIZE(manager_list); |
46 | 46 | ||
47 | static void psthread_exit(void) __attribute__ ((noinline)); |
47 | static void psthread_exit(void) __attribute__ ((noinline)); |
48 | static void psthread_main(void); |
48 | static void psthread_main(void); |
49 | 49 | ||
50 | static atomic_t psthread_futex = FUTEX_INITIALIZER; |
50 | static atomic_t psthread_futex = FUTEX_INITIALIZER; |
51 | 51 | ||
52 | /** Setup PSthread information into TCB structure */ |
52 | /** Setup PSthread information into TCB structure */ |
53 | psthread_data_t * psthread_setup() |
53 | psthread_data_t * psthread_setup() |
54 | { |
54 | { |
55 | psthread_data_t *pt; |
55 | psthread_data_t *pt; |
56 | tcb_t *tcb; |
56 | tcb_t *tcb; |
57 | 57 | ||
58 | tcb = __make_tls(); |
58 | tcb = __make_tls(); |
59 | if (!tcb) |
59 | if (!tcb) |
60 | return NULL; |
60 | return NULL; |
61 | 61 | ||
62 | pt = malloc(sizeof(*pt)); |
62 | pt = malloc(sizeof(*pt)); |
63 | if (!pt) { |
63 | if (!pt) { |
64 | __free_tls(tcb); |
64 | __free_tls(tcb); |
65 | return NULL; |
65 | return NULL; |
66 | } |
66 | } |
67 | 67 | ||
68 | tcb->pst_data = pt; |
68 | tcb->pst_data = pt; |
69 | pt->tcb = tcb; |
69 | pt->tcb = tcb; |
70 | 70 | ||
71 | return pt; |
71 | return pt; |
72 | } |
72 | } |
73 | 73 | ||
74 | void psthread_teardown(psthread_data_t *pt) |
74 | void psthread_teardown(psthread_data_t *pt) |
75 | { |
75 | { |
76 | __free_tls(pt->tcb); |
76 | __free_tls(pt->tcb); |
77 | free(pt); |
77 | free(pt); |
78 | } |
78 | } |
79 | 79 | ||
80 | /** Function to preempt to other pseudo thread without adding |
80 | /** Function to preempt to other pseudo thread without adding |
81 | * currently running pseudo thread to ready_list. |
81 | * currently running pseudo thread to ready_list. |
82 | */ |
82 | */ |
83 | void psthread_exit(void) |
83 | void psthread_exit(void) |
84 | { |
84 | { |
85 | psthread_data_t *pt; |
85 | psthread_data_t *pt; |
86 | 86 | ||
87 | futex_down(&psthread_futex); |
87 | futex_down(&psthread_futex); |
88 | 88 | ||
89 | if (!list_empty(&ready_list)) |
89 | if (!list_empty(&ready_list)) |
90 | pt = list_get_instance(ready_list.next, psthread_data_t, link); |
90 | pt = list_get_instance(ready_list.next, psthread_data_t, link); |
91 | else if (!list_empty(&manager_list)) |
91 | else if (!list_empty(&manager_list)) |
92 | pt = list_get_instance(manager_list.next, psthread_data_t, link); |
92 | pt = list_get_instance(manager_list.next, psthread_data_t, link); |
93 | else { |
93 | else { |
94 | printf("Cannot find suitable psthread to run.\n"); |
94 | printf("Cannot find suitable psthread to run.\n"); |
95 | _exit(0); |
95 | _exit(0); |
96 | } |
96 | } |
97 | list_remove(&pt->link); |
97 | list_remove(&pt->link); |
98 | futex_up(&psthread_futex); |
98 | futex_up(&psthread_futex); |
99 | 99 | ||
100 | context_restore(&pt->ctx); |
100 | context_restore(&pt->ctx); |
101 | /* Never reached */ |
101 | /* Never reached */ |
102 | } |
102 | } |
103 | 103 | ||
104 | /** Function that is called on entry to new uspace thread */ |
104 | /** Function that is called on entry to new uspace thread */ |
105 | void psthread_main(void) |
105 | void psthread_main(void) |
106 | { |
106 | { |
107 | psthread_data_t *pt = __tcb_get()->pst_data; |
107 | psthread_data_t *pt = __tcb_get()->pst_data; |
108 | 108 | ||
109 | pt->retval = pt->func(pt->arg); |
109 | pt->retval = pt->func(pt->arg); |
110 | 110 | ||
111 | pt->finished = 1; |
111 | pt->finished = 1; |
112 | if (pt->waiter) |
112 | if (pt->waiter) |
113 | list_append(&pt->waiter->link, &ready_list); |
113 | list_append(&pt->waiter->link, &ready_list); |
114 | 114 | ||
115 | psthread_exit(); |
115 | psthread_exit(); |
116 | } |
116 | } |
117 | 117 | ||
118 | /** Schedule next userspace pseudo thread. |
118 | /** Schedule next userspace pseudo thread. |
119 | * |
119 | * |
- | 120 | * If calling with PS_TO_MANAGER parameter, the async_futex should be |
|
- | 121 | * held. |
|
- | 122 | * |
|
120 | * @param tomanager If true, we are switching to next ready manager thread |
123 | * @param tomanager If true, we are switching to next ready manager thread |
121 | * (if none is found, thread is exited) |
124 | * (if none is found, thread is exited) |
122 | * @param frommanager If true, we are switching from manager thread |
125 | * @param frommanager If true, we are switching from manager thread |
123 | * @return 0 if there is no ready pseudo thread, 1 otherwise. |
126 | * @return 0 if there is no ready pseudo thread, 1 otherwise. |
124 | */ |
127 | */ |
125 | int psthread_schedule_next_adv(pschange_type ctype) |
128 | int psthread_schedule_next_adv(pschange_type ctype) |
126 | { |
129 | { |
127 | psthread_data_t *pt; |
130 | psthread_data_t *pt; |
128 | int retval = 0; |
131 | int retval = 0; |
129 | 132 | ||
130 | futex_down(&psthread_futex); |
133 | futex_down(&psthread_futex); |
131 | 134 | ||
132 | if (ctype == PS_PREEMPT && list_empty(&ready_list)) |
135 | if (ctype == PS_PREEMPT && list_empty(&ready_list)) |
133 | goto ret_0; |
136 | goto ret_0; |
134 | 137 | ||
135 | if (ctype == PS_FROM_MANAGER && list_empty(&ready_list)) { |
138 | if (ctype == PS_FROM_MANAGER && list_empty(&ready_list)) { |
136 | goto ret_0; |
139 | goto ret_0; |
137 | } |
140 | } |
138 | /* If we are going to manager and none exists, create it */ |
141 | /* If we are going to manager and none exists, create it */ |
139 | if (ctype == PS_TO_MANAGER && list_empty(&manager_list)) |
142 | while (ctype == PS_TO_MANAGER && list_empty(&manager_list)) { |
- | 143 | futex_up(&psthread_futex); |
|
140 | async_create_manager(); |
144 | async_create_manager(); |
- | 145 | futex_down(&psthread_futex); |
|
- | 146 | } |
|
141 | 147 | ||
142 | pt = __tcb_get()->pst_data; |
148 | pt = __tcb_get()->pst_data; |
143 | if (!context_save(&pt->ctx)) |
149 | if (!context_save(&pt->ctx)) |
144 | return 1; // futex_up already done here |
150 | return 1; // futex_up already done here |
145 | 151 | ||
146 | if (ctype == PS_PREEMPT) |
152 | if (ctype == PS_PREEMPT) |
147 | list_append(&pt->link, &ready_list); |
153 | list_append(&pt->link, &ready_list); |
148 | else if (ctype == PS_FROM_MANAGER) |
154 | else if (ctype == PS_FROM_MANAGER) |
149 | list_append(&pt->link, &manager_list); |
155 | list_append(&pt->link, &manager_list); |
150 | 156 | ||
151 | if (ctype == PS_TO_MANAGER) |
157 | if (ctype == PS_TO_MANAGER) |
152 | pt = list_get_instance(manager_list.next,psthread_data_t, link); |
158 | pt = list_get_instance(manager_list.next,psthread_data_t, link); |
153 | else |
159 | else |
154 | pt = list_get_instance(ready_list.next, psthread_data_t, link); |
160 | pt = list_get_instance(ready_list.next, psthread_data_t, link); |
155 | list_remove(&pt->link); |
161 | list_remove(&pt->link); |
156 | 162 | ||
157 | futex_up(&psthread_futex); |
163 | futex_up(&psthread_futex); |
158 | context_restore(&pt->ctx); |
164 | context_restore(&pt->ctx); |
159 | 165 | ||
160 | ret_0: |
166 | ret_0: |
161 | futex_up(&psthread_futex); |
167 | futex_up(&psthread_futex); |
162 | return retval; |
168 | return retval; |
163 | } |
169 | } |
164 | 170 | ||
165 | /** Wait for uspace pseudo thread to finish. |
171 | /** Wait for uspace pseudo thread to finish. |
166 | * |
172 | * |
167 | * @param psthrid Pseudo thread to wait for. |
173 | * @param psthrid Pseudo thread to wait for. |
168 | * |
174 | * |
169 | * @return Value returned by the finished thread. |
175 | * @return Value returned by the finished thread. |
170 | */ |
176 | */ |
171 | int psthread_join(pstid_t psthrid) |
177 | int psthread_join(pstid_t psthrid) |
172 | { |
178 | { |
173 | volatile psthread_data_t *pt, *mypt; |
179 | volatile psthread_data_t *pt, *mypt; |
174 | volatile int retval; |
180 | volatile int retval; |
175 | 181 | ||
176 | /* Handle psthrid = Kernel address -> it is wait for call */ |
182 | /* Handle psthrid = Kernel address -> it is wait for call */ |
177 | pt = (psthread_data_t *) psthrid; |
183 | pt = (psthread_data_t *) psthrid; |
178 | 184 | ||
179 | if (!pt->finished) { |
185 | if (!pt->finished) { |
180 | mypt = __tcb_get()->pst_data; |
186 | mypt = __tcb_get()->pst_data; |
181 | if (context_save(&((psthread_data_t *) mypt)->ctx)) { |
187 | if (context_save(&((psthread_data_t *) mypt)->ctx)) { |
182 | pt->waiter = (psthread_data_t *) mypt; |
188 | pt->waiter = (psthread_data_t *) mypt; |
183 | psthread_exit(); |
189 | psthread_exit(); |
184 | } |
190 | } |
185 | } |
191 | } |
186 | retval = pt->retval; |
192 | retval = pt->retval; |
187 | 193 | ||
188 | free(pt->stack); |
194 | free(pt->stack); |
189 | psthread_teardown((void *)pt); |
195 | psthread_teardown((void *)pt); |
190 | 196 | ||
191 | return retval; |
197 | return retval; |
192 | } |
198 | } |
193 | 199 | ||
194 | /** |
200 | /** |
195 | * Create a userspace thread |
201 | * Create a userspace thread |
196 | * |
202 | * |
197 | * @param func Pseudo thread function. |
203 | * @param func Pseudo thread function. |
198 | * @param arg Argument to pass to func. |
204 | * @param arg Argument to pass to func. |
199 | * |
205 | * |
200 | * @return 0 on failure, TLS of the new pseudo thread. |
206 | * @return 0 on failure, TLS of the new pseudo thread. |
201 | */ |
207 | */ |
202 | pstid_t psthread_create(int (*func)(void *), void *arg) |
208 | pstid_t psthread_create(int (*func)(void *), void *arg) |
203 | { |
209 | { |
204 | psthread_data_t *pt; |
210 | psthread_data_t *pt; |
205 | 211 | ||
206 | pt = psthread_setup(); |
212 | pt = psthread_setup(); |
207 | if (!pt) |
213 | if (!pt) |
208 | return 0; |
214 | return 0; |
209 | pt->stack = (char *) malloc(PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize()); |
215 | pt->stack = (char *) malloc(PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize()); |
210 | 216 | ||
211 | if (!pt->stack) { |
217 | if (!pt->stack) { |
212 | psthread_teardown(pt); |
218 | psthread_teardown(pt); |
213 | return 0; |
219 | return 0; |
214 | } |
220 | } |
215 | 221 | ||
216 | pt->arg= arg; |
222 | pt->arg= arg; |
217 | pt->func = func; |
223 | pt->func = func; |
218 | pt->finished = 0; |
224 | pt->finished = 0; |
219 | pt->waiter = NULL; |
225 | pt->waiter = NULL; |
220 | 226 | ||
221 | context_save(&pt->ctx); |
227 | context_save(&pt->ctx); |
222 | context_set(&pt->ctx, FADDR(psthread_main), pt->stack, PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize(), |
228 | context_set(&pt->ctx, FADDR(psthread_main), pt->stack, PSTHREAD_INITIAL_STACK_PAGES_NO*getpagesize(), |
223 | pt->tcb); |
229 | pt->tcb); |
224 | 230 | ||
225 | return (pstid_t )pt; |
231 | return (pstid_t )pt; |
226 | } |
232 | } |
227 | 233 | ||
228 | /** Add a thread to ready list */ |
234 | /** Add a thread to ready list */ |
229 | void psthread_add_ready(pstid_t psthrid) |
235 | void psthread_add_ready(pstid_t psthrid) |
230 | { |
236 | { |
231 | psthread_data_t *pt; |
237 | psthread_data_t *pt; |
232 | 238 | ||
233 | pt = (psthread_data_t *) psthrid; |
239 | pt = (psthread_data_t *) psthrid; |
234 | futex_down(&psthread_futex); |
240 | futex_down(&psthread_futex); |
235 | list_append(&pt->link, &ready_list); |
241 | list_append(&pt->link, &ready_list); |
236 | futex_up(&psthread_futex); |
242 | futex_up(&psthread_futex); |
237 | } |
243 | } |
238 | 244 | ||
239 | /** Add a thread to manager list */ |
245 | /** Add a thread to manager list */ |
240 | void psthread_add_manager(pstid_t psthrid) |
246 | void psthread_add_manager(pstid_t psthrid) |
241 | { |
247 | { |
242 | psthread_data_t *pt; |
248 | psthread_data_t *pt; |
243 | 249 | ||
244 | pt = (psthread_data_t *) psthrid; |
250 | pt = (psthread_data_t *) psthrid; |
245 | 251 | ||
246 | futex_down(&psthread_futex); |
252 | futex_down(&psthread_futex); |
247 | list_append(&pt->link, &manager_list); |
253 | list_append(&pt->link, &manager_list); |
248 | futex_up(&psthread_futex); |
254 | futex_up(&psthread_futex); |
249 | } |
255 | } |
250 | 256 | ||
251 | /** Remove one manager from manager list */ |
257 | /** Remove one manager from manager list */ |
252 | void psthread_remove_manager() |
258 | void psthread_remove_manager() |
253 | { |
259 | { |
254 | futex_down(&psthread_futex); |
260 | futex_down(&psthread_futex); |
255 | if (list_empty(&manager_list)) { |
261 | if (list_empty(&manager_list)) { |
256 | futex_up(&psthread_futex); |
262 | futex_up(&psthread_futex); |
257 | return; |
263 | return; |
258 | } |
264 | } |
259 | list_remove(manager_list.next); |
265 | list_remove(manager_list.next); |
260 | futex_up(&psthread_futex); |
266 | futex_up(&psthread_futex); |
261 | } |
267 | } |
- | 268 | ||
- | 269 | /** Return thread id of current running thread */ |
|
- | 270 | pstid_t psthread_get_id(void) |
|
- | 271 | { |
|
- | 272 | return (pstid_t)__tcb_get()->pst_data; |
|
- | 273 | } |
|
262 | 274 |