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