Subversion Repositories HelenOS

Rev

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

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