Subversion Repositories HelenOS

Rev

Rev 1248 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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