Subversion Repositories HelenOS

Rev

Rev 3343 | Rev 3593 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 3343 Rev 3397
Line 69... Line 69...
69
 */
69
 */
70
avltree_t tasks_tree;
70
avltree_t tasks_tree;
71
 
71
 
72
static task_id_t task_counter = 0;
72
static task_id_t task_counter = 0;
73
 
73
 
74
/** Initialize tasks
-
 
75
 *
-
 
76
 * Initialize kernel tasks support.
74
/** Initialize kernel tasks support. */
77
 *
-
 
78
 */
-
 
79
void task_init(void)
75
void task_init(void)
80
{
76
{
81
    TASK = NULL;
77
    TASK = NULL;
82
    avltree_create(&tasks_tree);
78
    avltree_create(&tasks_tree);
83
}
79
}
84
 
80
 
85
/*
81
/*
86
 * The idea behind this walker is to remember a single task different from TASK.
82
 * The idea behind this walker is to remember a single task different from
-
 
83
 * TASK.
87
 */
84
 */
88
static bool task_done_walker(avltree_node_t *node, void *arg)
85
static bool task_done_walker(avltree_node_t *node, void *arg)
89
{
86
{
90
    task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
87
    task_t *t = avltree_get_instance(node, task_t, tasks_tree_node);
91
    task_t **tp = (task_t **) arg;
88
    task_t **tp = (task_t **) arg;
Line 96... Line 93...
96
    }
93
    }
97
 
94
 
98
    return true;    /* continue the walk */
95
    return true;    /* continue the walk */
99
}
96
}
100
 
97
 
101
/** Kill all tasks except the current task.
98
/** Kill all tasks except the current task. */
102
 *
-
 
103
 */
-
 
104
void task_done(void)
99
void task_done(void)
105
{
100
{
106
    task_t *t;
101
    task_t *t;
107
    do { /* Repeat until there are any tasks except TASK */
102
    do { /* Repeat until there are any tasks except TASK */
108
       
103
       
Line 130... Line 125...
130
        }
125
        }
131
       
126
       
132
    } while (t != NULL);
127
    } while (t != NULL);
133
}
128
}
134
 
129
 
135
/** Create new task
-
 
136
 *
-
 
137
 * Create new task with no threads.
130
/** Create new task with no threads.
138
 *
131
 *
139
 * @param as Task's address space.
132
 * @param as        Task's address space.
140
 * @param name Symbolic name.
133
 * @param name      Symbolic name.
141
 *
134
 *
142
 * @return New task's structure
135
 * @return      New task's structure.
143
 *
136
 *
144
 */
137
 */
145
task_t *task_create(as_t *as, char *name)
138
task_t *task_create(as_t *as, char *name)
146
{
139
{
147
    ipl_t ipl;
140
    ipl_t ipl;
Line 192... Line 185...
192
    return ta;
185
    return ta;
193
}
186
}
194
 
187
 
195
/** Destroy task.
188
/** Destroy task.
196
 *
189
 *
197
 * @param t Task to be destroyed.
190
 * @param t     Task to be destroyed.
198
 */
191
 */
199
void task_destroy(task_t *t)
192
void task_destroy(task_t *t)
200
{
193
{
201
    /*
194
    /*
202
     * Remove the task from the task B+tree.
195
     * Remove the task from the task B+tree.
Line 225... Line 218...
225
    TASK = NULL;
218
    TASK = NULL;
226
}
219
}
227
 
220
 
228
/** Syscall for reading task ID from userspace.
221
/** Syscall for reading task ID from userspace.
229
 *
222
 *
230
 * @param uspace_task_id Userspace address of 8-byte buffer where to store
223
 * @param       uspace_task_id userspace address of 8-byte buffer
231
 * current task ID.
224
 *          where to store current task ID.
232
 *
225
 *
233
 * @return 0 on success or an error code from @ref errno.h.
226
 * @return      Zero on success or an error code from @ref errno.h.
234
 */
227
 */
235
unative_t sys_task_get_id(task_id_t *uspace_task_id)
228
unative_t sys_task_get_id(task_id_t *uspace_task_id)
236
{
229
{
237
    /*
230
    /*
238
     * No need to acquire lock on TASK because taskid
231
     * No need to acquire lock on TASK because taskid remains constant for
239
     * remains constant for the lifespan of the task.
232
     * the lifespan of the task.
240
     */
233
     */
241
    return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
234
    return (unative_t) copy_to_uspace(uspace_task_id, &TASK->taskid,
242
        sizeof(TASK->taskid));
235
        sizeof(TASK->taskid));
243
}
236
}
244
 
237
 
245
/** Find task structure corresponding to task ID.
238
/** Find task structure corresponding to task ID.
246
 *
239
 *
247
 * The tasks_lock must be already held by the caller of this function
240
 * The tasks_lock must be already held by the caller of this function and
248
 * and interrupts must be disabled.
241
 * interrupts must be disabled.
249
 *
242
 *
250
 * @param id Task ID.
243
 * @param id        Task ID.
251
 *
244
 *
252
 * @return Task structure address or NULL if there is no such task ID.
245
 * @return      Task structure address or NULL if there is no such task
-
 
246
 *          ID.
253
 */
247
 */
254
task_t *task_find_by_id(task_id_t id)
248
task_t *task_find_by_id(task_id_t id) { avltree_node_t *node;
255
{
-
 
256
    avltree_node_t *node;
-
 
257
   
249
   
258
    node = avltree_search(&tasks_tree, (avltree_key_t) id);
250
    node = avltree_search(&tasks_tree, (avltree_key_t) id);
259
 
251
 
260
    if (node)
252
    if (node)
261
        return avltree_get_instance(node, task_t, tasks_tree_node);
253
        return avltree_get_instance(node, task_t, tasks_tree_node);
262
    return NULL;
254
    return NULL;
263
}
255
}
264
 
256
 
265
/** Get accounting data of given task.
257
/** Get accounting data of given task.
266
 *
258
 *
267
 * Note that task lock of 't' must be already held and
259
 * Note that task lock of 't' must be already held and interrupts must be
268
 * interrupts must be already disabled.
260
 * already disabled.
269
 *
261
 *
270
 * @param t Pointer to thread.
262
 * @param t     Pointer to thread.
271
 *
263
 *
-
 
264
 * @return      Number of cycles used by the task and all its threads
-
 
265
 *          so far.
272
 */
266
 */
273
uint64_t task_get_accounting(task_t *t)
267
uint64_t task_get_accounting(task_t *t)
274
{
268
{
275
    /* Accumulated value of task */
269
    /* Accumulated value of task */
276
    uint64_t ret = t->cycles;
270
    uint64_t ret = t->cycles;
Line 298... Line 292...
298
/** Kill task.
292
/** Kill task.
299
 *
293
 *
300
 * This function is idempotent.
294
 * This function is idempotent.
301
 * It signals all the task's threads to bail it out.
295
 * It signals all the task's threads to bail it out.
302
 *
296
 *
303
 * @param id ID of the task to be killed.
297
 * @param id        ID of the task to be killed.
304
 *
298
 *
305
 * @return 0 on success or an error code from errno.h
299
 * @return      Zero on success or an error code from errno.h.
306
 */
300
 */
307
int task_kill(task_id_t id)
301
int task_kill(task_id_t id)
308
{
302
{
309
    ipl_t ipl;
303
    ipl_t ipl;
310
    task_t *ta;
304
    task_t *ta;
Line 321... Line 315...
321
        return ENOENT;
315
        return ENOENT;
322
    }
316
    }
323
    spinlock_unlock(&tasks_lock);
317
    spinlock_unlock(&tasks_lock);
324
   
318
   
325
    /*
319
    /*
326
     * Interrupt all threads except ktaskclnp.
320
     * Interrupt all threads.
327
     */
321
     */
328
    spinlock_lock(&ta->lock);
322
    spinlock_lock(&ta->lock);
329
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
323
    for (cur = ta->th_head.next; cur != &ta->th_head; cur = cur->next) {
330
        thread_t *thr;
324
        thread_t *thr;
331
        bool sleeping = false;
325
        bool sleeping = false;