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