Subversion Repositories HelenOS-historic

Rev

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

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