source: mainline/generic/src/proc/thread.c@ 0f250f9

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0f250f9 was 0f250f9, checked in by Jakub Jermar <jakub@…>, 19 years ago

Improved uspace threads.
ia64 needs fixing.

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