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