Subversion Repositories HelenOS-historic

Rev

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

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