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