Rev 2839 | Rev 2854 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 2839 | Rev 2848 | ||
---|---|---|---|
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/cycle.h> |
45 | #include <arch/cycle.h> |
46 | #include <arch.h> |
46 | #include <arch.h> |
47 | #include <synch/synch.h> |
47 | #include <synch/synch.h> |
48 | #include <synch/spinlock.h> |
48 | #include <synch/spinlock.h> |
49 | #include <synch/waitq.h> |
49 | #include <synch/waitq.h> |
50 | #include <synch/rwlock.h> |
50 | #include <synch/rwlock.h> |
51 | #include <cpu.h> |
51 | #include <cpu.h> |
52 | #include <func.h> |
52 | #include <func.h> |
53 | #include <context.h> |
53 | #include <context.h> |
54 | #include <adt/avl.h> |
54 | #include <adt/avl.h> |
55 | #include <adt/list.h> |
55 | #include <adt/list.h> |
56 | #include <time/clock.h> |
56 | #include <time/clock.h> |
57 | #include <time/timeout.h> |
57 | #include <time/timeout.h> |
58 | #include <config.h> |
58 | #include <config.h> |
59 | #include <arch/interrupt.h> |
59 | #include <arch/interrupt.h> |
60 | #include <smp/ipi.h> |
60 | #include <smp/ipi.h> |
61 | #include <arch/faddr.h> |
61 | #include <arch/faddr.h> |
62 | #include <atomic.h> |
62 | #include <atomic.h> |
63 | #include <memstr.h> |
63 | #include <memstr.h> |
64 | #include <print.h> |
64 | #include <print.h> |
65 | #include <mm/slab.h> |
65 | #include <mm/slab.h> |
66 | #include <debug.h> |
66 | #include <debug.h> |
67 | #include <main/uinit.h> |
67 | #include <main/uinit.h> |
68 | #include <syscall/copy.h> |
68 | #include <syscall/copy.h> |
69 | #include <errno.h> |
69 | #include <errno.h> |
70 | #include <console/klog.h> |
70 | #include <console/klog.h> |
71 | 71 | ||
72 | 72 | ||
73 | /** Thread states */ |
73 | /** Thread states */ |
74 | char *thread_states[] = { |
74 | char *thread_states[] = { |
75 | "Invalid", |
75 | "Invalid", |
76 | "Running", |
76 | "Running", |
77 | "Sleeping", |
77 | "Sleeping", |
78 | "Ready", |
78 | "Ready", |
79 | "Entering", |
79 | "Entering", |
80 | "Exiting", |
80 | "Exiting", |
81 | "Lingering" |
81 | "Lingering" |
82 | }; |
82 | }; |
83 | 83 | ||
84 | /** Lock protecting the threads_tree AVL tree. |
84 | /** Lock protecting the threads_tree AVL tree. |
85 | * |
85 | * |
86 | * For locking rules, see declaration thereof. |
86 | * For locking rules, see declaration thereof. |
87 | */ |
87 | */ |
88 | SPINLOCK_INITIALIZE(threads_lock); |
88 | SPINLOCK_INITIALIZE(threads_lock); |
89 | 89 | ||
90 | /** ALV tree of all threads. |
90 | /** ALV tree of all threads. |
91 | * |
91 | * |
92 | * When a thread is found in the threads_tree AVL tree, it is guaranteed to |
92 | * When a thread is found in the threads_tree AVL tree, it is guaranteed to |
93 | * exist as long as the threads_lock is held. |
93 | * exist as long as the threads_lock is held. |
94 | */ |
94 | */ |
95 | avltree_t threads_tree; |
95 | avltree_t threads_tree; |
96 | 96 | ||
97 | SPINLOCK_INITIALIZE(tidlock); |
97 | SPINLOCK_INITIALIZE(tidlock); |
98 | thread_id_t last_tid = 0; |
98 | thread_id_t last_tid = 0; |
99 | 99 | ||
100 | static slab_cache_t *thread_slab; |
100 | static slab_cache_t *thread_slab; |
101 | #ifdef ARCH_HAS_FPU |
101 | #ifdef ARCH_HAS_FPU |
102 | slab_cache_t *fpu_context_slab; |
102 | slab_cache_t *fpu_context_slab; |
103 | #endif |
103 | #endif |
104 | 104 | ||
105 | /** Thread wrapper. |
105 | /** Thread wrapper. |
106 | * |
106 | * |
107 | * This wrapper is provided to ensure that every thread makes a call to |
107 | * This wrapper is provided to ensure that every thread makes a call to |
108 | * thread_exit() when its implementing function returns. |
108 | * thread_exit() when its implementing function returns. |
109 | * |
109 | * |
110 | * interrupts_disable() is assumed. |
110 | * interrupts_disable() is assumed. |
111 | * |
111 | * |
112 | */ |
112 | */ |
113 | static void cushion(void) |
113 | static void cushion(void) |
114 | { |
114 | { |
115 | void (*f)(void *) = THREAD->thread_code; |
115 | void (*f)(void *) = THREAD->thread_code; |
116 | void *arg = THREAD->thread_arg; |
116 | void *arg = THREAD->thread_arg; |
117 | THREAD->last_cycle = get_cycle(); |
117 | THREAD->last_cycle = get_cycle(); |
118 | 118 | ||
119 | /* This is where each thread wakes up after its creation */ |
119 | /* This is where each thread wakes up after its creation */ |
120 | spinlock_unlock(&THREAD->lock); |
120 | spinlock_unlock(&THREAD->lock); |
121 | interrupts_enable(); |
121 | interrupts_enable(); |
122 | 122 | ||
123 | f(arg); |
123 | f(arg); |
124 | 124 | ||
125 | /* Accumulate accounting to the task */ |
125 | /* Accumulate accounting to the task */ |
126 | ipl_t ipl = interrupts_disable(); |
126 | ipl_t ipl = interrupts_disable(); |
127 | 127 | ||
128 | spinlock_lock(&THREAD->lock); |
128 | spinlock_lock(&THREAD->lock); |
129 | if (!THREAD->uncounted) { |
129 | if (!THREAD->uncounted) { |
130 | thread_update_accounting(); |
130 | thread_update_accounting(); |
131 | uint64_t cycles = THREAD->cycles; |
131 | uint64_t cycles = THREAD->cycles; |
132 | THREAD->cycles = 0; |
132 | THREAD->cycles = 0; |
133 | spinlock_unlock(&THREAD->lock); |
133 | spinlock_unlock(&THREAD->lock); |
134 | 134 | ||
135 | spinlock_lock(&TASK->lock); |
135 | spinlock_lock(&TASK->lock); |
136 | TASK->cycles += cycles; |
136 | TASK->cycles += cycles; |
137 | spinlock_unlock(&TASK->lock); |
137 | spinlock_unlock(&TASK->lock); |
138 | } else |
138 | } else |
139 | spinlock_unlock(&THREAD->lock); |
139 | spinlock_unlock(&THREAD->lock); |
140 | 140 | ||
141 | interrupts_restore(ipl); |
141 | interrupts_restore(ipl); |
142 | 142 | ||
143 | thread_exit(); |
143 | thread_exit(); |
144 | /* not reached */ |
144 | /* not reached */ |
145 | } |
145 | } |
146 | 146 | ||
147 | /** Initialization and allocation for thread_t structure */ |
147 | /** Initialization and allocation for thread_t structure */ |
148 | static int thr_constructor(void *obj, int kmflags) |
148 | static int thr_constructor(void *obj, int kmflags) |
149 | { |
149 | { |
150 | thread_t *t = (thread_t *) obj; |
150 | thread_t *t = (thread_t *) obj; |
151 | 151 | ||
152 | spinlock_initialize(&t->lock, "thread_t_lock"); |
152 | spinlock_initialize(&t->lock, "thread_t_lock"); |
153 | link_initialize(&t->rq_link); |
153 | link_initialize(&t->rq_link); |
154 | link_initialize(&t->wq_link); |
154 | link_initialize(&t->wq_link); |
155 | link_initialize(&t->th_link); |
155 | link_initialize(&t->th_link); |
156 | 156 | ||
157 | /* call the architecture-specific part of the constructor */ |
157 | /* call the architecture-specific part of the constructor */ |
158 | thr_constructor_arch(t); |
158 | thr_constructor_arch(t); |
159 | 159 | ||
160 | #ifdef ARCH_HAS_FPU |
160 | #ifdef ARCH_HAS_FPU |
161 | #ifdef CONFIG_FPU_LAZY |
161 | #ifdef CONFIG_FPU_LAZY |
162 | t->saved_fpu_context = NULL; |
162 | t->saved_fpu_context = NULL; |
163 | #else |
163 | #else |
164 | t->saved_fpu_context = slab_alloc(fpu_context_slab, kmflags); |
164 | t->saved_fpu_context = slab_alloc(fpu_context_slab, kmflags); |
165 | if (!t->saved_fpu_context) |
165 | if (!t->saved_fpu_context) |
166 | return -1; |
166 | return -1; |
167 | #endif |
167 | #endif |
168 | #endif |
168 | #endif |
169 | 169 | ||
170 | t->kstack = (uint8_t *) frame_alloc(STACK_FRAMES, FRAME_KA | kmflags); |
170 | t->kstack = (uint8_t *) frame_alloc(STACK_FRAMES, FRAME_KA | kmflags); |
171 | if (!t->kstack) { |
171 | if (!t->kstack) { |
172 | #ifdef ARCH_HAS_FPU |
172 | #ifdef ARCH_HAS_FPU |
173 | if (t->saved_fpu_context) |
173 | if (t->saved_fpu_context) |
174 | slab_free(fpu_context_slab, t->saved_fpu_context); |
174 | slab_free(fpu_context_slab, t->saved_fpu_context); |
175 | #endif |
175 | #endif |
176 | return -1; |
176 | return -1; |
177 | } |
177 | } |
178 | 178 | ||
- | 179 | spinlock_initialize(&t->debug_lock, "thread_debug_lock"); |
|
- | 180 | ||
179 | return 0; |
181 | return 0; |
180 | } |
182 | } |
181 | 183 | ||
182 | /** Destruction of thread_t object */ |
184 | /** Destruction of thread_t object */ |
183 | static int thr_destructor(void *obj) |
185 | static int thr_destructor(void *obj) |
184 | { |
186 | { |
185 | thread_t *t = (thread_t *) obj; |
187 | thread_t *t = (thread_t *) obj; |
186 | 188 | ||
187 | /* call the architecture-specific part of the destructor */ |
189 | /* call the architecture-specific part of the destructor */ |
188 | thr_destructor_arch(t); |
190 | thr_destructor_arch(t); |
189 | 191 | ||
190 | frame_free(KA2PA(t->kstack)); |
192 | frame_free(KA2PA(t->kstack)); |
191 | #ifdef ARCH_HAS_FPU |
193 | #ifdef ARCH_HAS_FPU |
192 | if (t->saved_fpu_context) |
194 | if (t->saved_fpu_context) |
193 | slab_free(fpu_context_slab, t->saved_fpu_context); |
195 | slab_free(fpu_context_slab, t->saved_fpu_context); |
194 | #endif |
196 | #endif |
195 | return 1; /* One page freed */ |
197 | return 1; /* One page freed */ |
196 | } |
198 | } |
197 | 199 | ||
198 | /** Initialize threads |
200 | /** Initialize threads |
199 | * |
201 | * |
200 | * Initialize kernel threads support. |
202 | * Initialize kernel threads support. |
201 | * |
203 | * |
202 | */ |
204 | */ |
203 | void thread_init(void) |
205 | void thread_init(void) |
204 | { |
206 | { |
205 | THREAD = NULL; |
207 | THREAD = NULL; |
206 | atomic_set(&nrdy,0); |
208 | atomic_set(&nrdy,0); |
207 | thread_slab = slab_cache_create("thread_slab", sizeof(thread_t), 0, |
209 | thread_slab = slab_cache_create("thread_slab", sizeof(thread_t), 0, |
208 | thr_constructor, thr_destructor, 0); |
210 | thr_constructor, thr_destructor, 0); |
209 | 211 | ||
210 | #ifdef ARCH_HAS_FPU |
212 | #ifdef ARCH_HAS_FPU |
211 | fpu_context_slab = slab_cache_create("fpu_slab", sizeof(fpu_context_t), |
213 | fpu_context_slab = slab_cache_create("fpu_slab", sizeof(fpu_context_t), |
212 | FPU_CONTEXT_ALIGN, NULL, NULL, 0); |
214 | FPU_CONTEXT_ALIGN, NULL, NULL, 0); |
213 | #endif |
215 | #endif |
214 | 216 | ||
215 | avltree_create(&threads_tree); |
217 | avltree_create(&threads_tree); |
216 | } |
218 | } |
217 | 219 | ||
218 | /** Make thread ready |
220 | /** Make thread ready |
219 | * |
221 | * |
220 | * Switch thread t to the ready state. |
222 | * Switch thread t to the ready state. |
221 | * |
223 | * |
222 | * @param t Thread to make ready. |
224 | * @param t Thread to make ready. |
223 | * |
225 | * |
224 | */ |
226 | */ |
225 | void thread_ready(thread_t *t) |
227 | void thread_ready(thread_t *t) |
226 | { |
228 | { |
227 | cpu_t *cpu; |
229 | cpu_t *cpu; |
228 | runq_t *r; |
230 | runq_t *r; |
229 | ipl_t ipl; |
231 | ipl_t ipl; |
230 | int i, avg; |
232 | int i, avg; |
231 | 233 | ||
232 | ipl = interrupts_disable(); |
234 | ipl = interrupts_disable(); |
233 | 235 | ||
234 | spinlock_lock(&t->lock); |
236 | spinlock_lock(&t->lock); |
235 | 237 | ||
236 | ASSERT(!(t->state == Ready)); |
238 | ASSERT(!(t->state == Ready)); |
237 | 239 | ||
238 | i = (t->priority < RQ_COUNT - 1) ? ++t->priority : t->priority; |
240 | i = (t->priority < RQ_COUNT - 1) ? ++t->priority : t->priority; |
239 | 241 | ||
240 | cpu = CPU; |
242 | cpu = CPU; |
241 | if (t->flags & THREAD_FLAG_WIRED) { |
243 | if (t->flags & THREAD_FLAG_WIRED) { |
242 | ASSERT(t->cpu != NULL); |
244 | ASSERT(t->cpu != NULL); |
243 | cpu = t->cpu; |
245 | cpu = t->cpu; |
244 | } |
246 | } |
245 | t->state = Ready; |
247 | t->state = Ready; |
246 | spinlock_unlock(&t->lock); |
248 | spinlock_unlock(&t->lock); |
247 | 249 | ||
248 | /* |
250 | /* |
249 | * Append t to respective ready queue on respective processor. |
251 | * Append t to respective ready queue on respective processor. |
250 | */ |
252 | */ |
251 | r = &cpu->rq[i]; |
253 | r = &cpu->rq[i]; |
252 | spinlock_lock(&r->lock); |
254 | spinlock_lock(&r->lock); |
253 | list_append(&t->rq_link, &r->rq_head); |
255 | list_append(&t->rq_link, &r->rq_head); |
254 | r->n++; |
256 | r->n++; |
255 | spinlock_unlock(&r->lock); |
257 | spinlock_unlock(&r->lock); |
256 | 258 | ||
257 | atomic_inc(&nrdy); |
259 | atomic_inc(&nrdy); |
258 | avg = atomic_get(&nrdy) / config.cpu_active; |
260 | avg = atomic_get(&nrdy) / config.cpu_active; |
259 | atomic_inc(&cpu->nrdy); |
261 | atomic_inc(&cpu->nrdy); |
260 | 262 | ||
261 | interrupts_restore(ipl); |
263 | interrupts_restore(ipl); |
262 | } |
264 | } |
263 | 265 | ||
264 | /** Create new thread |
266 | /** Create new thread |
265 | * |
267 | * |
266 | * Create a new thread. |
268 | * Create a new thread. |
267 | * |
269 | * |
268 | * @param func Thread's implementing function. |
270 | * @param func Thread's implementing function. |
269 | * @param arg Thread's implementing function argument. |
271 | * @param arg Thread's implementing function argument. |
270 | * @param task Task to which the thread belongs. |
272 | * @param task Task to which the thread belongs. |
271 | * @param flags Thread flags. |
273 | * @param flags Thread flags. |
272 | * @param name Symbolic name. |
274 | * @param name Symbolic name. |
273 | * @param uncounted Thread's accounting doesn't affect accumulated task |
275 | * @param uncounted Thread's accounting doesn't affect accumulated task |
274 | * accounting. |
276 | * accounting. |
275 | * |
277 | * |
276 | * @return New thread's structure on success, NULL on failure. |
278 | * @return New thread's structure on success, NULL on failure. |
277 | * |
279 | * |
278 | */ |
280 | */ |
279 | thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, |
281 | thread_t *thread_create(void (* func)(void *), void *arg, task_t *task, |
280 | int flags, char *name, bool uncounted) |
282 | int flags, char *name, bool uncounted) |
281 | { |
283 | { |
282 | thread_t *t; |
284 | thread_t *t; |
283 | ipl_t ipl; |
285 | ipl_t ipl; |
284 | 286 | ||
285 | t = (thread_t *) slab_alloc(thread_slab, 0); |
287 | t = (thread_t *) slab_alloc(thread_slab, 0); |
286 | if (!t) |
288 | if (!t) |
287 | return NULL; |
289 | return NULL; |
288 | 290 | ||
289 | /* Not needed, but good for debugging */ |
291 | /* Not needed, but good for debugging */ |
290 | memsetb((uintptr_t) t->kstack, THREAD_STACK_SIZE * 1 << STACK_FRAMES, |
292 | memsetb((uintptr_t) t->kstack, THREAD_STACK_SIZE * 1 << STACK_FRAMES, |
291 | 0); |
293 | 0); |
292 | 294 | ||
293 | ipl = interrupts_disable(); |
295 | ipl = interrupts_disable(); |
294 | spinlock_lock(&tidlock); |
296 | spinlock_lock(&tidlock); |
295 | t->tid = ++last_tid; |
297 | t->tid = ++last_tid; |
296 | spinlock_unlock(&tidlock); |
298 | spinlock_unlock(&tidlock); |
297 | interrupts_restore(ipl); |
299 | interrupts_restore(ipl); |
298 | 300 | ||
299 | context_save(&t->saved_context); |
301 | context_save(&t->saved_context); |
300 | context_set(&t->saved_context, FADDR(cushion), (uintptr_t) t->kstack, |
302 | context_set(&t->saved_context, FADDR(cushion), (uintptr_t) t->kstack, |
301 | THREAD_STACK_SIZE); |
303 | THREAD_STACK_SIZE); |
302 | 304 | ||
303 | the_initialize((the_t *) t->kstack); |
305 | the_initialize((the_t *) t->kstack); |
304 | 306 | ||
305 | ipl = interrupts_disable(); |
307 | ipl = interrupts_disable(); |
306 | t->saved_context.ipl = interrupts_read(); |
308 | t->saved_context.ipl = interrupts_read(); |
307 | interrupts_restore(ipl); |
309 | interrupts_restore(ipl); |
308 | 310 | ||
309 | memcpy(t->name, name, THREAD_NAME_BUFLEN); |
311 | memcpy(t->name, name, THREAD_NAME_BUFLEN); |
310 | 312 | ||
311 | t->thread_code = func; |
313 | t->thread_code = func; |
312 | t->thread_arg = arg; |
314 | t->thread_arg = arg; |
313 | t->ticks = -1; |
315 | t->ticks = -1; |
314 | t->cycles = 0; |
316 | t->cycles = 0; |
315 | t->uncounted = uncounted; |
317 | t->uncounted = uncounted; |
316 | t->priority = -1; /* start in rq[0] */ |
318 | t->priority = -1; /* start in rq[0] */ |
317 | t->cpu = NULL; |
319 | t->cpu = NULL; |
318 | t->flags = flags; |
320 | t->flags = flags; |
319 | t->state = Entering; |
321 | t->state = Entering; |
320 | t->call_me = NULL; |
322 | t->call_me = NULL; |
321 | t->call_me_with = NULL; |
323 | t->call_me_with = NULL; |
322 | 324 | ||
323 | timeout_initialize(&t->sleep_timeout); |
325 | timeout_initialize(&t->sleep_timeout); |
324 | t->sleep_interruptible = false; |
326 | t->sleep_interruptible = false; |
325 | t->sleep_queue = NULL; |
327 | t->sleep_queue = NULL; |
326 | t->timeout_pending = 0; |
328 | t->timeout_pending = 0; |
327 | 329 | ||
328 | t->in_copy_from_uspace = false; |
330 | t->in_copy_from_uspace = false; |
329 | t->in_copy_to_uspace = false; |
331 | t->in_copy_to_uspace = false; |
330 | 332 | ||
331 | t->interrupted = false; |
333 | t->interrupted = false; |
332 | t->detached = false; |
334 | t->detached = false; |
333 | waitq_initialize(&t->join_wq); |
335 | waitq_initialize(&t->join_wq); |
334 | 336 | ||
335 | t->rwlock_holder_type = RWLOCK_NONE; |
337 | t->rwlock_holder_type = RWLOCK_NONE; |
336 | 338 | ||
337 | t->task = task; |
339 | t->task = task; |
338 | 340 | ||
339 | t->fpu_context_exists = 0; |
341 | t->fpu_context_exists = 0; |
340 | t->fpu_context_engaged = 0; |
342 | t->fpu_context_engaged = 0; |
341 | 343 | ||
342 | avltree_node_initialize(&t->threads_tree_node); |
344 | avltree_node_initialize(&t->threads_tree_node); |
343 | t->threads_tree_node.key = (uintptr_t) t; |
345 | t->threads_tree_node.key = (uintptr_t) t; |
344 | 346 | ||
345 | /* Init debugging stuff */ |
347 | /* Init debugging stuff */ |
346 | waitq_initialize(&t->go_wq); |
348 | waitq_initialize(&t->go_wq); |
347 | t->debug_go_call = NULL; |
349 | t->debug_go_call = NULL; |
348 | t->uspace_state = NULL; |
350 | t->uspace_state = NULL; |
349 | t->debug_stop = true; |
351 | t->debug_stop = true; |
350 | t->debug_active = false; |
352 | t->debug_active = false; |
351 | 353 | ||
352 | /* might depend on previous initialization */ |
354 | /* might depend on previous initialization */ |
353 | thread_create_arch(t); |
355 | thread_create_arch(t); |
354 | 356 | ||
355 | if (!(flags & THREAD_FLAG_NOATTACH)) |
357 | if (!(flags & THREAD_FLAG_NOATTACH)) |
356 | thread_attach(t, task); |
358 | thread_attach(t, task); |
357 | 359 | ||
358 | return t; |
360 | return t; |
359 | } |
361 | } |
360 | 362 | ||
361 | /** Destroy thread structure of an unattached thread. |
363 | /** Destroy thread structure of an unattached thread. |
362 | * |
364 | * |
363 | * Thread t must only have been created and never attached. |
365 | * Thread t must only have been created and never attached. |
364 | */ |
366 | */ |
365 | void thread_unattached_free(thread_t *t) |
367 | void thread_unattached_free(thread_t *t) |
366 | { |
368 | { |
367 | slab_free(thread_slab, t); |
369 | slab_free(thread_slab, t); |
368 | } |
370 | } |
369 | 371 | ||
370 | 372 | ||
371 | /** Destroy thread memory structure |
373 | /** Destroy thread memory structure |
372 | * |
374 | * |
373 | * Detach thread from all queues, cpus etc. and destroy it. |
375 | * Detach thread from all queues, cpus etc. and destroy it. |
374 | * |
376 | * |
375 | * Assume thread->lock is held!! |
377 | * Assume thread->lock is held!! |
376 | */ |
378 | */ |
377 | void thread_destroy(thread_t *t) |
379 | void thread_destroy(thread_t *t) |
378 | { |
380 | { |
379 | ASSERT(t->state == Exiting || t->state == Lingering); |
381 | ASSERT(t->state == Exiting || t->state == Lingering); |
380 | ASSERT(t->task); |
382 | ASSERT(t->task); |
381 | ASSERT(t->cpu); |
383 | ASSERT(t->cpu); |
382 | 384 | ||
383 | spinlock_lock(&t->cpu->lock); |
385 | spinlock_lock(&t->cpu->lock); |
384 | if (t->cpu->fpu_owner == t) |
386 | if (t->cpu->fpu_owner == t) |
385 | t->cpu->fpu_owner = NULL; |
387 | t->cpu->fpu_owner = NULL; |
386 | spinlock_unlock(&t->cpu->lock); |
388 | spinlock_unlock(&t->cpu->lock); |
387 | 389 | ||
388 | spinlock_unlock(&t->lock); |
390 | spinlock_unlock(&t->lock); |
389 | 391 | ||
390 | spinlock_lock(&threads_lock); |
392 | spinlock_lock(&threads_lock); |
391 | avltree_delete(&threads_tree, &t->threads_tree_node); |
393 | avltree_delete(&threads_tree, &t->threads_tree_node); |
392 | spinlock_unlock(&threads_lock); |
394 | spinlock_unlock(&threads_lock); |
393 | 395 | ||
394 | /* |
396 | /* |
395 | * Detach from the containing task. |
397 | * Detach from the containing task. |
396 | */ |
398 | */ |
397 | spinlock_lock(&t->task->lock); |
399 | spinlock_lock(&t->task->lock); |
398 | list_remove(&t->th_link); |
400 | list_remove(&t->th_link); |
399 | spinlock_unlock(&t->task->lock); |
401 | spinlock_unlock(&t->task->lock); |
400 | 402 | ||
401 | /* |
403 | /* |
402 | * t is guaranteed to be the very last thread of its task. |
404 | * t is guaranteed to be the very last thread of its task. |
403 | * It is safe to destroy the task. |
405 | * It is safe to destroy the task. |
404 | */ |
406 | */ |
405 | if (atomic_predec(&t->task->refcount) == 0) |
407 | if (atomic_predec(&t->task->refcount) == 0) |
406 | task_destroy(t->task); |
408 | task_destroy(t->task); |
407 | 409 | ||
408 | slab_free(thread_slab, t); |
410 | slab_free(thread_slab, t); |
409 | } |
411 | } |
410 | 412 | ||
411 | /** Attach thread to the given task. |
413 | /** Attach thread to the given task. |
412 | * |
414 | * |
413 | * The task's lock must already be held and interrupts must be disabled. |
415 | * The task's lock must already be held and interrupts must be disabled. |
414 | * |
416 | * |
415 | * @param t Thread to be attached to the task. |
417 | * @param t Thread to be attached to the task. |
416 | * @param task Task to which the thread is to be attached. |
418 | * @param task Task to which the thread is to be attached. |
417 | */ |
419 | */ |
418 | static void _thread_attach_task(thread_t *t, task_t *task) |
420 | static void _thread_attach_task(thread_t *t, task_t *task) |
419 | { |
421 | { |
420 | atomic_inc(&task->refcount); |
422 | atomic_inc(&task->refcount); |
421 | atomic_inc(&task->lifecount); |
423 | atomic_inc(&task->lifecount); |
422 | 424 | ||
423 | /* FIXME: this must be done very carefully.. an unstoppable |
425 | /* FIXME: this must be done very carefully.. an unstoppable |
424 | thread cannot appear just-so, it must be possible to catch it. */ |
426 | thread cannot appear just-so, it must be possible to catch it. */ |
425 | if (t->flags & THREAD_FLAG_USPACE) |
427 | if (t->flags & THREAD_FLAG_USPACE) |
426 | ++task->not_stoppable_count; |
428 | ++task->not_stoppable_count; |
427 | list_append(&t->th_link, &task->th_head); |
429 | list_append(&t->th_link, &task->th_head); |
428 | } |
430 | } |
429 | 431 | ||
430 | /** Add thread to the threads tree. |
432 | /** Add thread to the threads tree. |
431 | * |
433 | * |
432 | * Interrupts must be already disabled. |
434 | * Interrupts must be already disabled. |
433 | * |
435 | * |
434 | * @param t Thread to be attached to the task. |
436 | * @param t Thread to be attached to the task. |
435 | * @param task Task to which the thread is to be attached. |
437 | * @param task Task to which the thread is to be attached. |
436 | */ |
438 | */ |
437 | static void _thread_attach_tree(thread_t *t) |
439 | static void _thread_attach_tree(thread_t *t) |
438 | { |
440 | { |
439 | /* |
441 | /* |
440 | * Register this thread in the system-wide list. |
442 | * Register this thread in the system-wide list. |
441 | */ |
443 | */ |
442 | spinlock_lock(&threads_lock); |
444 | spinlock_lock(&threads_lock); |
443 | avltree_insert(&threads_tree, &t->threads_tree_node); |
445 | avltree_insert(&threads_tree, &t->threads_tree_node); |
444 | spinlock_unlock(&threads_lock); |
446 | spinlock_unlock(&threads_lock); |
445 | } |
447 | } |
446 | 448 | ||
447 | 449 | ||
448 | /** Make the thread visible to the system. |
450 | /** Make the thread visible to the system. |
449 | * |
451 | * |
450 | * Attach the thread structure to the current task and make it visible in the |
452 | * Attach the thread structure to the current task and make it visible in the |
451 | * threads_tree. |
453 | * threads_tree. |
452 | * |
454 | * |
453 | * @param t Thread to be attached to the task. |
455 | * @param t Thread to be attached to the task. |
454 | * @param task Task to which the thread is to be attached. |
456 | * @param task Task to which the thread is to be attached. |
455 | */ |
457 | */ |
456 | void thread_attach(thread_t *t, task_t *task) |
458 | void thread_attach(thread_t *t, task_t *task) |
457 | { |
459 | { |
458 | ipl_t ipl; |
460 | ipl_t ipl; |
459 | 461 | ||
460 | ipl = interrupts_disable(); |
462 | ipl = interrupts_disable(); |
461 | 463 | ||
462 | /* |
464 | /* |
463 | * Attach to the current task. |
465 | * Attach to the current task. |
464 | */ |
466 | */ |
465 | spinlock_lock(&task->lock); |
467 | spinlock_lock(&task->lock); |
466 | _thread_attach_task(t, task); |
468 | _thread_attach_task(t, task); |
467 | spinlock_unlock(&task->lock); |
469 | spinlock_unlock(&task->lock); |
468 | 470 | ||
469 | /* |
471 | /* |
470 | * Register this thread in the system-wide list. |
472 | * Register this thread in the system-wide list. |
471 | */ |
473 | */ |
472 | _thread_attach_tree(t); |
474 | _thread_attach_tree(t); |
473 | 475 | ||
474 | interrupts_restore(ipl); |
476 | interrupts_restore(ipl); |
475 | } |
477 | } |
476 | 478 | ||
477 | /** Attach thread to a task given by its ID. |
479 | /** Attach thread to a task given by its ID. |
478 | * |
480 | * |
479 | * Unlike thread_attach(), this function allows to attach a thread |
481 | * Unlike thread_attach(), this function allows to attach a thread |
480 | * to an arbitrary task. |
482 | * to an arbitrary task. |
481 | * |
483 | * |
482 | * @param t Thread to be attached to the task. |
484 | * @param t Thread to be attached to the task. |
483 | * @param taskid Task id to which the thread is to be attached. |
485 | * @param taskid Task id to which the thread is to be attached. |
484 | * @return An error code from errno.h |
486 | * @return An error code from errno.h |
485 | */ |
487 | */ |
486 | int thread_attach_by_id(thread_t *t, task_id_t taskid) |
488 | int thread_attach_by_id(thread_t *t, task_id_t taskid) |
487 | { |
489 | { |
488 | ipl_t ipl; |
490 | ipl_t ipl; |
489 | task_t *task; |
491 | task_t *task; |
490 | 492 | ||
491 | ipl = interrupts_disable(); |
493 | ipl = interrupts_disable(); |
492 | 494 | ||
493 | spinlock_lock(&tasks_lock); |
495 | spinlock_lock(&tasks_lock); |
494 | task = task_find_by_id(taskid); |
496 | task = task_find_by_id(taskid); |
495 | if (task == NULL) { |
497 | if (task == NULL) { |
496 | spinlock_unlock(&tasks_lock); |
498 | spinlock_unlock(&tasks_lock); |
497 | interrupts_restore(ipl); |
499 | interrupts_restore(ipl); |
498 | return ENOENT; |
500 | return ENOENT; |
499 | } |
501 | } |
500 | 502 | ||
501 | spinlock_lock(&task->lock); |
503 | spinlock_lock(&task->lock); |
502 | spinlock_unlock(&tasks_lock); |
504 | spinlock_unlock(&tasks_lock); |
503 | 505 | ||
504 | /* |
506 | /* |
505 | * Attach to the current task. |
507 | * Attach to the current task. |
506 | */ |
508 | */ |
507 | _thread_attach_task(t, task); |
509 | _thread_attach_task(t, task); |
508 | 510 | ||
509 | spinlock_unlock(&task->lock); |
511 | spinlock_unlock(&task->lock); |
510 | 512 | ||
511 | /* |
513 | /* |
512 | * Register this thread in the system-wide list. |
514 | * Register this thread in the system-wide list. |
513 | */ |
515 | */ |
514 | _thread_attach_tree(t); |
516 | _thread_attach_tree(t); |
515 | 517 | ||
516 | interrupts_restore(ipl); |
518 | interrupts_restore(ipl); |
517 | 519 | ||
518 | return EOK; |
520 | return EOK; |
519 | } |
521 | } |
520 | 522 | ||
521 | 523 | ||
522 | /** Terminate thread. |
524 | /** Terminate thread. |
523 | * |
525 | * |
524 | * End current thread execution and switch it to the exiting state. All pending |
526 | * End current thread execution and switch it to the exiting state. All pending |
525 | * timeouts are executed. |
527 | * timeouts are executed. |
526 | */ |
528 | */ |
527 | void thread_exit(void) |
529 | void thread_exit(void) |
528 | { |
530 | { |
529 | ipl_t ipl; |
531 | ipl_t ipl; |
530 | 532 | ||
531 | if (atomic_predec(&TASK->lifecount) == 0) { |
533 | if (atomic_predec(&TASK->lifecount) == 0) { |
532 | /* |
534 | /* |
533 | * We are the last thread in the task that still has not exited. |
535 | * We are the last thread in the task that still has not exited. |
534 | * With the exception of the moment the task was created, new |
536 | * With the exception of the moment the task was created, new |
535 | * threads can only be created by threads of the same task. |
537 | * threads can only be created by threads of the same task. |
536 | * We are safe to perform cleanup. |
538 | * We are safe to perform cleanup. |
537 | */ |
539 | */ |
538 | if (THREAD->flags & THREAD_FLAG_USPACE) { |
540 | if (THREAD->flags & THREAD_FLAG_USPACE) { |
539 | ipc_cleanup(); |
541 | ipc_cleanup(); |
540 | futex_cleanup(); |
542 | futex_cleanup(); |
541 | klog_printf("Cleanup of task %llu completed.", |
543 | klog_printf("Cleanup of task %llu completed.", |
542 | TASK->taskid); |
544 | TASK->taskid); |
543 | } |
545 | } |
544 | } |
546 | } |
545 | 547 | ||
546 | restart: |
548 | restart: |
547 | ipl = interrupts_disable(); |
549 | ipl = interrupts_disable(); |
548 | spinlock_lock(&THREAD->lock); |
550 | spinlock_lock(&THREAD->lock); |
549 | if (THREAD->timeout_pending) { |
551 | if (THREAD->timeout_pending) { |
550 | /* busy waiting for timeouts in progress */ |
552 | /* busy waiting for timeouts in progress */ |
551 | spinlock_unlock(&THREAD->lock); |
553 | spinlock_unlock(&THREAD->lock); |
552 | interrupts_restore(ipl); |
554 | interrupts_restore(ipl); |
553 | goto restart; |
555 | goto restart; |
554 | } |
556 | } |
555 | 557 | ||
556 | THREAD->state = Exiting; |
558 | THREAD->state = Exiting; |
557 | spinlock_unlock(&THREAD->lock); |
559 | spinlock_unlock(&THREAD->lock); |
558 | scheduler(); |
560 | scheduler(); |
559 | 561 | ||
560 | /* Not reached */ |
562 | /* Not reached */ |
561 | while (1) |
563 | while (1) |
562 | ; |
564 | ; |
563 | } |
565 | } |
564 | 566 | ||
565 | 567 | ||
566 | /** Thread sleep |
568 | /** Thread sleep |
567 | * |
569 | * |
568 | * Suspend execution of the current thread. |
570 | * Suspend execution of the current thread. |
569 | * |
571 | * |
570 | * @param sec Number of seconds to sleep. |
572 | * @param sec Number of seconds to sleep. |
571 | * |
573 | * |
572 | */ |
574 | */ |
573 | void thread_sleep(uint32_t sec) |
575 | void thread_sleep(uint32_t sec) |
574 | { |
576 | { |
575 | thread_usleep(sec * 1000000); |
577 | thread_usleep(sec * 1000000); |
576 | } |
578 | } |
577 | 579 | ||
578 | /** Wait for another thread to exit. |
580 | /** Wait for another thread to exit. |
579 | * |
581 | * |
580 | * @param t Thread to join on exit. |
582 | * @param t Thread to join on exit. |
581 | * @param usec Timeout in microseconds. |
583 | * @param usec Timeout in microseconds. |
582 | * @param flags Mode of operation. |
584 | * @param flags Mode of operation. |
583 | * |
585 | * |
584 | * @return An error code from errno.h or an error code from synch.h. |
586 | * @return An error code from errno.h or an error code from synch.h. |
585 | */ |
587 | */ |
586 | int thread_join_timeout(thread_t *t, uint32_t usec, int flags) |
588 | int thread_join_timeout(thread_t *t, uint32_t usec, int flags) |
587 | { |
589 | { |
588 | ipl_t ipl; |
590 | ipl_t ipl; |
589 | int rc; |
591 | int rc; |
590 | 592 | ||
591 | if (t == THREAD) |
593 | if (t == THREAD) |
592 | return EINVAL; |
594 | return EINVAL; |
593 | 595 | ||
594 | /* |
596 | /* |
595 | * Since thread join can only be called once on an undetached thread, |
597 | * Since thread join can only be called once on an undetached thread, |
596 | * the thread pointer is guaranteed to be still valid. |
598 | * the thread pointer is guaranteed to be still valid. |
597 | */ |
599 | */ |
598 | 600 | ||
599 | ipl = interrupts_disable(); |
601 | ipl = interrupts_disable(); |
600 | spinlock_lock(&t->lock); |
602 | spinlock_lock(&t->lock); |
601 | ASSERT(!t->detached); |
603 | ASSERT(!t->detached); |
602 | spinlock_unlock(&t->lock); |
604 | spinlock_unlock(&t->lock); |
603 | interrupts_restore(ipl); |
605 | interrupts_restore(ipl); |
604 | 606 | ||
605 | rc = waitq_sleep_timeout(&t->join_wq, usec, flags); |
607 | rc = waitq_sleep_timeout(&t->join_wq, usec, flags); |
606 | 608 | ||
607 | return rc; |
609 | return rc; |
608 | } |
610 | } |
609 | 611 | ||
610 | /** Detach thread. |
612 | /** Detach thread. |
611 | * |
613 | * |
612 | * Mark the thread as detached, if the thread is already in the Lingering |
614 | * Mark the thread as detached, if the thread is already in the Lingering |
613 | * state, deallocate its resources. |
615 | * state, deallocate its resources. |
614 | * |
616 | * |
615 | * @param t Thread to be detached. |
617 | * @param t Thread to be detached. |
616 | */ |
618 | */ |
617 | void thread_detach(thread_t *t) |
619 | void thread_detach(thread_t *t) |
618 | { |
620 | { |
619 | ipl_t ipl; |
621 | ipl_t ipl; |
620 | 622 | ||
621 | /* |
623 | /* |
622 | * Since the thread is expected not to be already detached, |
624 | * Since the thread is expected not to be already detached, |
623 | * pointer to it must be still valid. |
625 | * pointer to it must be still valid. |
624 | */ |
626 | */ |
625 | ipl = interrupts_disable(); |
627 | ipl = interrupts_disable(); |
626 | spinlock_lock(&t->lock); |
628 | spinlock_lock(&t->lock); |
627 | ASSERT(!t->detached); |
629 | ASSERT(!t->detached); |
628 | if (t->state == Lingering) { |
630 | if (t->state == Lingering) { |
629 | thread_destroy(t); /* unlocks &t->lock */ |
631 | thread_destroy(t); /* unlocks &t->lock */ |
630 | interrupts_restore(ipl); |
632 | interrupts_restore(ipl); |
631 | return; |
633 | return; |
632 | } else { |
634 | } else { |
633 | t->detached = true; |
635 | t->detached = true; |
634 | } |
636 | } |
635 | spinlock_unlock(&t->lock); |
637 | spinlock_unlock(&t->lock); |
636 | interrupts_restore(ipl); |
638 | interrupts_restore(ipl); |
637 | } |
639 | } |
638 | 640 | ||
639 | /** Thread usleep |
641 | /** Thread usleep |
640 | * |
642 | * |
641 | * Suspend execution of the current thread. |
643 | * Suspend execution of the current thread. |
642 | * |
644 | * |
643 | * @param usec Number of microseconds to sleep. |
645 | * @param usec Number of microseconds to sleep. |
644 | * |
646 | * |
645 | */ |
647 | */ |
646 | void thread_usleep(uint32_t usec) |
648 | void thread_usleep(uint32_t usec) |
647 | { |
649 | { |
648 | waitq_t wq; |
650 | waitq_t wq; |
649 | 651 | ||
650 | waitq_initialize(&wq); |
652 | waitq_initialize(&wq); |
651 | 653 | ||
652 | (void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING); |
654 | (void) waitq_sleep_timeout(&wq, usec, SYNCH_FLAGS_NON_BLOCKING); |
653 | } |
655 | } |
654 | 656 | ||
655 | /** Register thread out-of-context invocation |
657 | /** Register thread out-of-context invocation |
656 | * |
658 | * |
657 | * Register a function and its argument to be executed |
659 | * Register a function and its argument to be executed |
658 | * on next context switch to the current thread. |
660 | * on next context switch to the current thread. |
659 | * |
661 | * |
660 | * @param call_me Out-of-context function. |
662 | * @param call_me Out-of-context function. |
661 | * @param call_me_with Out-of-context function argument. |
663 | * @param call_me_with Out-of-context function argument. |
662 | * |
664 | * |
663 | */ |
665 | */ |
664 | void thread_register_call_me(void (* call_me)(void *), void *call_me_with) |
666 | void thread_register_call_me(void (* call_me)(void *), void *call_me_with) |
665 | { |
667 | { |
666 | ipl_t ipl; |
668 | ipl_t ipl; |
667 | 669 | ||
668 | ipl = interrupts_disable(); |
670 | ipl = interrupts_disable(); |
669 | spinlock_lock(&THREAD->lock); |
671 | spinlock_lock(&THREAD->lock); |
670 | THREAD->call_me = call_me; |
672 | THREAD->call_me = call_me; |
671 | THREAD->call_me_with = call_me_with; |
673 | THREAD->call_me_with = call_me_with; |
672 | spinlock_unlock(&THREAD->lock); |
674 | spinlock_unlock(&THREAD->lock); |
673 | interrupts_restore(ipl); |
675 | interrupts_restore(ipl); |
674 | } |
676 | } |
675 | 677 | ||
676 | static bool thread_walker(avltree_node_t *node, void *arg) |
678 | static bool thread_walker(avltree_node_t *node, void *arg) |
677 | { |
679 | { |
678 | thread_t *t; |
680 | thread_t *t; |
679 | 681 | ||
680 | t = avltree_get_instance(node, thread_t, threads_tree_node); |
682 | t = avltree_get_instance(node, thread_t, threads_tree_node); |
681 | 683 | ||
682 | uint64_t cycles; |
684 | uint64_t cycles; |
683 | char suffix; |
685 | char suffix; |
684 | order(t->cycles, &cycles, &suffix); |
686 | order(t->cycles, &cycles, &suffix); |
685 | 687 | ||
686 | if (sizeof(void *) == 4) |
688 | if (sizeof(void *) == 4) |
687 | printf("%-6llu %-10s %#10zx %-8s %#10zx %-3ld %#10zx %#10zx %9llu%c ", |
689 | printf("%-6llu %-10s %#10zx %-8s %#10zx %-3ld %#10zx %#10zx %9llu%c ", |
688 | t->tid, t->name, t, thread_states[t->state], t->task, |
690 | t->tid, t->name, t, thread_states[t->state], t->task, |
689 | t->task->context, t->thread_code, t->kstack, cycles, suffix); |
691 | t->task->context, t->thread_code, t->kstack, cycles, suffix); |
690 | else |
692 | else |
691 | printf("%-6llu %-10s %#18zx %-8s %#18zx %-3ld %#18zx %#18zx %9llu%c ", |
693 | printf("%-6llu %-10s %#18zx %-8s %#18zx %-3ld %#18zx %#18zx %9llu%c ", |
692 | t->tid, t->name, t, thread_states[t->state], t->task, |
694 | t->tid, t->name, t, thread_states[t->state], t->task, |
693 | t->task->context, t->thread_code, t->kstack, cycles, suffix); |
695 | t->task->context, t->thread_code, t->kstack, cycles, suffix); |
694 | 696 | ||
695 | if (t->cpu) |
697 | if (t->cpu) |
696 | printf("%-4zd", t->cpu->id); |
698 | printf("%-4zd", t->cpu->id); |
697 | else |
699 | else |
698 | printf("none"); |
700 | printf("none"); |
699 | 701 | ||
700 | if (t->state == Sleeping) { |
702 | if (t->state == Sleeping) { |
701 | if (sizeof(uintptr_t) == 4) |
703 | if (sizeof(uintptr_t) == 4) |
702 | printf(" %#10zx", t->sleep_queue); |
704 | printf(" %#10zx", t->sleep_queue); |
703 | else |
705 | else |
704 | printf(" %#18zx", t->sleep_queue); |
706 | printf(" %#18zx", t->sleep_queue); |
705 | } |
707 | } |
706 | 708 | ||
707 | printf("\n"); |
709 | printf("\n"); |
708 | 710 | ||
709 | return true; |
711 | return true; |
710 | } |
712 | } |
711 | 713 | ||
712 | /** Print list of threads debug info */ |
714 | /** Print list of threads debug info */ |
713 | void thread_print_list(void) |
715 | void thread_print_list(void) |
714 | { |
716 | { |
715 | ipl_t ipl; |
717 | ipl_t ipl; |
716 | 718 | ||
717 | /* Messing with thread structures, avoid deadlock */ |
719 | /* Messing with thread structures, avoid deadlock */ |
718 | ipl = interrupts_disable(); |
720 | ipl = interrupts_disable(); |
719 | spinlock_lock(&threads_lock); |
721 | spinlock_lock(&threads_lock); |
720 | 722 | ||
721 | if (sizeof(uintptr_t) == 4) { |
723 | if (sizeof(uintptr_t) == 4) { |
722 | printf("tid name address state task " |
724 | printf("tid name address state task " |
723 | "ctx code stack cycles cpu " |
725 | "ctx code stack cycles cpu " |
724 | "waitqueue\n"); |
726 | "waitqueue\n"); |
725 | printf("------ ---------- ---------- -------- ---------- " |
727 | printf("------ ---------- ---------- -------- ---------- " |
726 | "--- ---------- ---------- ---------- ---- " |
728 | "--- ---------- ---------- ---------- ---- " |
727 | "----------\n"); |
729 | "----------\n"); |
728 | } else { |
730 | } else { |
729 | printf("tid name address state task " |
731 | printf("tid name address state task " |
730 | "ctx code stack cycles cpu " |
732 | "ctx code stack cycles cpu " |
731 | "waitqueue\n"); |
733 | "waitqueue\n"); |
732 | printf("------ ---------- ------------------ -------- ------------------ " |
734 | printf("------ ---------- ------------------ -------- ------------------ " |
733 | "--- ------------------ ------------------ ---------- ---- " |
735 | "--- ------------------ ------------------ ---------- ---- " |
734 | "------------------\n"); |
736 | "------------------\n"); |
735 | } |
737 | } |
736 | 738 | ||
737 | avltree_walk(&threads_tree, thread_walker, NULL); |
739 | avltree_walk(&threads_tree, thread_walker, NULL); |
738 | 740 | ||
739 | spinlock_unlock(&threads_lock); |
741 | spinlock_unlock(&threads_lock); |
740 | interrupts_restore(ipl); |
742 | interrupts_restore(ipl); |
741 | } |
743 | } |
742 | 744 | ||
743 | /** Check whether thread exists. |
745 | /** Check whether thread exists. |
744 | * |
746 | * |
745 | * Note that threads_lock must be already held and |
747 | * Note that threads_lock must be already held and |
746 | * interrupts must be already disabled. |
748 | * interrupts must be already disabled. |
747 | * |
749 | * |
748 | * @param t Pointer to thread. |
750 | * @param t Pointer to thread. |
749 | * |
751 | * |
750 | * @return True if thread t is known to the system, false otherwise. |
752 | * @return True if thread t is known to the system, false otherwise. |
751 | */ |
753 | */ |
752 | bool thread_exists(thread_t *t) |
754 | bool thread_exists(thread_t *t) |
753 | { |
755 | { |
754 | avltree_node_t *node; |
756 | avltree_node_t *node; |
755 | 757 | ||
756 | node = avltree_search(&threads_tree, (avltree_key_t) ((uintptr_t) t)); |
758 | node = avltree_search(&threads_tree, (avltree_key_t) ((uintptr_t) t)); |
757 | 759 | ||
758 | return node != NULL; |
760 | return node != NULL; |
759 | } |
761 | } |
760 | 762 | ||
761 | 763 | ||
762 | /** Update accounting of current thread. |
764 | /** Update accounting of current thread. |
763 | * |
765 | * |
764 | * Note that thread_lock on THREAD must be already held and |
766 | * Note that thread_lock on THREAD must be already held and |
765 | * interrupts must be already disabled. |
767 | * interrupts must be already disabled. |
766 | * |
768 | * |
767 | */ |
769 | */ |
768 | void thread_update_accounting(void) |
770 | void thread_update_accounting(void) |
769 | { |
771 | { |
770 | uint64_t time = get_cycle(); |
772 | uint64_t time = get_cycle(); |
771 | THREAD->cycles += time - THREAD->last_cycle; |
773 | THREAD->cycles += time - THREAD->last_cycle; |
772 | THREAD->last_cycle = time; |
774 | THREAD->last_cycle = time; |
773 | } |
775 | } |
774 | 776 | ||
775 | /** Process syscall to create new thread. |
777 | /** Process syscall to create new thread. |
776 | * |
778 | * |
777 | */ |
779 | */ |
778 | unative_t sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name, |
780 | unative_t sys_thread_create(uspace_arg_t *uspace_uarg, char *uspace_name, |
779 | thread_id_t *uspace_thread_id) |
781 | thread_id_t *uspace_thread_id) |
780 | { |
782 | { |
781 | thread_t *t; |
783 | thread_t *t; |
782 | char namebuf[THREAD_NAME_BUFLEN]; |
784 | char namebuf[THREAD_NAME_BUFLEN]; |
783 | uspace_arg_t *kernel_uarg; |
785 | uspace_arg_t *kernel_uarg; |
784 | int rc; |
786 | int rc; |
785 | 787 | ||
786 | rc = copy_from_uspace(namebuf, uspace_name, THREAD_NAME_BUFLEN); |
788 | rc = copy_from_uspace(namebuf, uspace_name, THREAD_NAME_BUFLEN); |
787 | if (rc != 0) |
789 | if (rc != 0) |
788 | return (unative_t) rc; |
790 | return (unative_t) rc; |
789 | 791 | ||
790 | /* |
792 | /* |
791 | * In case of failure, kernel_uarg will be deallocated in this function. |
793 | * In case of failure, kernel_uarg will be deallocated in this function. |
792 | * In case of success, kernel_uarg will be freed in uinit(). |
794 | * In case of success, kernel_uarg will be freed in uinit(). |
793 | */ |
795 | */ |
794 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
796 | kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0); |
795 | 797 | ||
796 | rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t)); |
798 | rc = copy_from_uspace(kernel_uarg, uspace_uarg, sizeof(uspace_arg_t)); |
797 | if (rc != 0) { |
799 | if (rc != 0) { |
798 | free(kernel_uarg); |
800 | free(kernel_uarg); |
799 | return (unative_t) rc; |
801 | return (unative_t) rc; |
800 | } |
802 | } |
801 | 803 | ||
802 | t = thread_create(uinit, kernel_uarg, TASK, |
804 | t = thread_create(uinit, kernel_uarg, TASK, |
803 | THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf, false); |
805 | THREAD_FLAG_USPACE | THREAD_FLAG_NOATTACH, namebuf, false); |
804 | if (t) { |
806 | if (t) { |
805 | if (uspace_thread_id != NULL) { |
807 | if (uspace_thread_id != NULL) { |
806 | int rc; |
808 | int rc; |
807 | 809 | ||
808 | rc = copy_to_uspace(uspace_thread_id, &t->tid, |
810 | rc = copy_to_uspace(uspace_thread_id, &t->tid, |
809 | sizeof(t->tid)); |
811 | sizeof(t->tid)); |
810 | if (rc != 0) { |
812 | if (rc != 0) { |
811 | /* |
813 | /* |
812 | * We have encountered a failure, but the thread |
814 | * We have encountered a failure, but the thread |
813 | * has already been created. We need to undo its |
815 | * has already been created. We need to undo its |
814 | * creation now. |
816 | * creation now. |
815 | */ |
817 | */ |
816 | 818 | ||
817 | /* |
819 | /* |
818 | * The new thread structure is initialized, but |
820 | * The new thread structure is initialized, but |
819 | * is still not visible to the system. |
821 | * is still not visible to the system. |
820 | * We can safely deallocate it. |
822 | * We can safely deallocate it. |
821 | */ |
823 | */ |
822 | slab_free(thread_slab, t); |
824 | slab_free(thread_slab, t); |
823 | free(kernel_uarg); |
825 | free(kernel_uarg); |
824 | 826 | ||
825 | return (unative_t) rc; |
827 | return (unative_t) rc; |
826 | } |
828 | } |
827 | } |
829 | } |
828 | thread_attach(t, TASK); |
830 | thread_attach(t, TASK); |
829 | thread_ready(t); |
831 | thread_ready(t); |
830 | 832 | ||
831 | return 0; |
833 | return 0; |
832 | } else |
834 | } else |
833 | free(kernel_uarg); |
835 | free(kernel_uarg); |
834 | 836 | ||
835 | return (unative_t) ENOMEM; |
837 | return (unative_t) ENOMEM; |
836 | } |
838 | } |
837 | 839 | ||
838 | /** Process syscall to terminate thread. |
840 | /** Process syscall to terminate thread. |
839 | * |
841 | * |
840 | */ |
842 | */ |
841 | unative_t sys_thread_exit(int uspace_status) |
843 | unative_t sys_thread_exit(int uspace_status) |
842 | { |
844 | { |
843 | thread_exit(); |
845 | thread_exit(); |
844 | /* Unreachable */ |
846 | /* Unreachable */ |
845 | return 0; |
847 | return 0; |
846 | } |
848 | } |
847 | 849 | ||
848 | /** Syscall for getting TID. |
850 | /** Syscall for getting TID. |
849 | * |
851 | * |
850 | * @param uspace_thread_id Userspace address of 8-byte buffer where to store |
852 | * @param uspace_thread_id Userspace address of 8-byte buffer where to store |
851 | * current thread ID. |
853 | * current thread ID. |
852 | * |
854 | * |
853 | * @return 0 on success or an error code from @ref errno.h. |
855 | * @return 0 on success or an error code from @ref errno.h. |
854 | */ |
856 | */ |
855 | unative_t sys_thread_get_id(thread_id_t *uspace_thread_id) |
857 | unative_t sys_thread_get_id(thread_id_t *uspace_thread_id) |
856 | { |
858 | { |
857 | /* |
859 | /* |
858 | * No need to acquire lock on THREAD because tid |
860 | * No need to acquire lock on THREAD because tid |
859 | * remains constant for the lifespan of the thread. |
861 | * remains constant for the lifespan of the thread. |
860 | */ |
862 | */ |
861 | return (unative_t) copy_to_uspace(uspace_thread_id, &THREAD->tid, |
863 | return (unative_t) copy_to_uspace(uspace_thread_id, &THREAD->tid, |
862 | sizeof(THREAD->tid)); |
864 | sizeof(THREAD->tid)); |
863 | } |
865 | } |
864 | 866 | ||
865 | /** @} |
867 | /** @} |
866 | */ |
868 | */ |
867 | 869 |