summaryrefslogtreecommitdiff
path: root/kern/task.c
blob: 51559434c07879c66c78da6d103ac052f710ccb5 (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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 * 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>
#include <vm/vm_map.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_head(&task_list, &kernel_task->node);
}

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

    task = kmem_cache_alloc(&task_cache);

    if (task == NULL) {
        error = ERROR_NOMEM;
        goto error_task;
    }

    error = vm_map_create(&map);

    if (error)
        goto error_map;

    task_init(task, name, map);

    spinlock_lock(&task_list_lock);
    list_insert_tail(&task_list, &task->node);
    spinlock_unlock(&task_list_lock);

    *taskp = task;
    return 0;

error_map:
    kmem_cache_free(&task_cache, task);
error_task:
    return error;
}

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_remove_thread(struct task *task, struct thread *thread)
{
    spinlock_lock(&task->lock);
    list_remove(&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);
}