Rev 1066 | Rev 1083 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1066 | Rev 1078 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
2 | * Copyright (C) 2001-2004 Jakub Jermar |
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 <proc/scheduler.h> |
29 | #include <proc/scheduler.h> |
30 | #include <proc/thread.h> |
30 | #include <proc/thread.h> |
31 | #include <proc/task.h> |
31 | #include <proc/task.h> |
- | 32 | #include <proc/uarg.h> |
|
32 | #include <mm/frame.h> |
33 | #include <mm/frame.h> |
33 | #include <mm/page.h> |
34 | #include <mm/page.h> |
34 | #include <arch/asm.h> |
35 | #include <arch/asm.h> |
35 | #include <arch.h> |
36 | #include <arch.h> |
36 | #include <synch/synch.h> |
37 | #include <synch/synch.h> |
37 | #include <synch/spinlock.h> |
38 | #include <synch/spinlock.h> |
38 | #include <synch/waitq.h> |
39 | #include <synch/waitq.h> |
39 | #include <synch/rwlock.h> |
40 | #include <synch/rwlock.h> |
40 | #include <cpu.h> |
41 | #include <cpu.h> |
41 | #include <func.h> |
42 | #include <func.h> |
42 | #include <context.h> |
43 | #include <context.h> |
43 | #include <adt/list.h> |
44 | #include <adt/list.h> |
44 | #include <typedefs.h> |
45 | #include <typedefs.h> |
45 | #include <time/clock.h> |
46 | #include <time/clock.h> |
46 | #include <adt/list.h> |
47 | #include <adt/list.h> |
47 | #include <config.h> |
48 | #include <config.h> |
48 | #include <arch/interrupt.h> |
49 | #include <arch/interrupt.h> |
49 | #include <smp/ipi.h> |
50 | #include <smp/ipi.h> |
50 | #include <arch/faddr.h> |
51 | #include <arch/faddr.h> |
51 | #include <arch/atomic.h> |
52 | #include <arch/atomic.h> |
52 | #include <memstr.h> |
53 | #include <memstr.h> |
53 | #include <print.h> |
54 | #include <print.h> |
54 | #include <mm/slab.h> |
55 | #include <mm/slab.h> |
55 | #include <debug.h> |
56 | #include <debug.h> |
56 | #include <main/uinit.h> |
57 | #include <main/uinit.h> |
57 | 58 | ||
58 | char *thread_states[] = {"Invalid", "Running", "Sleeping", "Ready", "Entering", "Exiting"}; /**< Thread states */ |
59 | char *thread_states[] = {"Invalid", "Running", "Sleeping", "Ready", "Entering", "Exiting"}; /**< Thread states */ |
59 | 60 | ||
60 | SPINLOCK_INITIALIZE(threads_lock); /**< Lock protecting threads_head list. For locking rules, see declaration thereof. */ |
61 | SPINLOCK_INITIALIZE(threads_lock); /**< Lock protecting threads_head list. For locking rules, see declaration thereof. */ |
61 | LIST_INITIALIZE(threads_head); /**< List of all threads. */ |
62 | LIST_INITIALIZE(threads_head); /**< List of all threads. */ |
62 | 63 | ||
63 | SPINLOCK_INITIALIZE(tidlock); |
64 | SPINLOCK_INITIALIZE(tidlock); |
64 | __u32 last_tid = 0; |
65 | __u32 last_tid = 0; |
65 | 66 | ||
66 | static slab_cache_t *thread_slab; |
67 | static slab_cache_t *thread_slab; |
67 | #ifdef ARCH_HAS_FPU |
68 | #ifdef ARCH_HAS_FPU |
68 | slab_cache_t *fpu_context_slab; |
69 | slab_cache_t *fpu_context_slab; |
69 | #endif |
70 | #endif |
70 | 71 | ||
71 | 72 | ||
72 | /** Thread wrapper |
73 | /** Thread wrapper |
73 | * |
74 | * |
74 | * This wrapper is provided to ensure that every thread |
75 | * This wrapper is provided to ensure that every thread |
75 | * makes a call to thread_exit() when its implementing |
76 | * makes a call to thread_exit() when its implementing |
76 | * function returns. |
77 | * function returns. |
77 | * |
78 | * |
78 | * interrupts_disable() is assumed. |
79 | * interrupts_disable() is assumed. |
79 | * |
80 | * |
80 | */ |
81 | */ |
81 | static void cushion(void) |
82 | static void cushion(void) |
82 | { |
83 | { |
83 | void (*f)(void *) = THREAD->thread_code; |
84 | void (*f)(void *) = THREAD->thread_code; |
84 | void *arg = THREAD->thread_arg; |
85 | void *arg = THREAD->thread_arg; |
85 | 86 | ||
86 | /* this is where each thread wakes up after its creation */ |
87 | /* this is where each thread wakes up after its creation */ |
87 | before_thread_runs(); |
88 | before_thread_runs(); |
88 | 89 | ||
89 | spinlock_unlock(&THREAD->lock); |
90 | spinlock_unlock(&THREAD->lock); |
90 | interrupts_enable(); |
91 | interrupts_enable(); |
91 | 92 | ||
92 | f(arg); |
93 | f(arg); |
93 | thread_exit(); |
94 | thread_exit(); |
94 | /* not reached */ |
95 | /* not reached */ |
95 | } |
96 | } |
96 | 97 | ||
97 | /** Initialization and allocation for thread_t structure */ |
98 | /** Initialization and allocation for thread_t structure */ |
98 | static int thr_constructor(void *obj, int kmflags) |
99 | static int thr_constructor(void *obj, int kmflags) |
99 | { |
100 | { |
100 | thread_t *t = (thread_t *)obj; |
101 | thread_t *t = (thread_t *)obj; |
101 | pfn_t pfn; |
102 | pfn_t pfn; |
102 | int status; |
103 | int status; |
103 | 104 | ||
104 | spinlock_initialize(&t->lock, "thread_t_lock"); |
105 | spinlock_initialize(&t->lock, "thread_t_lock"); |
105 | link_initialize(&t->rq_link); |
106 | link_initialize(&t->rq_link); |
106 | link_initialize(&t->wq_link); |
107 | link_initialize(&t->wq_link); |
107 | link_initialize(&t->th_link); |
108 | link_initialize(&t->th_link); |
108 | link_initialize(&t->threads_link); |
109 | link_initialize(&t->threads_link); |
109 | 110 | ||
110 | #ifdef ARCH_HAS_FPU |
111 | #ifdef ARCH_HAS_FPU |
111 | # ifdef CONFIG_FPU_LAZY |
112 | # ifdef CONFIG_FPU_LAZY |
112 | t->saved_fpu_context = NULL; |
113 | t->saved_fpu_context = NULL; |
113 | # else |
114 | # else |
114 | t->saved_fpu_context = slab_alloc(fpu_context_slab,kmflags); |
115 | t->saved_fpu_context = slab_alloc(fpu_context_slab,kmflags); |
115 | if (!t->saved_fpu_context) |
116 | if (!t->saved_fpu_context) |
116 | return -1; |
117 | return -1; |
117 | # endif |
118 | # endif |
118 | #endif |
119 | #endif |
119 | 120 | ||
120 | pfn = frame_alloc_rc(STACK_FRAMES, FRAME_KA | kmflags,&status); |
121 | pfn = frame_alloc_rc(STACK_FRAMES, FRAME_KA | kmflags,&status); |
121 | if (status) { |
122 | if (status) { |
122 | #ifdef ARCH_HAS_FPU |
123 | #ifdef ARCH_HAS_FPU |
123 | if (t->saved_fpu_context) |
124 | if (t->saved_fpu_context) |
124 | slab_free(fpu_context_slab,t->saved_fpu_context); |
125 | slab_free(fpu_context_slab,t->saved_fpu_context); |
125 | #endif |
126 | #endif |
126 | return -1; |
127 | return -1; |
127 | } |
128 | } |
128 | t->kstack = (__u8 *)PA2KA(PFN2ADDR(pfn)); |
129 | t->kstack = (__u8 *)PA2KA(PFN2ADDR(pfn)); |
129 | 130 | ||
130 | return 0; |
131 | return 0; |
131 | } |
132 | } |
132 | 133 | ||
133 | /** Destruction of thread_t object */ |
134 | /** Destruction of thread_t object */ |
134 | static int thr_destructor(void *obj) |
135 | static int thr_destructor(void *obj) |
135 | { |
136 | { |
136 | thread_t *t = (thread_t *)obj; |
137 | thread_t *t = (thread_t *)obj; |
137 | 138 | ||
138 | frame_free(ADDR2PFN(KA2PA(t->kstack))); |
139 | frame_free(ADDR2PFN(KA2PA(t->kstack))); |
139 | #ifdef ARCH_HAS_FPU |
140 | #ifdef ARCH_HAS_FPU |
140 | if (t->saved_fpu_context) |
141 | if (t->saved_fpu_context) |
141 | slab_free(fpu_context_slab,t->saved_fpu_context); |
142 | slab_free(fpu_context_slab,t->saved_fpu_context); |
142 | #endif |
143 | #endif |
143 | return 1; /* One page freed */ |
144 | return 1; /* One page freed */ |
144 | } |
145 | } |
145 | 146 | ||
146 | /** Initialize threads |
147 | /** Initialize threads |
147 | * |
148 | * |
148 | * Initialize kernel threads support. |
149 | * Initialize kernel threads support. |
149 | * |
150 | * |
150 | */ |
151 | */ |
151 | void thread_init(void) |
152 | void thread_init(void) |
152 | { |
153 | { |
153 | THREAD = NULL; |
154 | THREAD = NULL; |
154 | atomic_set(&nrdy,0); |
155 | atomic_set(&nrdy,0); |
155 | thread_slab = slab_cache_create("thread_slab", |
156 | thread_slab = slab_cache_create("thread_slab", |
156 | sizeof(thread_t),0, |
157 | sizeof(thread_t),0, |
157 | thr_constructor, thr_destructor, 0); |
158 | thr_constructor, thr_destructor, 0); |
158 | #ifdef ARCH_HAS_FPU |
159 | #ifdef ARCH_HAS_FPU |
159 | fpu_context_slab = slab_cache_create("fpu_slab", |
160 | fpu_context_slab = slab_cache_create("fpu_slab", |
160 | sizeof(fpu_context_t), |
161 | sizeof(fpu_context_t), |
161 | FPU_CONTEXT_ALIGN, |
162 | FPU_CONTEXT_ALIGN, |
162 | NULL, NULL, 0); |
163 | NULL, NULL, 0); |
163 | #endif |
164 | #endif |
164 | } |
165 | } |
165 | 166 | ||
166 | 167 | ||
167 | /** Make thread ready |
168 | /** Make thread ready |
168 | * |
169 | * |
169 | * Switch thread t to the ready state. |
170 | * Switch thread t to the ready state. |
170 | * |
171 | * |
171 | * @param t Thread to make ready. |
172 | * @param t Thread to make ready. |
172 | * |
173 | * |
173 | */ |
174 | */ |
174 | void thread_ready(thread_t *t) |
175 | void thread_ready(thread_t *t) |
175 | { |
176 | { |
176 | cpu_t *cpu; |
177 | cpu_t *cpu; |
177 | runq_t *r; |
178 | runq_t *r; |
178 | ipl_t ipl; |
179 | ipl_t ipl; |
179 | int i, avg; |
180 | int i, avg; |
180 | 181 | ||
181 | ipl = interrupts_disable(); |
182 | ipl = interrupts_disable(); |
182 | 183 | ||
183 | spinlock_lock(&t->lock); |
184 | spinlock_lock(&t->lock); |
184 | 185 | ||
185 | i = (t->priority < RQ_COUNT -1) ? ++t->priority : t->priority; |
186 | i = (t->priority < RQ_COUNT -1) ? ++t->priority : t->priority; |
186 | 187 | ||
187 | cpu = CPU; |
188 | cpu = CPU; |
188 | if (t->flags & X_WIRED) { |
189 | if (t->flags & X_WIRED) { |
189 | cpu = t->cpu; |
190 | cpu = t->cpu; |
190 | } |
191 | } |
191 | spinlock_unlock(&t->lock); |
192 | spinlock_unlock(&t->lock); |
192 | 193 | ||
193 | /* |
194 | /* |
194 | * Append t to respective ready queue on respective processor. |
195 | * Append t to respective ready queue on respective processor. |
195 | */ |
196 | */ |
196 | r = &cpu->rq[i]; |
197 | r = &cpu->rq[i]; |
197 | spinlock_lock(&r->lock); |
198 | spinlock_lock(&r->lock); |
198 | list_append(&t->rq_link, &r->rq_head); |
199 | list_append(&t->rq_link, &r->rq_head); |
199 | r->n++; |
200 | r->n++; |
200 | spinlock_unlock(&r->lock); |
201 | spinlock_unlock(&r->lock); |
201 | 202 | ||
202 | atomic_inc(&nrdy); |
203 | atomic_inc(&nrdy); |
203 | avg = atomic_get(&nrdy) / config.cpu_active; |
204 | avg = atomic_get(&nrdy) / config.cpu_active; |
204 | atomic_inc(&cpu->nrdy); |
205 | atomic_inc(&cpu->nrdy); |
205 | 206 | ||
206 | interrupts_restore(ipl); |
207 | interrupts_restore(ipl); |
207 | } |
208 | } |
208 | 209 | ||
209 | 210 | ||
210 | /** Destroy thread memory structure |
211 | /** Destroy thread memory structure |
211 | * |
212 | * |
212 | * Detach thread from all queues, cpus etc. and destroy it. |
213 | * Detach thread from all queues, cpus etc. and destroy it. |
213 | * |
214 | * |
214 | * Assume thread->lock is held!! |
215 | * Assume thread->lock is held!! |
215 | */ |
216 | */ |
216 | void thread_destroy(thread_t *t) |
217 | void thread_destroy(thread_t *t) |
217 | { |
218 | { |
218 | ASSERT(t->state == Exiting); |
219 | ASSERT(t->state == Exiting); |
219 | ASSERT(t->task); |
220 | ASSERT(t->task); |
220 | ASSERT(t->cpu); |
221 | ASSERT(t->cpu); |
221 | 222 | ||
222 | spinlock_lock(&t->cpu->lock); |
223 | spinlock_lock(&t->cpu->lock); |
223 | if(t->cpu->fpu_owner==t) |
224 | if(t->cpu->fpu_owner==t) |
224 | t->cpu->fpu_owner=NULL; |
225 | t->cpu->fpu_owner=NULL; |
225 | spinlock_unlock(&t->cpu->lock); |
226 | spinlock_unlock(&t->cpu->lock); |
226 | 227 | ||
227 | /* |
228 | /* |
228 | * Detach from the containing task. |
229 | * Detach from the containing task. |
229 | */ |
230 | */ |
230 | spinlock_lock(&t->task->lock); |
231 | spinlock_lock(&t->task->lock); |
231 | list_remove(&t->th_link); |
232 | list_remove(&t->th_link); |
232 | spinlock_unlock(&t->task->lock); |
233 | spinlock_unlock(&t->task->lock); |
233 | 234 | ||
234 | spinlock_unlock(&t->lock); |
235 | spinlock_unlock(&t->lock); |
235 | 236 | ||
236 | spinlock_lock(&threads_lock); |
237 | spinlock_lock(&threads_lock); |
237 | list_remove(&t->threads_link); |
238 | list_remove(&t->threads_link); |
238 | spinlock_unlock(&threads_lock); |
239 | spinlock_unlock(&threads_lock); |
239 | 240 | ||
240 | slab_free(thread_slab, t); |
241 | slab_free(thread_slab, t); |
241 | } |
242 | } |
242 | 243 | ||
243 | 244 | ||
244 | /** Create new thread |
245 | /** Create new thread |
245 | * |
246 | * |
246 | * Create a new thread. |
247 | * Create a new thread. |
247 | * |
248 | * |
248 | * @param func Thread's implementing function. |
249 | * @param func Thread's implementing function. |
249 | * @param arg Thread's implementing function argument. |
250 | * @param arg Thread's implementing function argument. |
250 | * @param task Task to which the thread belongs. |
251 | * @param task Task to which the thread belongs. |
251 | * @param flags Thread flags. |
252 | * @param flags Thread flags. |
252 | * @param name Symbolic name. |
253 | * @param name Symbolic name. |
253 | * |
254 | * |
254 | * @return New thread's structure on success, NULL on failure. |
255 | * @return New thread's structure on success, NULL on failure. |
255 | * |
256 | * |
256 | */ |
257 | */ |
257 | thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, int flags, char *name) |
258 | thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, int flags, char *name) |
258 | { |
259 | { |
259 | thread_t *t; |
260 | thread_t *t; |
260 | ipl_t ipl; |
261 | ipl_t ipl; |
261 | 262 | ||
262 | t = (thread_t *) slab_alloc(thread_slab, 0); |
263 | t = (thread_t *) slab_alloc(thread_slab, 0); |
263 | if (!t) |
264 | if (!t) |
264 | return NULL; |
265 | return NULL; |
265 | 266 | ||
266 | /* Not needed, but good for debugging */ |
267 | /* Not needed, but good for debugging */ |
267 | memsetb((__address)t->kstack, THREAD_STACK_SIZE, 0); |
268 | memsetb((__address)t->kstack, THREAD_STACK_SIZE, 0); |
268 | 269 | ||
269 | ipl = interrupts_disable(); |
270 | ipl = interrupts_disable(); |
270 | spinlock_lock(&tidlock); |
271 | spinlock_lock(&tidlock); |
271 | t->tid = ++last_tid; |
272 | t->tid = ++last_tid; |
272 | spinlock_unlock(&tidlock); |
273 | spinlock_unlock(&tidlock); |
273 | interrupts_restore(ipl); |
274 | interrupts_restore(ipl); |
274 | 275 | ||
275 | context_save(&t->saved_context); |
276 | context_save(&t->saved_context); |
276 | context_set(&t->saved_context, FADDR(cushion), (__address) t->kstack, THREAD_STACK_SIZE); |
277 | context_set(&t->saved_context, FADDR(cushion), (__address) t->kstack, THREAD_STACK_SIZE); |
277 | 278 | ||
278 | the_initialize((the_t *) t->kstack); |
279 | the_initialize((the_t *) t->kstack); |
279 | 280 | ||
280 | ipl = interrupts_disable(); |
281 | ipl = interrupts_disable(); |
281 | t->saved_context.ipl = interrupts_read(); |
282 | t->saved_context.ipl = interrupts_read(); |
282 | interrupts_restore(ipl); |
283 | interrupts_restore(ipl); |
283 | 284 | ||
284 | memcpy(t->name, name, THREAD_NAME_BUFLEN); |
285 | memcpy(t->name, name, THREAD_NAME_BUFLEN); |
285 | 286 | ||
286 | t->thread_code = func; |
287 | t->thread_code = func; |
287 | t->thread_arg = arg; |
288 | t->thread_arg = arg; |
288 | t->ticks = -1; |
289 | t->ticks = -1; |
289 | t->priority = -1; /* start in rq[0] */ |
290 | t->priority = -1; /* start in rq[0] */ |
290 | t->cpu = NULL; |
291 | t->cpu = NULL; |
291 | t->flags = 0; |
292 | t->flags = 0; |
292 | t->state = Entering; |
293 | t->state = Entering; |
293 | t->call_me = NULL; |
294 | t->call_me = NULL; |
294 | t->call_me_with = NULL; |
295 | t->call_me_with = NULL; |
295 | 296 | ||
296 | timeout_initialize(&t->sleep_timeout); |
297 | timeout_initialize(&t->sleep_timeout); |
297 | t->sleep_queue = NULL; |
298 | t->sleep_queue = NULL; |
298 | t->timeout_pending = 0; |
299 | t->timeout_pending = 0; |
299 | 300 | ||
300 | t->rwlock_holder_type = RWLOCK_NONE; |
301 | t->rwlock_holder_type = RWLOCK_NONE; |
301 | 302 | ||
302 | t->task = task; |
303 | t->task = task; |
303 | 304 | ||
304 | t->fpu_context_exists = 0; |
305 | t->fpu_context_exists = 0; |
305 | t->fpu_context_engaged = 0; |
306 | t->fpu_context_engaged = 0; |
306 | 307 | ||
307 | /* |
308 | /* |
308 | * Register this thread in the system-wide list. |
309 | * Register this thread in the system-wide list. |
309 | */ |
310 | */ |
310 | ipl = interrupts_disable(); |
311 | ipl = interrupts_disable(); |
311 | spinlock_lock(&threads_lock); |
312 | spinlock_lock(&threads_lock); |
312 | list_append(&t->threads_link, &threads_head); |
313 | list_append(&t->threads_link, &threads_head); |
313 | spinlock_unlock(&threads_lock); |
314 | spinlock_unlock(&threads_lock); |
314 | 315 | ||
315 | /* |
316 | /* |
316 | * Attach to the containing task. |
317 | * Attach to the containing task. |
317 | */ |
318 | */ |
318 | spinlock_lock(&task->lock); |
319 | spinlock_lock(&task->lock); |
319 | list_append(&t->th_link, &task->th_head); |
320 | list_append(&t->th_link, &task->th_head); |
320 | spinlock_unlock(&task->lock); |
321 | spinlock_unlock(&task->lock); |
321 | 322 | ||
322 | interrupts_restore(ipl); |
323 | interrupts_restore(ipl); |
323 | 324 | ||
324 | return t; |
325 | return t; |
325 | } |
326 | } |
326 | 327 | ||
327 | 328 | ||
328 | /** Make thread exiting |
329 | /** Make thread exiting |
329 | * |
330 | * |
330 | * End current thread execution and switch it to the exiting |
331 | * End current thread execution and switch it to the exiting |
331 | * state. All pending timeouts are executed. |
332 | * state. All pending timeouts are executed. |
332 | * |
333 | * |
333 | */ |
334 | */ |
334 | void thread_exit(void) |
335 | void thread_exit(void) |
335 | { |
336 | { |
336 | ipl_t ipl; |
337 | ipl_t ipl; |
337 | 338 | ||
338 | restart: |
339 | restart: |
339 | ipl = interrupts_disable(); |
340 | ipl = interrupts_disable(); |
340 | spinlock_lock(&THREAD->lock); |
341 | spinlock_lock(&THREAD->lock); |
341 | if (THREAD->timeout_pending) { /* busy waiting for timeouts in progress */ |
342 | if (THREAD->timeout_pending) { /* busy waiting for timeouts in progress */ |
342 | spinlock_unlock(&THREAD->lock); |
343 | spinlock_unlock(&THREAD->lock); |
343 | interrupts_restore(ipl); |
344 | interrupts_restore(ipl); |
344 | goto restart; |
345 | goto restart; |
345 | } |
346 | } |
346 | THREAD->state = Exiting; |
347 | THREAD->state = Exiting; |
347 | spinlock_unlock(&THREAD->lock); |
348 | spinlock_unlock(&THREAD->lock); |
348 | scheduler(); |
349 | scheduler(); |
349 | } |
350 | } |
350 | 351 | ||
351 | 352 | ||
352 | /** Thread sleep |
353 | /** Thread sleep |
353 | * |
354 | * |
354 | * Suspend execution of the current thread. |
355 | * Suspend execution of the current thread. |
355 | * |
356 | * |
356 | * @param sec Number of seconds to sleep. |
357 | * @param sec Number of seconds to sleep. |
357 | * |
358 | * |
358 | */ |
359 | */ |
359 | void thread_sleep(__u32 sec) |
360 | void thread_sleep(__u32 sec) |
360 | { |
361 | { |
361 | thread_usleep(sec*1000000); |
362 | thread_usleep(sec*1000000); |
362 | } |
363 | } |
363 | 364 | ||
364 | 365 | ||
365 | /** Thread usleep |
366 | /** Thread usleep |
366 | * |
367 | * |
367 | * Suspend execution of the current thread. |
368 | * Suspend execution of the current thread. |
368 | * |
369 | * |
369 | * @param usec Number of microseconds to sleep. |
370 | * @param usec Number of microseconds to sleep. |
370 | * |
371 | * |
371 | */ |
372 | */ |
372 | void thread_usleep(__u32 usec) |
373 | void thread_usleep(__u32 usec) |
373 | { |
374 | { |
374 | waitq_t wq; |
375 | waitq_t wq; |
375 | 376 | ||
376 | waitq_initialize(&wq); |
377 | waitq_initialize(&wq); |
377 | 378 | ||
378 | (void) waitq_sleep_timeout(&wq, usec, SYNCH_NON_BLOCKING); |
379 | (void) waitq_sleep_timeout(&wq, usec, SYNCH_NON_BLOCKING); |
379 | } |
380 | } |
380 | 381 | ||
381 | 382 | ||
382 | /** Register thread out-of-context invocation |
383 | /** Register thread out-of-context invocation |
383 | * |
384 | * |
384 | * Register a function and its argument to be executed |
385 | * Register a function and its argument to be executed |
385 | * on next context switch to the current thread. |
386 | * on next context switch to the current thread. |
386 | * |
387 | * |
387 | * @param call_me Out-of-context function. |
388 | * @param call_me Out-of-context function. |
388 | * @param call_me_with Out-of-context function argument. |
389 | * @param call_me_with Out-of-context function argument. |
389 | * |
390 | * |
390 | */ |
391 | */ |
391 | void thread_register_call_me(void (* call_me)(void *), void *call_me_with) |
392 | void thread_register_call_me(void (* call_me)(void *), void *call_me_with) |
392 | { |
393 | { |
393 | ipl_t ipl; |
394 | ipl_t ipl; |
394 | 395 | ||
395 | ipl = interrupts_disable(); |
396 | ipl = interrupts_disable(); |
396 | spinlock_lock(&THREAD->lock); |
397 | spinlock_lock(&THREAD->lock); |
397 | THREAD->call_me = call_me; |
398 | THREAD->call_me = call_me; |
398 | THREAD->call_me_with = call_me_with; |
399 | THREAD->call_me_with = call_me_with; |
399 | spinlock_unlock(&THREAD->lock); |
400 | spinlock_unlock(&THREAD->lock); |
400 | interrupts_restore(ipl); |
401 | interrupts_restore(ipl); |
401 | } |
402 | } |
402 | 403 | ||
403 | /** Print list of threads debug info */ |
404 | /** Print list of threads debug info */ |
404 | void thread_print_list(void) |
405 | void thread_print_list(void) |
405 | { |
406 | { |
406 | link_t *cur; |
407 | link_t *cur; |
407 | thread_t *t; |
408 | thread_t *t; |
408 | ipl_t ipl; |
409 | ipl_t ipl; |
409 | 410 | ||
410 | /* Messing with thread structures, avoid deadlock */ |
411 | /* Messing with thread structures, avoid deadlock */ |
411 | ipl = interrupts_disable(); |
412 | ipl = interrupts_disable(); |
412 | spinlock_lock(&threads_lock); |
413 | spinlock_lock(&threads_lock); |
413 | 414 | ||
414 | for (cur=threads_head.next; cur!=&threads_head; cur=cur->next) { |
415 | for (cur=threads_head.next; cur!=&threads_head; cur=cur->next) { |
415 | t = list_get_instance(cur, thread_t, threads_link); |
416 | t = list_get_instance(cur, thread_t, threads_link); |
416 | printf("%s: address=%P, tid=%d, state=%s, task=%P, code=%P, stack=%P, cpu=", |
417 | printf("%s: address=%P, tid=%d, state=%s, task=%P, code=%P, stack=%P, cpu=", |
417 | t->name, t, t->tid, thread_states[t->state], t->task, t->thread_code, t->kstack); |
418 | t->name, t, t->tid, thread_states[t->state], t->task, t->thread_code, t->kstack); |
418 | if (t->cpu) |
419 | if (t->cpu) |
419 | printf("cpu%d ", t->cpu->id); |
420 | printf("cpu%d ", t->cpu->id); |
420 | else |
421 | else |
421 | printf("none"); |
422 | printf("none"); |
422 | printf("\n"); |
423 | printf("\n"); |
423 | } |
424 | } |
424 | 425 | ||
425 | spinlock_unlock(&threads_lock); |
426 | spinlock_unlock(&threads_lock); |
426 | interrupts_restore(ipl); |
427 | interrupts_restore(ipl); |
427 | } |
428 | } |
428 | 429 | ||
429 | /** Process syscall to create new thread. |
430 | /** Process syscall to create new thread. |
430 | * |
431 | * |
431 | */ |
432 | */ |
432 | __native sys_thread_create(__address function, void *arg, void *stack, char *name) |
433 | __native sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name) |
433 | { |
434 | { |
434 | thread_t *t; |
435 | thread_t *t; |
435 | char namebuf[THREAD_NAME_BUFLEN]; |
436 | char namebuf[THREAD_NAME_BUFLEN]; |
436 | uspace_arg_t *uarg; |
437 | uspace_arg_t *kernel_uarg; /* TODO: store kernel_uarg in thread_t */ |
437 | __u32 tid; |
438 | __u32 tid; |
438 | 439 | ||
439 | copy_from_uspace(namebuf, name, THREAD_NAME_BUFLEN); |
440 | copy_from_uspace(namebuf, uspace_name, THREAD_NAME_BUFLEN); |
440 | uarg = (uspace_arg_t *) malloc(sizeof(uarg), 0); |
- | |
441 | 441 | ||
442 | uarg->uspace_entry = function; |
442 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
443 | uarg->uspace_stack = (__address) stack; |
443 | copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t)); |
444 | 444 | ||
445 | if ((t = thread_create(uinit, uarg, TASK, 0, namebuf))) { |
445 | if ((t = thread_create(uinit, kernel_uarg, TASK, 0, namebuf))) { |
446 | tid = t->tid; |
446 | tid = t->tid; |
447 | thread_ready(t); |
447 | thread_ready(t); |
448 | return (__native) tid; |
448 | return (__native) tid; |
449 | } else { |
449 | } else { |
450 | free(namebuf); |
450 | free(kernel_uarg); |
451 | } |
451 | } |
452 | 452 | ||
453 | return (__native) -1; |
453 | return (__native) -1; |
454 | } |
454 | } |
455 | 455 | ||
456 | /** Process syscall to terminate thread. |
456 | /** Process syscall to terminate thread. |
457 | * |
457 | * |
458 | */ |
458 | */ |
459 | __native sys_thread_exit(int status) |
459 | __native sys_thread_exit(int uspace_status) |
460 | { |
460 | { |
461 | thread_exit(); |
461 | thread_exit(); |
462 | /* Unreachable */ |
462 | /* Unreachable */ |
463 | return 0; |
463 | return 0; |
464 | } |
464 | } |
465 | 465 |