summaryrefslogtreecommitdiff
path: root/kern/task.c
blob: 9a34f7c3e88c8d75abd2783fc27ce59e822bd7ec (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
/*
 * Copyright (c) 2012 Richard Braun.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <kern/error.h>
#include <kern/init.h>
#include <kern/kmem.h>
#include <kern/list.h>
#include <kern/spinlock.h>
#include <kern/stddef.h>
#include <kern/string.h>
#include <kern/task.h>
#include <kern/thread.h>
#include <vm/vm_kmem.h>

/*
 * Kernel task and storage.
 */
static struct task kernel_task_store;
struct task *kernel_task = &kernel_task_store;

/*
 * Cache for allocated tasks.
 */
static struct kmem_cache task_cache;

/*
 * Global list of tasks.
 */
static struct list task_list;
static struct spinlock task_list_lock;

static void
task_init(struct task *task, const char *name, struct vm_map *map)
{
    spinlock_init(&task->lock);
    list_init(&task->threads);
    task->map = map;
    strlcpy(task->name, name, sizeof(task->name));
}

void __init
task_setup(void)
{
    kmem_cache_init(&task_cache, "task", sizeof(struct task),
                    0, NULL, NULL, NULL, 0);
    list_init(&task_list);
    spinlock_init(&task_list_lock);
    task_init(kernel_task, "x15", kernel_map);
    list_insert(&task_list, &kernel_task->node);
}

int
task_create(struct task **taskp, const char *name)
{
    struct task *task;

    task = kmem_cache_alloc(&task_cache);

    if (task == NULL)
        return ERROR_NOMEM;

    /* TODO VM map */
    task_init(task, name, NULL);

    spinlock_lock(&task_list_lock);
    list_insert(&task_list, &kernel_task->node);
    spinlock_unlock(&task_list_lock);

    *taskp = task;
    return 0;
}

void
task_add_thread(struct task *task, struct thread *thread)
{
    spinlock_lock(&task->lock);
    list_insert_tail(&task->threads, &thread->task_node);
    spinlock_unlock(&task->lock);
}

void
task_info(struct task *task)
{
    struct thread *thread;

    if (task == NULL) {
        spinlock_lock(&task_list_lock);

        list_for_each_entry(&task_list, task, node)
            printk("task: %s\n", task->name);

        spinlock_unlock(&task_list_lock);

        return;
    }

    spinlock_lock(&task->lock);

    printk("task: name: %s, threads :\n", task->name);

    list_for_each_entry(&task->threads, thread, task_node)
        printk("task: %s: %s\n", task->name, thread->name);

    spinlock_unlock(&task->lock);
}