Subversion Repositories HelenOS-historic

Rev

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

Rev 1588 Rev 1589
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    task.c
30
 * @file    task.c
31
 * @brief   Task management.
31
 * @brief   Task management.
32
 */
32
 */
33
 
33
 
34
#include <main/uinit.h>
34
#include <main/uinit.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/as.h>
38
#include <mm/as.h>
39
#include <mm/slab.h>
39
#include <mm/slab.h>
40
#include <synch/spinlock.h>
40
#include <synch/spinlock.h>
41
#include <arch.h>
41
#include <arch.h>
42
#include <panic.h>
42
#include <panic.h>
43
#include <adt/btree.h>
43
#include <adt/btree.h>
44
#include <adt/list.h>
44
#include <adt/list.h>
45
#include <ipc/ipc.h>
45
#include <ipc/ipc.h>
46
#include <security/cap.h>
46
#include <security/cap.h>
47
#include <memstr.h>
47
#include <memstr.h>
48
#include <print.h>
48
#include <print.h>
49
#include <elf.h>
49
#include <elf.h>
50
#include <errno.h>
50
#include <errno.h>
51
#include <syscall/copy.h>
51
#include <syscall/copy.h>
52
 
52
 
53
#ifndef LOADED_PROG_STACK_PAGES_NO
53
#ifndef LOADED_PROG_STACK_PAGES_NO
54
#define LOADED_PROG_STACK_PAGES_NO 1
54
#define LOADED_PROG_STACK_PAGES_NO 1
55
#endif
55
#endif
56
 
56
 
57
SPINLOCK_INITIALIZE(tasks_lock);
57
SPINLOCK_INITIALIZE(tasks_lock);
58
btree_t tasks_btree;
58
btree_t tasks_btree;
59
static task_id_t task_counter = 0;
59
static task_id_t task_counter = 0;
60
 
60
 
61
static void ktaskclnp(void *arg);
61
static void ktaskclnp(void *arg);
62
static void ktaskkill(void *arg);
62
static void ktaskkill(void *arg);
63
 
63
 
64
/** Initialize tasks
64
/** Initialize tasks
65
 *
65
 *
66
 * Initialize kernel tasks support.
66
 * Initialize kernel tasks support.
67
 *
67
 *
68
 */
68
 */
69
void task_init(void)
69
void task_init(void)
70
{
70
{
71
    TASK = NULL;
71
    TASK = NULL;
72
    btree_create(&tasks_btree);
72
    btree_create(&tasks_btree);
73
}
73
}
74
 
74
 
75
 
75
 
76
/** Create new task
76
/** Create new task
77
 *
77
 *
78
 * Create new task with no threads.
78
 * Create new task with no threads.
79
 *
79
 *
80
 * @param as Task's address space.
80
 * @param as Task's address space.
81
 * @param name Symbolic name.
81
 * @param name Symbolic name.
82
 *
82
 *
83
 * @return New task's structure
83
 * @return New task's structure
84
 *
84
 *
85
 */
85
 */
86
task_t *task_create(as_t *as, char *name)
86
task_t *task_create(as_t *as, char *name)
87
{
87
{
88
    ipl_t ipl;
88
    ipl_t ipl;
89
    task_t *ta;
89
    task_t *ta;
90
    int i;
90
    int i;
91
   
91
   
92
    ta = (task_t *) malloc(sizeof(task_t), 0);
92
    ta = (task_t *) malloc(sizeof(task_t), 0);
93
 
93
 
94
    task_create_arch(ta);
94
    task_create_arch(ta);
95
 
95
 
96
    spinlock_initialize(&ta->lock, "task_ta_lock");
96
    spinlock_initialize(&ta->lock, "task_ta_lock");
97
    list_initialize(&ta->th_head);
97
    list_initialize(&ta->th_head);
98
    ta->as = as;
98
    ta->as = as;
99
    ta->name = name;
99
    ta->name = name;
100
    ta->main_thread = NULL;
100
    ta->main_thread = NULL;
101
    ta->refcount = 0;
101
    ta->refcount = 0;
102
 
102
 
103
    ta->capabilities = 0;
103
    ta->capabilities = 0;
104
    ta->accept_new_threads = true;
104
    ta->accept_new_threads = true;
105
   
105
   
106
    ipc_answerbox_init(&ta->answerbox);
106
    ipc_answerbox_init(&ta->answerbox);
107
    for (i=0; i < IPC_MAX_PHONES;i++)
107
    for (i=0; i < IPC_MAX_PHONES;i++)
108
        ipc_phone_init(&ta->phones[i]);
108
        ipc_phone_init(&ta->phones[i]);
109
    if (ipc_phone_0)
109
    if (ipc_phone_0)
110
        ipc_phone_connect(&ta->phones[0], ipc_phone_0);
110
        ipc_phone_connect(&ta->phones[0], ipc_phone_0);
111
    atomic_set(&ta->active_calls, 0);
111
    atomic_set(&ta->active_calls, 0);
112
 
112
 
113
    mutex_initialize(&ta->futexes_lock);
113
    mutex_initialize(&ta->futexes_lock);
114
    btree_create(&ta->futexes);
114
    btree_create(&ta->futexes);
115
   
115
   
116
    ipl = interrupts_disable();
116
    ipl = interrupts_disable();
117
 
117
 
118
    /*
118
    /*
119
     * Increment address space reference count.
119
     * Increment address space reference count.
120
     * TODO: Reconsider the locking scheme.
120
     * TODO: Reconsider the locking scheme.
121
     */
121
     */
122
    mutex_lock(&as->lock);
122
    mutex_lock(&as->lock);
123
    as->refcount++;
123
    as->refcount++;
124
    mutex_unlock(&as->lock);
124
    mutex_unlock(&as->lock);
125
 
125
 
126
    spinlock_lock(&tasks_lock);
126
    spinlock_lock(&tasks_lock);
127
 
127
 
128
    ta->taskid = ++task_counter;
128
    ta->taskid = ++task_counter;
129
    btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
129
    btree_insert(&tasks_btree, (btree_key_t) ta->taskid, (void *) ta, NULL);
130
 
130
 
131
    spinlock_unlock(&tasks_lock);
131
    spinlock_unlock(&tasks_lock);
132
    interrupts_restore(ipl);
132
    interrupts_restore(ipl);
133
 
133
 
134
    return ta;
134
    return ta;
135
}
135
}
136
 
136
 
137
/** Destroy task.
137
/** Destroy task.
138
 *
138
 *
139
 * @param t Task to be destroyed.
139
 * @param t Task to be destroyed.
140
 */
140
 */
141
void task_destroy(task_t *t)
141
void task_destroy(task_t *t)
142
{
142
{
143
    task_destroy_arch(t);
143
    task_destroy_arch(t);
144
    btree_destroy(&t->futexes);
144
    btree_destroy(&t->futexes);
145
 
145
 
146
    mutex_lock_active(&t->as->lock);
146
    mutex_lock_active(&t->as->lock);
147
    if (--t->as->refcount == 0) {
147
    if (--t->as->refcount == 0) {
148
        mutex_unlock(&t->as->lock);
148
        mutex_unlock(&t->as->lock);
149
        as_destroy(t->as);
149
        as_destroy(t->as);
150
        /*
150
        /*
151
         * t->as is destroyed.
151
         * t->as is destroyed.
152
         */
152
         */
153
    } else {
153
    } else {
154
        mutex_unlock(&t->as->lock);
154
        mutex_unlock(&t->as->lock);
155
    }
155
    }
156
   
156
   
157
    free(t);
157
    free(t);
158
    TASK = NULL;
158
    TASK = NULL;
159
}
159
}
160
 
160
 
161
/** Create new task with 1 thread and run it
161
/** Create new task with 1 thread and run it
162
 *
162
 *
163
 * @param program_addr Address of program executable image.
163
 * @param program_addr Address of program executable image.
164
 * @param name Program name.
164
 * @param name Program name.
165
 *
165
 *
166
 * @return Task of the running program or NULL on error.
166
 * @return Task of the running program or NULL on error.
167
 */
167
 */
168
task_t * task_run_program(void *program_addr, char *name)
168
task_t * task_run_program(void *program_addr, char *name)
169
{
169
{
170
    as_t *as;
170
    as_t *as;
171
    as_area_t *a;
171
    as_area_t *a;
172
    int rc;
172
    int rc;
173
    thread_t *t1, *t2;
173
    thread_t *t1, *t2;
174
    task_t *task;
174
    task_t *task;
175
    uspace_arg_t *kernel_uarg;
175
    uspace_arg_t *kernel_uarg;
176
 
176
 
177
    as = as_create(0);
177
    as = as_create(0);
178
    ASSERT(as);
178
    ASSERT(as);
179
 
179
 
180
    rc = elf_load((elf_header_t *) program_addr, as);
180
    rc = elf_load((elf_header_t *) program_addr, as);
181
    if (rc != EE_OK) {
181
    if (rc != EE_OK) {
182
        as_destroy(as);
182
        as_destroy(as);
183
        return NULL;
183
        return NULL;
184
    }
184
    }
185
   
185
   
186
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
186
    kernel_uarg = (uspace_arg_t *) malloc(sizeof(uspace_arg_t), 0);
187
    kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
187
    kernel_uarg->uspace_entry = (void *) ((elf_header_t *) program_addr)->e_entry;
188
    kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
188
    kernel_uarg->uspace_stack = (void *) USTACK_ADDRESS;
189
    kernel_uarg->uspace_thread_function = NULL;
189
    kernel_uarg->uspace_thread_function = NULL;
190
    kernel_uarg->uspace_thread_arg = NULL;
190
    kernel_uarg->uspace_thread_arg = NULL;
191
    kernel_uarg->uspace_uarg = NULL;
191
    kernel_uarg->uspace_uarg = NULL;
192
   
192
   
193
    task = task_create(as, name);
193
    task = task_create(as, name);
194
    ASSERT(task);
194
    ASSERT(task);
195
 
195
 
196
    /*
196
    /*
197
     * Create the data as_area.
197
     * Create the data as_area.
198
     */
198
     */
199
    a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
199
    a = as_area_create(as, AS_AREA_READ | AS_AREA_WRITE | AS_AREA_CACHEABLE,
200
        LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE,
200
        LOADED_PROG_STACK_PAGES_NO*PAGE_SIZE,
201
        USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL);
201
        USTACK_ADDRESS, AS_AREA_ATTR_NONE, &anon_backend, NULL);
202
 
202
 
203
    /*
203
    /*
204
     * Create the main thread.
204
     * Create the main thread.
205
     */
205
     */
206
    t1 = thread_create(uinit, kernel_uarg, task, 0, "uinit");
206
    t1 = thread_create(uinit, kernel_uarg, task, 0, "uinit");
207
    ASSERT(t1);
207
    ASSERT(t1);
208
   
208
   
209
    /*
209
    /*
210
     * Create killer thread for the new task.
210
     * Create killer thread for the new task.
211
     */
211
     */
212
    t2 = thread_create(ktaskkill, t1, task, 0, "ktaskkill");
212
    t2 = thread_create(ktaskkill, t1, task, 0, "ktaskkill");
213
    ASSERT(t2);
213
    ASSERT(t2);
214
    thread_ready(t2);
214
    thread_ready(t2);
215
 
215
 
216
    thread_ready(t1);
216
    thread_ready(t1);
217
 
217
 
218
    return task;
218
    return task;
219
}
219
}
220
 
220
 
221
/** Syscall for reading task ID from userspace.
221
/** Syscall for reading task ID from userspace.
222
 *
222
 *
223
 * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID.
223
 * @param uspace_task_id Userspace address of 8-byte buffer where to store current task ID.
224
 *
224
 *
225
 * @return 0 on success or an error code from @ref errno.h.
225
 * @return 0 on success or an error code from @ref errno.h.
226
 */
226
 */
227
__native sys_task_get_id(task_id_t *uspace_task_id)
227
__native sys_task_get_id(task_id_t *uspace_task_id)
228
{
228
{
229
    /*
229
    /*
230
     * No need to acquire lock on TASK because taskid
230
     * No need to acquire lock on TASK because taskid
231
     * remains constant for the lifespan of the task.
231
     * remains constant for the lifespan of the task.
232
     */
232
     */
233
    return (__native) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid));
233
    return (__native) copy_to_uspace(uspace_task_id, &TASK->taskid, sizeof(TASK->taskid));
234
}
234
}
235
 
235
 
236
/** Find task structure corresponding to task ID.
236
/** Find task structure corresponding to task ID.
237
 *
237
 *
238
 * The tasks_lock must be already held by the caller of this function
238
 * The tasks_lock must be already held by the caller of this function
239
 * and interrupts must be disabled.
239
 * and interrupts must be disabled.
240
 *
240
 *
-
 
241
 * The task is guaranteed to exist after it was found in the tasks_btree as long as:
-
 
242
 * @li the tasks_lock is held,
-
 
243
 * @li the task's lock is held when task's lock is acquired before releasing tasks_lock or
-
 
244
 * @li the task's refcount is grater than 0
-
 
245
 *
241
 * @param id Task ID.
246
 * @param id Task ID.
242
 *
247
 *
243
 * @return Task structure address or NULL if there is no such task ID.
248
 * @return Task structure address or NULL if there is no such task ID.
244
 */
249
 */
245
task_t *task_find_by_id(task_id_t id)
250
task_t *task_find_by_id(task_id_t id)
246
{
251
{
247
    btree_node_t *leaf;
252
    btree_node_t *leaf;
248
   
253
   
249
    return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf);
254
    return (task_t *) btree_search(&tasks_btree, (btree_key_t) id, &leaf);
250
}
255
}
251
 
256
 
252
/** Kill task.
257
/** Kill task.
253
 *
258
 *
254
 * @param id ID of the task to be killed.
259
 * @param id ID of the task to be killed.
255
 *
260
 *
256
 * @return 0 on success or an error code from errno.h
261
 * @return 0 on success or an error code from errno.h
257
 */
262
 */
258
int task_kill(task_id_t id)
263
int task_kill(task_id_t id)
259
{
264
{
260
    ipl_t ipl;
265
    ipl_t ipl;
261
    task_t *ta;
266
    task_t *ta;
262
    thread_t *t;
267
    thread_t *t;
263
    link_t *cur;
268
    link_t *cur;
264
   
269
   
265
    ipl = interrupts_disable();
270
    ipl = interrupts_disable();
266
    spinlock_lock(&tasks_lock);
271
    spinlock_lock(&tasks_lock);
267
 
272
 
268
    if (!(ta = task_find_by_id(id))) {
273
    if (!(ta = task_find_by_id(id))) {
269
        spinlock_unlock(&tasks_lock);
274
        spinlock_unlock(&tasks_lock);
270
        interrupts_restore(ipl);
275
        interrupts_restore(ipl);
271
        return ENOENT;
276
        return ENOENT;
272
    }
277
    }
273
 
278
 
274
    spinlock_lock(&ta->lock);
279
    spinlock_lock(&ta->lock);
275
    ta->refcount++;
280
    ta->refcount++;
276
    spinlock_unlock(&ta->lock);
281
    spinlock_unlock(&ta->lock);
277
 
282
 
278
    btree_remove(&tasks_btree, ta->taskid, NULL);
283
    btree_remove(&tasks_btree, ta->taskid, NULL);
279
    spinlock_unlock(&tasks_lock);
284
    spinlock_unlock(&tasks_lock);
280
   
285
   
281
    t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp");
286
    t = thread_create(ktaskclnp, NULL, ta, 0, "ktaskclnp");
282
   
287
   
283
    spinlock_lock(&ta->lock);
288
    spinlock_lock(&ta->lock);
284
    ta->accept_new_threads = false;
289
    ta->accept_new_threads = false;
285
    ta->refcount--;
290
    ta->refcount--;
286
 
291
 
287
    /*
292
    /*
288
     * Interrupt all threads except this one.
293
     * Interrupt all threads except this one.
289
     */
294
     */
290
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
295
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
291
        thread_t *thr;
296
        thread_t *thr;
292
        bool  sleeping = false;
297
        bool  sleeping = false;
293
       
298
       
294
        thr = list_get_instance(cur, thread_t, th_link);
299
        thr = list_get_instance(cur, thread_t, th_link);
295
        if (thr == t)
300
        if (thr == t)
296
            continue;
301
            continue;
297
           
302
           
298
        spinlock_lock(&thr->lock);
303
        spinlock_lock(&thr->lock);
299
        thr->interrupted = true;
304
        thr->interrupted = true;
300
        if (thr->state == Sleeping)
305
        if (thr->state == Sleeping)
301
            sleeping = true;
306
            sleeping = true;
302
        spinlock_unlock(&thr->lock);
307
        spinlock_unlock(&thr->lock);
303
       
308
       
304
        if (sleeping)
309
        if (sleeping)
305
            waitq_interrupt_sleep(thr);
310
            waitq_interrupt_sleep(thr);
306
    }
311
    }
307
   
312
   
308
    spinlock_unlock(&ta->lock);
313
    spinlock_unlock(&ta->lock);
309
    interrupts_restore(ipl);
314
    interrupts_restore(ipl);
310
   
315
   
311
    if (t)
316
    if (t)
312
        thread_ready(t);
317
        thread_ready(t);
313
 
318
 
314
    return 0;
319
    return 0;
315
}
320
}
316
 
321
 
317
/** Print task list */
322
/** Print task list */
318
void task_print_list(void)
323
void task_print_list(void)
319
{
324
{
320
    link_t *cur;
325
    link_t *cur;
321
    ipl_t ipl;
326
    ipl_t ipl;
322
   
327
   
323
    /* Messing with thread structures, avoid deadlock */
328
    /* Messing with thread structures, avoid deadlock */
324
    ipl = interrupts_disable();
329
    ipl = interrupts_disable();
325
    spinlock_lock(&tasks_lock);
330
    spinlock_lock(&tasks_lock);
326
 
331
 
327
    for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
332
    for (cur = tasks_btree.leaf_head.next; cur != &tasks_btree.leaf_head; cur = cur->next) {
328
        btree_node_t *node;
333
        btree_node_t *node;
329
        int i;
334
        int i;
330
       
335
       
331
        node = list_get_instance(cur, btree_node_t, leaf_link);
336
        node = list_get_instance(cur, btree_node_t, leaf_link);
332
        for (i = 0; i < node->keys; i++) {
337
        for (i = 0; i < node->keys; i++) {
333
            task_t *t;
338
            task_t *t;
334
            int j;
339
            int j;
335
 
340
 
336
            t = (task_t *) node->value[i];
341
            t = (task_t *) node->value[i];
337
       
342
       
338
            spinlock_lock(&t->lock);
343
            spinlock_lock(&t->lock);
339
            printf("%s(%lld): address=%#zX, as=%#zX, ActiveCalls: %zd",
344
            printf("%s(%lld): address=%#zX, as=%#zX, ActiveCalls: %zd",
340
                t->name, t->taskid, t, t->as, atomic_get(&t->active_calls));
345
                t->name, t->taskid, t, t->as, atomic_get(&t->active_calls));
341
            for (j=0; j < IPC_MAX_PHONES; j++) {
346
            for (j=0; j < IPC_MAX_PHONES; j++) {
342
                if (t->phones[j].callee)
347
                if (t->phones[j].callee)
343
                    printf(" Ph(%zd): %#zX ", j, t->phones[j].callee);
348
                    printf(" Ph(%zd): %#zX ", j, t->phones[j].callee);
344
            }
349
            }
345
            printf("\n");
350
            printf("\n");
346
            spinlock_unlock(&t->lock);
351
            spinlock_unlock(&t->lock);
347
        }
352
        }
348
    }
353
    }
349
 
354
 
350
    spinlock_unlock(&tasks_lock);
355
    spinlock_unlock(&tasks_lock);
351
    interrupts_restore(ipl);
356
    interrupts_restore(ipl);
352
}
357
}
353
 
358
 
354
/** Kernel thread used to cleanup the task after it is killed. */
359
/** Kernel thread used to cleanup the task after it is killed. */
355
void ktaskclnp(void *arg)
360
void ktaskclnp(void *arg)
356
{
361
{
357
    ipl_t ipl;
362
    ipl_t ipl;
358
    thread_t *t = NULL, *main_thread;
363
    thread_t *t = NULL, *main_thread;
359
    link_t *cur;
364
    link_t *cur;
360
 
365
 
361
    thread_detach(THREAD);
366
    thread_detach(THREAD);
362
 
367
 
363
loop:
368
loop:
364
    ipl = interrupts_disable();
369
    ipl = interrupts_disable();
365
    spinlock_lock(&TASK->lock);
370
    spinlock_lock(&TASK->lock);
366
   
371
   
367
    main_thread = TASK->main_thread;
372
    main_thread = TASK->main_thread;
368
   
373
   
369
    /*
374
    /*
370
     * Find a thread to join.
375
     * Find a thread to join.
371
     */
376
     */
372
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
377
    for (cur = TASK->th_head.next; cur != &TASK->th_head; cur = cur->next) {
373
        t = list_get_instance(cur, thread_t, th_link);
378
        t = list_get_instance(cur, thread_t, th_link);
374
        if (t == THREAD)
379
        if (t == THREAD)
375
            continue;
380
            continue;
376
        else if (t == main_thread)
381
        else if (t == main_thread)
377
            continue;
382
            continue;
378
        else
383
        else
379
            break;
384
            break;
380
    }
385
    }
381
   
386
   
382
    spinlock_unlock(&TASK->lock);
387
    spinlock_unlock(&TASK->lock);
383
    interrupts_restore(ipl);
388
    interrupts_restore(ipl);
384
   
389
   
385
    if (t != THREAD) {
390
    if (t != THREAD) {
386
        ASSERT(t != main_thread);   /* uninit is joined and detached in ktaskkill */
391
        ASSERT(t != main_thread);   /* uninit is joined and detached in ktaskkill */
387
        thread_join(t);
392
        thread_join(t);
388
        thread_detach(t);
393
        thread_detach(t);
389
        goto loop;  /* go for another thread */
394
        goto loop;  /* go for another thread */
390
    }
395
    }
391
   
396
   
392
    /*
397
    /*
393
     * Now there are no other threads in this task
398
     * Now there are no other threads in this task
394
     * and no new threads can be created.
399
     * and no new threads can be created.
395
     */
400
     */
396
   
401
   
397
    ipc_cleanup();
402
    ipc_cleanup();
398
    futex_cleanup();
403
    futex_cleanup();
399
}
404
}
400
 
405
 
401
/** Kernel task used to kill a userspace task when its main thread exits.
406
/** Kernel task used to kill a userspace task when its main thread exits.
402
 *
407
 *
403
 * This thread waits until the main userspace thread (i.e. uninit) exits.
408
 * This thread waits until the main userspace thread (i.e. uninit) exits.
404
 * When this happens, the task is killed.
409
 * When this happens, the task is killed.
405
 *
410
 *
406
 * @param arg Pointer to the thread structure of the task's main thread.
411
 * @param arg Pointer to the thread structure of the task's main thread.
407
 */
412
 */
408
void ktaskkill(void *arg)
413
void ktaskkill(void *arg)
409
{
414
{
410
    thread_t *t = (thread_t *) arg;
415
    thread_t *t = (thread_t *) arg;
411
   
416
   
412
    /*
417
    /*
413
     * Userspace threads cannot detach themselves,
418
     * Userspace threads cannot detach themselves,
414
     * therefore the thread pointer is guaranteed to be valid.
419
     * therefore the thread pointer is guaranteed to be valid.
415
     */
420
     */
416
    thread_join(t); /* sleep uninterruptibly here! */
421
    thread_join(t); /* sleep uninterruptibly here! */
417
    thread_detach(t);
422
    thread_detach(t);
418
    task_kill(TASK->taskid);
423
    task_kill(TASK->taskid);
419
}
424
}
420
 
425