Rev 1661 | Rev 1687 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1661 | Rev 1680 | ||
---|---|---|---|
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 | /** |
29 | /** |
30 | * @file thread.c |
30 | * @file thread.c |
31 | * @brief Thread management functions. |
31 | * @brief Thread management functions. |
32 | */ |
32 | */ |
33 | 33 | ||
34 | #include <proc/scheduler.h> |
34 | #include <proc/scheduler.h> |
35 | #include <proc/thread.h> |
35 | #include <proc/thread.h> |
36 | #include <proc/task.h> |
36 | #include <proc/task.h> |
37 | #include <proc/uarg.h> |
37 | #include <proc/uarg.h> |
38 | #include <mm/frame.h> |
38 | #include <mm/frame.h> |
39 | #include <mm/page.h> |
39 | #include <mm/page.h> |
40 | #include <arch/asm.h> |
40 | #include <arch/asm.h> |
41 | #include <arch.h> |
41 | #include <arch.h> |
42 | #include <synch/synch.h> |
42 | #include <synch/synch.h> |
43 | #include <synch/spinlock.h> |
43 | #include <synch/spinlock.h> |
44 | #include <synch/waitq.h> |
44 | #include <synch/waitq.h> |
45 | #include <synch/rwlock.h> |
45 | #include <synch/rwlock.h> |
46 | #include <cpu.h> |
46 | #include <cpu.h> |
47 | #include <func.h> |
47 | #include <func.h> |
48 | #include <context.h> |
48 | #include <context.h> |
49 | #include <adt/btree.h> |
49 | #include <adt/btree.h> |
50 | #include <adt/list.h> |
50 | #include <adt/list.h> |
51 | #include <typedefs.h> |
51 | #include <typedefs.h> |
52 | #include <time/clock.h> |
52 | #include <time/clock.h> |
53 | #include <config.h> |
53 | #include <config.h> |
54 | #include <arch/interrupt.h> |
54 | #include <arch/interrupt.h> |
55 | #include <smp/ipi.h> |
55 | #include <smp/ipi.h> |
56 | #include <arch/faddr.h> |
56 | #include <arch/faddr.h> |
57 | #include <atomic.h> |
57 | #include <atomic.h> |
58 | #include <memstr.h> |
58 | #include <memstr.h> |
59 | #include <print.h> |
59 | #include <print.h> |
60 | #include <mm/slab.h> |
60 | #include <mm/slab.h> |
61 | #include <debug.h> |
61 | #include <debug.h> |
62 | #include <main/uinit.h> |
62 | #include <main/uinit.h> |
63 | #include <syscall/copy.h> |
63 | #include <syscall/copy.h> |
64 | #include <errno.h> |
64 | #include <errno.h> |
65 | 65 | ||
66 | 66 | ||
67 | /** Thread states */ |
67 | /** Thread states */ |
68 | char *thread_states[] = { |
68 | char *thread_states[] = { |
69 | "Invalid", |
69 | "Invalid", |
70 | "Running", |
70 | "Running", |
71 | "Sleeping", |
71 | "Sleeping", |
72 | "Ready", |
72 | "Ready", |
73 | "Entering", |
73 | "Entering", |
74 | "Exiting", |
74 | "Exiting", |
75 | "Undead" |
75 | "Undead" |
76 | }; |
76 | }; |
77 | 77 | ||
78 | /** Lock protecting the threads_btree B+tree. For locking rules, see declaration thereof. */ |
78 | /** Lock protecting the threads_btree B+tree. For locking rules, see declaration thereof. */ |
79 | SPINLOCK_INITIALIZE(threads_lock); |
79 | SPINLOCK_INITIALIZE(threads_lock); |
80 | 80 | ||
81 | /** B+tree of all threads. |
81 | /** B+tree of all threads. |
82 | * |
82 | * |
83 | * When a thread is found in the threads_btree B+tree, it is guaranteed to exist as long |
83 | * When a thread is found in the threads_btree B+tree, it is guaranteed to exist as long |
84 | * as the threads_lock is held. |
84 | * as the threads_lock is held. |
85 | */ |
85 | */ |
86 | btree_t threads_btree; |
86 | btree_t threads_btree; |
87 | 87 | ||
88 | SPINLOCK_INITIALIZE(tidlock); |
88 | SPINLOCK_INITIALIZE(tidlock); |
89 | __u32 last_tid = 0; |
89 | __u32 last_tid = 0; |
90 | 90 | ||
91 | static slab_cache_t *thread_slab; |
91 | static slab_cache_t *thread_slab; |
92 | #ifdef ARCH_HAS_FPU |
92 | #ifdef ARCH_HAS_FPU |
93 | slab_cache_t *fpu_context_slab; |
93 | slab_cache_t *fpu_context_slab; |
94 | #endif |
94 | #endif |
95 | 95 | ||
96 | /** Thread wrapper |
96 | /** Thread wrapper |
97 | * |
97 | * |
98 | * This wrapper is provided to ensure that every thread |
98 | * This wrapper is provided to ensure that every thread |
99 | * makes a call to thread_exit() when its implementing |
99 | * makes a call to thread_exit() when its implementing |
100 | * function returns. |
100 | * function returns. |
101 | * |
101 | * |
102 | * interrupts_disable() is assumed. |
102 | * interrupts_disable() is assumed. |
103 | * |
103 | * |
104 | */ |
104 | */ |
105 | static void cushion(void) |
105 | static void cushion(void) |
106 | { |
106 | { |
107 | void (*f)(void *) = THREAD->thread_code; |
107 | void (*f)(void *) = THREAD->thread_code; |
108 | void *arg = THREAD->thread_arg; |
108 | void *arg = THREAD->thread_arg; |
109 | 109 | ||
110 | /* this is where each thread wakes up after its creation */ |
110 | /* this is where each thread wakes up after its creation */ |
111 | spinlock_unlock(&THREAD->lock); |
111 | spinlock_unlock(&THREAD->lock); |
112 | interrupts_enable(); |
112 | interrupts_enable(); |
113 | 113 | ||
114 | f(arg); |
114 | f(arg); |
115 | thread_exit(); |
115 | thread_exit(); |
116 | /* not reached */ |
116 | /* not reached */ |
117 | } |
117 | } |
118 | 118 | ||
119 | /** Initialization and allocation for thread_t structure */ |
119 | /** Initialization and allocation for thread_t structure */ |
120 | static int thr_constructor(void *obj, int kmflags) |
120 | static int thr_constructor(void *obj, int kmflags) |
121 | { |
121 | { |
122 | thread_t *t = (thread_t *)obj; |
122 | thread_t *t = (thread_t *)obj; |
123 | pfn_t pfn; |
123 | pfn_t pfn; |
124 | int status; |
124 | int status; |
125 | 125 | ||
126 | spinlock_initialize(&t->lock, "thread_t_lock"); |
126 | spinlock_initialize(&t->lock, "thread_t_lock"); |
127 | link_initialize(&t->rq_link); |
127 | link_initialize(&t->rq_link); |
128 | link_initialize(&t->wq_link); |
128 | link_initialize(&t->wq_link); |
129 | link_initialize(&t->th_link); |
129 | link_initialize(&t->th_link); |
130 | 130 | ||
131 | #ifdef ARCH_HAS_FPU |
131 | #ifdef ARCH_HAS_FPU |
132 | # ifdef CONFIG_FPU_LAZY |
132 | # ifdef CONFIG_FPU_LAZY |
133 | t->saved_fpu_context = NULL; |
133 | t->saved_fpu_context = NULL; |
134 | # else |
134 | # else |
135 | t->saved_fpu_context = slab_alloc(fpu_context_slab,kmflags); |
135 | t->saved_fpu_context = slab_alloc(fpu_context_slab,kmflags); |
136 | if (!t->saved_fpu_context) |
136 | if (!t->saved_fpu_context) |
137 | return -1; |
137 | return -1; |
138 | # endif |
138 | # endif |
139 | #endif |
139 | #endif |
140 | 140 | ||
141 | pfn = frame_alloc_rc(STACK_FRAMES, FRAME_KA | kmflags,&status); |
141 | pfn = frame_alloc_rc(STACK_FRAMES, FRAME_KA | kmflags,&status); |
142 | if (status) { |
142 | if (status) { |
143 | #ifdef ARCH_HAS_FPU |
143 | #ifdef ARCH_HAS_FPU |
144 | if (t->saved_fpu_context) |
144 | if (t->saved_fpu_context) |
145 | slab_free(fpu_context_slab,t->saved_fpu_context); |
145 | slab_free(fpu_context_slab,t->saved_fpu_context); |
146 | #endif |
146 | #endif |
147 | return -1; |
147 | return -1; |
148 | } |
148 | } |
149 | t->kstack = (__u8 *)PA2KA(PFN2ADDR(pfn)); |
149 | t->kstack = (__u8 *)PA2KA(PFN2ADDR(pfn)); |
150 | 150 | ||
151 | return 0; |
151 | return 0; |
152 | } |
152 | } |
153 | 153 | ||
154 | /** Destruction of thread_t object */ |
154 | /** Destruction of thread_t object */ |
155 | static int thr_destructor(void *obj) |
155 | static int thr_destructor(void *obj) |
156 | { |
156 | { |
157 | thread_t *t = (thread_t *)obj; |
157 | thread_t *t = (thread_t *)obj; |
158 | 158 | ||
159 | frame_free(ADDR2PFN(KA2PA(t->kstack))); |
159 | frame_free(ADDR2PFN(KA2PA(t->kstack))); |
160 | #ifdef ARCH_HAS_FPU |
160 | #ifdef ARCH_HAS_FPU |
161 | if (t->saved_fpu_context) |
161 | if (t->saved_fpu_context) |
162 | slab_free(fpu_context_slab,t->saved_fpu_context); |
162 | slab_free(fpu_context_slab,t->saved_fpu_context); |
163 | #endif |
163 | #endif |
164 | return 1; /* One page freed */ |
164 | return 1; /* One page freed */ |
165 | } |
165 | } |
166 | 166 | ||
167 | /** Initialize threads |
167 | /** Initialize threads |
168 | * |
168 | * |
169 | * Initialize kernel threads support. |
169 | * Initialize kernel threads support. |
170 | * |
170 | * |
171 | */ |
171 | */ |
172 | void thread_init(void) |
172 | void thread_init(void) |
173 | { |
173 | { |
174 | THREAD = NULL; |
174 | THREAD = NULL; |
175 | atomic_set(&nrdy,0); |
175 | atomic_set(&nrdy,0); |
176 | thread_slab = slab_cache_create("thread_slab", |
176 | thread_slab = slab_cache_create("thread_slab", |
177 | sizeof(thread_t),0, |
177 | sizeof(thread_t),0, |
178 | thr_constructor, thr_destructor, 0); |
178 | thr_constructor, thr_destructor, 0); |
179 | #ifdef ARCH_HAS_FPU |
179 | #ifdef ARCH_HAS_FPU |
180 | fpu_context_slab = slab_cache_create("fpu_slab", |
180 | fpu_context_slab = slab_cache_create("fpu_slab", |
181 | sizeof(fpu_context_t), |
181 | sizeof(fpu_context_t), |
182 | FPU_CONTEXT_ALIGN, |
182 | FPU_CONTEXT_ALIGN, |
183 | NULL, NULL, 0); |
183 | NULL, NULL, 0); |
184 | #endif |
184 | #endif |
185 | 185 | ||
186 | btree_create(&threads_btree); |
186 | btree_create(&threads_btree); |
187 | } |
187 | } |
188 | 188 | ||
189 | /** Make thread ready |
189 | /** Make thread ready |
190 | * |
190 | * |
191 | * Switch thread t to the ready state. |
191 | * Switch thread t to the ready state. |
192 | * |
192 | * |
193 | * @param t Thread to make ready. |
193 | * @param t Thread to make ready. |
194 | * |
194 | * |
195 | */ |
195 | */ |
196 | void thread_ready(thread_t *t) |
196 | void thread_ready(thread_t *t) |
197 | { |
197 | { |
198 | cpu_t *cpu; |
198 | cpu_t *cpu; |
199 | runq_t *r; |
199 | runq_t *r; |
200 | ipl_t ipl; |
200 | ipl_t ipl; |
201 | int i, avg; |
201 | int i, avg; |
202 | 202 | ||
203 | ipl = interrupts_disable(); |
203 | ipl = interrupts_disable(); |
204 | 204 | ||
205 | spinlock_lock(&t->lock); |
205 | spinlock_lock(&t->lock); |
206 | 206 | ||
207 | ASSERT(! (t->state == Ready)); |
207 | ASSERT(! (t->state == Ready)); |
208 | 208 | ||
209 | i = (t->priority < RQ_COUNT -1) ? ++t->priority : t->priority; |
209 | i = (t->priority < RQ_COUNT -1) ? ++t->priority : t->priority; |
210 | 210 | ||
211 | cpu = CPU; |
211 | cpu = CPU; |
212 | if (t->flags & X_WIRED) { |
212 | if (t->flags & X_WIRED) { |
213 | cpu = t->cpu; |
213 | cpu = t->cpu; |
214 | } |
214 | } |
215 | t->state = Ready; |
215 | t->state = Ready; |
216 | spinlock_unlock(&t->lock); |
216 | spinlock_unlock(&t->lock); |
217 | 217 | ||
218 | /* |
218 | /* |
219 | * Append t to respective ready queue on respective processor. |
219 | * Append t to respective ready queue on respective processor. |
220 | */ |
220 | */ |
221 | r = &cpu->rq[i]; |
221 | r = &cpu->rq[i]; |
222 | spinlock_lock(&r->lock); |
222 | spinlock_lock(&r->lock); |
223 | list_append(&t->rq_link, &r->rq_head); |
223 | list_append(&t->rq_link, &r->rq_head); |
224 | r->n++; |
224 | r->n++; |
225 | spinlock_unlock(&r->lock); |
225 | spinlock_unlock(&r->lock); |
226 | 226 | ||
227 | atomic_inc(&nrdy); |
227 | atomic_inc(&nrdy); |
228 | avg = atomic_get(&nrdy) / config.cpu_active; |
228 | avg = atomic_get(&nrdy) / config.cpu_active; |
229 | atomic_inc(&cpu->nrdy); |
229 | atomic_inc(&cpu->nrdy); |
230 | 230 | ||
231 | interrupts_restore(ipl); |
231 | interrupts_restore(ipl); |
232 | } |
232 | } |
233 | 233 | ||
234 | /** Destroy thread memory structure |
234 | /** Destroy thread memory structure |
235 | * |
235 | * |
236 | * Detach thread from all queues, cpus etc. and destroy it. |
236 | * Detach thread from all queues, cpus etc. and destroy it. |
237 | * |
237 | * |
238 | * Assume thread->lock is held!! |
238 | * Assume thread->lock is held!! |
239 | */ |
239 | */ |
240 | void thread_destroy(thread_t *t) |
240 | void thread_destroy(thread_t *t) |
241 | { |
241 | { |
242 | bool destroy_task = false; |
242 | bool destroy_task = false; |
243 | 243 | ||
244 | ASSERT(t->state == Exiting || t->state == Undead); |
244 | ASSERT(t->state == Exiting || t->state == Undead); |
245 | ASSERT(t->task); |
245 | ASSERT(t->task); |
246 | ASSERT(t->cpu); |
246 | ASSERT(t->cpu); |
247 | 247 | ||
248 | spinlock_lock(&t->cpu->lock); |
248 | spinlock_lock(&t->cpu->lock); |
249 | if(t->cpu->fpu_owner==t) |
249 | if(t->cpu->fpu_owner==t) |
250 | t->cpu->fpu_owner=NULL; |
250 | t->cpu->fpu_owner=NULL; |
251 | spinlock_unlock(&t->cpu->lock); |
251 | spinlock_unlock(&t->cpu->lock); |
252 | 252 | ||
253 | spinlock_unlock(&t->lock); |
253 | spinlock_unlock(&t->lock); |
254 | 254 | ||
255 | spinlock_lock(&threads_lock); |
255 | spinlock_lock(&threads_lock); |
256 | btree_remove(&threads_btree, (btree_key_t) ((__address ) t), NULL); |
256 | btree_remove(&threads_btree, (btree_key_t) ((__address ) t), NULL); |
257 | spinlock_unlock(&threads_lock); |
257 | spinlock_unlock(&threads_lock); |
258 | 258 | ||
259 | /* |
259 | /* |
260 | * Detach from the containing task. |
260 | * Detach from the containing task. |
261 | */ |
261 | */ |
262 | spinlock_lock(&t->task->lock); |
262 | spinlock_lock(&t->task->lock); |
263 | list_remove(&t->th_link); |
263 | list_remove(&t->th_link); |
264 | if (--t->task->refcount == 0) { |
264 | if (--t->task->refcount == 0) { |
265 | t->task->accept_new_threads = false; |
265 | t->task->accept_new_threads = false; |
266 | destroy_task = true; |
266 | destroy_task = true; |
267 | } |
267 | } |
268 | spinlock_unlock(&t->task->lock); |
268 | spinlock_unlock(&t->task->lock); |
269 | 269 | ||
270 | if (destroy_task) |
270 | if (destroy_task) |
271 | task_destroy(t->task); |
271 | task_destroy(t->task); |
272 | 272 | ||
273 | slab_free(thread_slab, t); |
273 | slab_free(thread_slab, t); |
274 | } |
274 | } |
275 | 275 | ||
276 | /** Create new thread |
276 | /** Create new thread |
277 | * |
277 | * |
278 | * Create a new thread. |
278 | * Create a new thread. |
279 | * |
279 | * |
280 | * @param func Thread's implementing function. |
280 | * @param func Thread's implementing function. |
281 | * @param arg Thread's implementing function argument. |
281 | * @param arg Thread's implementing function argument. |
282 | * @param task Task to which the thread belongs. |
282 | * @param task Task to which the thread belongs. |
283 | * @param flags Thread flags. |
283 | * @param flags Thread flags. |
284 | * @param name Symbolic name. |
284 | * @param name Symbolic name. |
285 | * |
285 | * |
286 | * @return New thread's structure on success, NULL on failure. |
286 | * @return New thread's structure on success, NULL on failure. |
287 | * |
287 | * |
288 | */ |
288 | */ |
289 | thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, int flags, char *name) |
289 | thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, int flags, char *name) |
290 | { |
290 | { |
291 | thread_t *t; |
291 | thread_t *t; |
292 | ipl_t ipl; |
292 | ipl_t ipl; |
293 | 293 | ||
294 | t = (thread_t *) slab_alloc(thread_slab, 0); |
294 | t = (thread_t *) slab_alloc(thread_slab, 0); |
295 | if (!t) |
295 | if (!t) |
296 | return NULL; |
296 | return NULL; |
297 | 297 | ||
298 | thread_create_arch(t); |
298 | thread_create_arch(t); |
299 | 299 | ||
300 | /* Not needed, but good for debugging */ |
300 | /* Not needed, but good for debugging */ |
301 | memsetb((__address)t->kstack, THREAD_STACK_SIZE * 1<<STACK_FRAMES, 0); |
301 | memsetb((__address)t->kstack, THREAD_STACK_SIZE * 1<<STACK_FRAMES, 0); |
302 | 302 | ||
303 | ipl = interrupts_disable(); |
303 | ipl = interrupts_disable(); |
304 | spinlock_lock(&tidlock); |
304 | spinlock_lock(&tidlock); |
305 | t->tid = ++last_tid; |
305 | t->tid = ++last_tid; |
306 | spinlock_unlock(&tidlock); |
306 | spinlock_unlock(&tidlock); |
307 | interrupts_restore(ipl); |
307 | interrupts_restore(ipl); |
308 | 308 | ||
309 | context_save(&t->saved_context); |
309 | context_save(&t->saved_context); |
310 | context_set(&t->saved_context, FADDR(cushion), (__address) t->kstack, THREAD_STACK_SIZE); |
310 | context_set(&t->saved_context, FADDR(cushion), (__address) t->kstack, THREAD_STACK_SIZE); |
311 | 311 | ||
312 | the_initialize((the_t *) t->kstack); |
312 | the_initialize((the_t *) t->kstack); |
313 | 313 | ||
314 | ipl = interrupts_disable(); |
314 | ipl = interrupts_disable(); |
315 | t->saved_context.ipl = interrupts_read(); |
315 | t->saved_context.ipl = interrupts_read(); |
316 | interrupts_restore(ipl); |
316 | interrupts_restore(ipl); |
317 | 317 | ||
318 | memcpy(t->name, name, THREAD_NAME_BUFLEN); |
318 | memcpy(t->name, name, THREAD_NAME_BUFLEN); |
319 | 319 | ||
320 | t->thread_code = func; |
320 | t->thread_code = func; |
321 | t->thread_arg = arg; |
321 | t->thread_arg = arg; |
322 | t->ticks = -1; |
322 | t->ticks = -1; |
323 | t->priority = -1; /* start in rq[0] */ |
323 | t->priority = -1; /* start in rq[0] */ |
324 | t->cpu = NULL; |
324 | t->cpu = NULL; |
325 | t->flags = 0; |
325 | t->flags = 0; |
326 | t->state = Entering; |
326 | t->state = Entering; |
327 | t->call_me = NULL; |
327 | t->call_me = NULL; |
328 | t->call_me_with = NULL; |
328 | t->call_me_with = NULL; |
329 | 329 | ||
330 | timeout_initialize(&t->sleep_timeout); |
330 | timeout_initialize(&t->sleep_timeout); |
331 | t->sleep_interruptible = false; |
331 | t->sleep_interruptible = false; |
332 | t->sleep_queue = NULL; |
332 | t->sleep_queue = NULL; |
333 | t->timeout_pending = 0; |
333 | t->timeout_pending = 0; |
334 | 334 | ||
335 | t->in_copy_from_uspace = false; |
335 | t->in_copy_from_uspace = false; |
336 | t->in_copy_to_uspace = false; |
336 | t->in_copy_to_uspace = false; |
337 | 337 | ||
338 | t->interrupted = false; |
338 | t->interrupted = false; |
339 | t->join_type = None; |
339 | t->join_type = None; |
340 | t->detached = false; |
340 | t->detached = false; |
341 | waitq_initialize(&t->join_wq); |
341 | waitq_initialize(&t->join_wq); |
342 | 342 | ||
343 | t->rwlock_holder_type = RWLOCK_NONE; |
343 | t->rwlock_holder_type = RWLOCK_NONE; |
344 | 344 | ||
345 | t->task = task; |
345 | t->task = task; |
346 | 346 | ||
347 | t->fpu_context_exists = 0; |
347 | t->fpu_context_exists = 0; |
348 | t->fpu_context_engaged = 0; |
348 | t->fpu_context_engaged = 0; |
349 | 349 | ||
350 | /* |
350 | /* |
351 | * Attach to the containing task. |
351 | * Attach to the containing task. |
352 | */ |
352 | */ |
353 | spinlock_lock(&task->lock); |
353 | spinlock_lock(&task->lock); |
354 | if (!task->accept_new_threads) { |
354 | if (!task->accept_new_threads) { |
355 | spinlock_unlock(&task->lock); |
355 | spinlock_unlock(&task->lock); |
356 | slab_free(thread_slab, t); |
356 | slab_free(thread_slab, t); |
357 | return NULL; |
357 | return NULL; |
358 | } |
358 | } |
359 | list_append(&t->th_link, &task->th_head); |
359 | list_append(&t->th_link, &task->th_head); |
360 | if (task->refcount++ == 0) |
360 | if (task->refcount++ == 0) |
361 | task->main_thread = t; |
361 | task->main_thread = t; |
362 | spinlock_unlock(&task->lock); |
362 | spinlock_unlock(&task->lock); |
363 | 363 | ||
364 | /* |
364 | /* |
365 | * Register this thread in the system-wide list. |
365 | * Register this thread in the system-wide list. |
366 | */ |
366 | */ |
367 | ipl = interrupts_disable(); |
367 | ipl = interrupts_disable(); |
368 | spinlock_lock(&threads_lock); |
368 | spinlock_lock(&threads_lock); |
369 | btree_insert(&threads_btree, (btree_key_t) ((__address) t), (void *) t, NULL); |
369 | btree_insert(&threads_btree, (btree_key_t) ((__address) t), (void *) t, NULL); |
370 | spinlock_unlock(&threads_lock); |
370 | spinlock_unlock(&threads_lock); |
371 | 371 | ||
372 | interrupts_restore(ipl); |
372 | interrupts_restore(ipl); |
373 | 373 | ||
374 | return t; |
374 | return t; |
375 | } |
375 | } |
376 | 376 | ||
377 | /** Make thread exiting |
377 | /** Make thread exiting |
378 | * |
378 | * |
379 | * End current thread execution and switch it to the exiting |
379 | * End current thread execution and switch it to the exiting |
380 | * state. All pending timeouts are executed. |
380 | * state. All pending timeouts are executed. |
381 | * |
381 | * |
382 | */ |
382 | */ |
383 | void thread_exit(void) |
383 | void thread_exit(void) |
384 | { |
384 | { |
385 | ipl_t ipl; |
385 | ipl_t ipl; |
386 | 386 | ||
387 | restart: |
387 | restart: |
388 | ipl = interrupts_disable(); |
388 | ipl = interrupts_disable(); |
389 | spinlock_lock(&THREAD->lock); |
389 | spinlock_lock(&THREAD->lock); |
390 | if (THREAD->timeout_pending) { /* busy waiting for timeouts in progress */ |
390 | if (THREAD->timeout_pending) { /* busy waiting for timeouts in progress */ |
391 | spinlock_unlock(&THREAD->lock); |
391 | spinlock_unlock(&THREAD->lock); |
392 | interrupts_restore(ipl); |
392 | interrupts_restore(ipl); |
393 | goto restart; |
393 | goto restart; |
394 | } |
394 | } |
395 | THREAD->state = Exiting; |
395 | THREAD->state = Exiting; |
396 | spinlock_unlock(&THREAD->lock); |
396 | spinlock_unlock(&THREAD->lock); |
397 | scheduler(); |
397 | scheduler(); |
398 | 398 | ||
399 | /* Not reached */ |
399 | /* Not reached */ |
400 | while (1) |
400 | while (1) |
401 | ; |
401 | ; |
402 | } |
402 | } |
403 | 403 | ||
404 | 404 | ||
405 | /** Thread sleep |
405 | /** Thread sleep |
406 | * |
406 | * |
407 | * Suspend execution of the current thread. |
407 | * Suspend execution of the current thread. |
408 | * |
408 | * |
409 | * @param sec Number of seconds to sleep. |
409 | * @param sec Number of seconds to sleep. |
410 | * |
410 | * |
411 | */ |
411 | */ |
412 | void thread_sleep(__u32 sec) |
412 | void thread_sleep(__u32 sec) |
413 | { |
413 | { |
414 | thread_usleep(sec*1000000); |
414 | thread_usleep(sec*1000000); |
415 | } |
415 | } |
416 | 416 | ||
417 | /** Wait for another thread to exit. |
417 | /** Wait for another thread to exit. |
418 | * |
418 | * |
419 | * @param t Thread to join on exit. |
419 | * @param t Thread to join on exit. |
420 | * @param usec Timeout in microseconds. |
420 | * @param usec Timeout in microseconds. |
421 | * @param flags Mode of operation. |
421 | * @param flags Mode of operation. |
422 | * |
422 | * |
423 | * @return An error code from errno.h or an error code from synch.h. |
423 | * @return An error code from errno.h or an error code from synch.h. |
424 | */ |
424 | */ |
425 | int thread_join_timeout(thread_t *t, __u32 usec, int flags) |
425 | int thread_join_timeout(thread_t *t, __u32 usec, int flags) |
426 | { |
426 | { |
427 | ipl_t ipl; |
427 | ipl_t ipl; |
428 | int rc; |
428 | int rc; |
429 | 429 | ||
430 | if (t == THREAD) |
430 | if (t == THREAD) |
431 | return EINVAL; |
431 | return EINVAL; |
432 | 432 | ||
433 | /* |
433 | /* |
434 | * Since thread join can only be called once on an undetached thread, |
434 | * Since thread join can only be called once on an undetached thread, |
435 | * the thread pointer is guaranteed to be still valid. |
435 | * the thread pointer is guaranteed to be still valid. |
436 | */ |
436 | */ |
437 | 437 | ||
438 | ipl = interrupts_disable(); |
438 | ipl = interrupts_disable(); |
439 | spinlock_lock(&t->lock); |
439 | spinlock_lock(&t->lock); |
440 | 440 | ||
441 | ASSERT(!t->detached); |
441 | ASSERT(!t->detached); |
442 | 442 | ||
443 | (void) waitq_sleep_prepare(&t->join_wq); |
443 | (void) waitq_sleep_prepare(&t->join_wq); |
444 | spinlock_unlock(&t->lock); |
444 | spinlock_unlock(&t->lock); |
445 | 445 | ||
446 | rc = waitq_sleep_timeout_unsafe(&t->join_wq, usec, flags); |
446 | rc = waitq_sleep_timeout_unsafe(&t->join_wq, usec, flags); |
447 | 447 | ||
448 | waitq_sleep_finish(&t->join_wq, rc, ipl); |
448 | waitq_sleep_finish(&t->join_wq, rc, ipl); |
- | 449 | interrupts_restore(ipl); |
|
449 | 450 | ||
450 | return rc; |
451 | return rc; |
451 | } |
452 | } |
452 | 453 | ||
453 | /** Detach thread. |
454 | /** Detach thread. |
454 | * |
455 | * |
455 | * Mark the thread as detached, if the thread is already in the Undead state, |
456 | * Mark the thread as detached, if the thread is already in the Undead state, |
456 | * deallocate its resources. |
457 | * deallocate its resources. |
457 | * |
458 | * |
458 | * @param t Thread to be detached. |
459 | * @param t Thread to be detached. |
459 | */ |
460 | */ |
460 | void thread_detach(thread_t *t) |
461 | void thread_detach(thread_t *t) |
461 | { |
462 | { |
462 | ipl_t ipl; |
463 | ipl_t ipl; |
463 | 464 | ||
464 | /* |
465 | /* |
465 | * Since the thread is expected to not be already detached, |
466 | * Since the thread is expected to not be already detached, |
466 | * pointer to it must be still valid. |
467 | * pointer to it must be still valid. |
467 | */ |
468 | */ |
468 | 469 | ||
469 | ipl = interrupts_disable(); |
470 | ipl = interrupts_disable(); |
470 | spinlock_lock(&t->lock); |
471 | spinlock_lock(&t->lock); |
471 | ASSERT(!t->detached); |
472 | ASSERT(!t->detached); |
472 | if (t->state == Undead) { |
473 | if (t->state == Undead) { |
473 | thread_destroy(t); /* unlocks &t->lock */ |
474 | thread_destroy(t); /* unlocks &t->lock */ |
474 | interrupts_restore(ipl); |
475 | interrupts_restore(ipl); |
475 | return; |
476 | return; |
476 | } else { |
477 | } else { |
477 | t->detached = true; |
478 | t->detached = true; |
478 | } |
479 | } |
479 | spinlock_unlock(&t->lock); |
480 | spinlock_unlock(&t->lock); |
480 | interrupts_restore(ipl); |
481 | interrupts_restore(ipl); |
481 | } |
482 | } |
482 | 483 | ||
483 | /** Thread usleep |
484 | /** Thread usleep |
484 | * |
485 | * |
485 | * Suspend execution of the current thread. |
486 | * Suspend execution of the current thread. |
486 | * |
487 | * |
487 | * @param usec Number of microseconds to sleep. |
488 | * @param usec Number of microseconds to sleep. |
488 | * |
489 | * |
489 | */ |
490 | */ |
490 | void thread_usleep(__u32 usec) |
491 | void thread_usleep(__u32 usec) |
491 | { |
492 | { |
492 | waitq_t wq; |
493 | waitq_t wq; |
493 | 494 | ||
494 | waitq_initialize(&wq); |
495 | waitq_initialize(&wq); |
495 | 496 | ||
496 | (void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING); |
497 | (void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING); |
497 | } |
498 | } |
498 | 499 | ||
499 | /** Register thread out-of-context invocation |
500 | /** Register thread out-of-context invocation |
500 | * |
501 | * |
501 | * Register a function and its argument to be executed |
502 | * Register a function and its argument to be executed |
502 | * on next context switch to the current thread. |
503 | * on next context switch to the current thread. |
503 | * |
504 | * |
504 | * @param call_me Out-of-context function. |
505 | * @param call_me Out-of-context function. |
505 | * @param call_me_with Out-of-context function argument. |
506 | * @param call_me_with Out-of-context function argument. |
506 | * |
507 | * |
507 | */ |
508 | */ |
508 | void thread_register_call_me(void (* call_me)(void *), void *call_me_with) |
509 | void thread_register_call_me(void (* call_me)(void *), void *call_me_with) |
509 | { |
510 | { |
510 | ipl_t ipl; |
511 | ipl_t ipl; |
511 | 512 | ||
512 | ipl = interrupts_disable(); |
513 | ipl = interrupts_disable(); |
513 | spinlock_lock(&THREAD->lock); |
514 | spinlock_lock(&THREAD->lock); |
514 | THREAD->call_me = call_me; |
515 | THREAD->call_me = call_me; |
515 | THREAD->call_me_with = call_me_with; |
516 | THREAD->call_me_with = call_me_with; |
516 | spinlock_unlock(&THREAD->lock); |
517 | spinlock_unlock(&THREAD->lock); |
517 | interrupts_restore(ipl); |
518 | interrupts_restore(ipl); |
518 | } |
519 | } |
519 | 520 | ||
520 | /** Print list of threads debug info */ |
521 | /** Print list of threads debug info */ |
521 | void thread_print_list(void) |
522 | void thread_print_list(void) |
522 | { |
523 | { |
523 | link_t *cur; |
524 | link_t *cur; |
524 | ipl_t ipl; |
525 | ipl_t ipl; |
525 | 526 | ||
526 | /* Messing with thread structures, avoid deadlock */ |
527 | /* Messing with thread structures, avoid deadlock */ |
527 | ipl = interrupts_disable(); |
528 | ipl = interrupts_disable(); |
528 | spinlock_lock(&threads_lock); |
529 | spinlock_lock(&threads_lock); |
529 | 530 | ||
530 | for (cur = threads_btree.leaf_head.next; cur != &threads_btree.leaf_head; cur = cur->next) { |
531 | for (cur = threads_btree.leaf_head.next; cur != &threads_btree.leaf_head; cur = cur->next) { |
531 | btree_node_t *node; |
532 | btree_node_t *node; |
532 | int i; |
533 | int i; |
533 | 534 | ||
534 | node = list_get_instance(cur, btree_node_t, leaf_link); |
535 | node = list_get_instance(cur, btree_node_t, leaf_link); |
535 | for (i = 0; i < node->keys; i++) { |
536 | for (i = 0; i < node->keys; i++) { |
536 | thread_t *t; |
537 | thread_t *t; |
537 | 538 | ||
538 | t = (thread_t *) node->value[i]; |
539 | t = (thread_t *) node->value[i]; |
539 | printf("%s: address=%#zX, tid=%zd, state=%s, task=%#zX, code=%#zX, stack=%#zX, cpu=", |
540 | printf("%s: address=%#zX, tid=%zd, state=%s, task=%#zX, code=%#zX, stack=%#zX, cpu=", |
540 | t->name, t, t->tid, thread_states[t->state], t->task, t->thread_code, t->kstack); |
541 | t->name, t, t->tid, thread_states[t->state], t->task, t->thread_code, t->kstack); |
541 | if (t->cpu) |
542 | if (t->cpu) |
542 | printf("cpu%zd", t->cpu->id); |
543 | printf("cpu%zd", t->cpu->id); |
543 | else |
544 | else |
544 | printf("none"); |
545 | printf("none"); |
545 | if (t->state == Sleeping) { |
546 | if (t->state == Sleeping) { |
546 | printf(", kst=%#zX", t->kstack); |
547 | printf(", kst=%#zX", t->kstack); |
547 | printf(", wq=%#zX", t->sleep_queue); |
548 | printf(", wq=%#zX", t->sleep_queue); |
548 | } |
549 | } |
549 | printf("\n"); |
550 | printf("\n"); |
550 | } |
551 | } |
551 | } |
552 | } |
552 | 553 | ||
553 | spinlock_unlock(&threads_lock); |
554 | spinlock_unlock(&threads_lock); |
554 | interrupts_restore(ipl); |
555 | interrupts_restore(ipl); |
555 | } |
556 | } |
556 | 557 | ||
557 | /** Check whether thread exists. |
558 | /** Check whether thread exists. |
558 | * |
559 | * |
559 | * Note that threads_lock must be already held and |
560 | * Note that threads_lock must be already held and |
560 | * interrupts must be already disabled. |
561 | * interrupts must be already disabled. |
561 | * |
562 | * |
562 | * @param t Pointer to thread. |
563 | * @param t Pointer to thread. |
563 | * |
564 | * |
564 | * @return True if thread t is known to the system, false otherwise. |
565 | * @return True if thread t is known to the system, false otherwise. |
565 | */ |
566 | */ |
566 | bool thread_exists(thread_t *t) |
567 | bool thread_exists(thread_t *t) |
567 | { |
568 | { |
568 | btree_node_t *leaf; |
569 | btree_node_t *leaf; |
569 | 570 | ||
570 | return btree_search(&threads_btree, (btree_key_t) ((__address) t), &leaf) != NULL; |
571 | return btree_search(&threads_btree, (btree_key_t) ((__address) t), &leaf) != NULL; |
571 | } |
572 | } |
572 | 573 | ||
573 | /** Process syscall to create new thread. |
574 | /** Process syscall to create new thread. |
574 | * |
575 | * |
575 | */ |
576 | */ |
576 | __native sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name) |
577 | __native sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name) |
577 | { |
578 | { |
578 | thread_t *t; |
579 | thread_t *t; |
579 | char namebuf[THREAD_NAME_BUFLEN]; |
580 | char namebuf[THREAD_NAME_BUFLEN]; |
580 | uspace_arg_t *kernel_uarg; |
581 | uspace_arg_t *kernel_uarg; |
581 | __u32 tid; |
582 | __u32 tid; |
582 | int rc; |
583 | int rc; |
583 | 584 | ||
584 | rc = copy_from_uspace(namebuf, uspace_name, THREAD_NAME_BUFLEN); |
585 | rc = copy_from_uspace(namebuf, uspace_name, THREAD_NAME_BUFLEN); |
585 | if (rc != 0) |
586 | if (rc != 0) |
586 | return (__native) rc; |
587 | return (__native) rc; |
587 | 588 | ||
588 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
589 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
589 | rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t)); |
590 | rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t)); |
590 | if (rc != 0) { |
591 | if (rc != 0) { |
591 | free(kernel_uarg); |
592 | free(kernel_uarg); |
592 | return (__native) rc; |
593 | return (__native) rc; |
593 | } |
594 | } |
594 | 595 | ||
595 | if ((t = thread_create(uinit, kernel_uarg, TASK, 0, namebuf))) { |
596 | if ((t = thread_create(uinit, kernel_uarg, TASK, 0, namebuf))) { |
596 | tid = t->tid; |
597 | tid = t->tid; |
597 | thread_ready(t); |
598 | thread_ready(t); |
598 | return (__native) tid; |
599 | return (__native) tid; |
599 | } else { |
600 | } else { |
600 | free(kernel_uarg); |
601 | free(kernel_uarg); |
601 | } |
602 | } |
602 | 603 | ||
603 | return (__native) ENOMEM; |
604 | return (__native) ENOMEM; |
604 | } |
605 | } |
605 | 606 | ||
606 | /** Process syscall to terminate thread. |
607 | /** Process syscall to terminate thread. |
607 | * |
608 | * |
608 | */ |
609 | */ |
609 | __native sys_thread_exit(int uspace_status) |
610 | __native sys_thread_exit(int uspace_status) |
610 | { |
611 | { |
611 | thread_exit(); |
612 | thread_exit(); |
612 | /* Unreachable */ |
613 | /* Unreachable */ |
613 | return 0; |
614 | return 0; |
614 | } |
615 | } |
615 | 616 |