Subversion Repositories HelenOS-historic

Rev

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

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