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