-
Notifications
You must be signed in to change notification settings - Fork 10
/
task.c
106 lines (78 loc) · 1.67 KB
/
task.c
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
#include <kernel.h>
#include <list.h>
task_t* running_task = NULL;
static uint32_t pid_n = 1000;
uint32_t num_tasks;
static list_t* tasks = NULL;
void
idle() {
debug_printf("task_idle started.\n");
while(1) {
// __asm__ __volatile__("sti"); // enable interrupts
__asm__ __volatile__("hlt"); // idle the CPU until a interrupt fires
}
}
task_t*
task_get() {
return running_task;
}
void
task_add(task_t* t) {
tasks = list_add(tasks, t);
num_tasks++;
}
void
tasking() {
task_add(thread_create(idle, -1, 100, "idle"));
// create_user_task_test();
}
void
task_block() {
running_task->status = TS_BLOCKED;
schedule();
}
void
task_listen(uint32_t l) {
ASSERT_PANIC(running_task != NULL);
running_task->listen |= l;
}
uint8_t
_task_get_pid(list_t* l, void* data) {
// printf("get_pid: %x == %d ?\n", (uint32_t)data, ((task_t*)(l->data))->id);
if( ((task_t*)(l->data))->pid == (uint32_t)data ) {
return FALSE;
}
return TRUE;
}
static uint32_t t[2]; // 0: pid; 1: pointer to pid (if found)
task_t*
task_from_pid(uint32_t pid) {
list_t *l;
l = list_foreach(tasks, _task_get_pid, (uint32_t*)pid);
if( l )
return (task_t*)l->data;
return NULL;
}
uint32_t
pid_new() {
return ++pid_n;
}
uint32_t
pid() {
ASSERT_PANIC(running_task != NULL);
return running_task->pid;
}
task_t*
task_foreach(task_iterator_t it, void* udata) {
list_t* item;
ASSERT_PANIC( tasks != NULL );
if( it == NULL )
return;
if(tasks->magic != LIST_MAGIC)
PANIC("task list is corromped!");
item = tasks;
do {
if( it( (task_t*)item->data, udata) )
return (task_t*)item->data;
} while( (item = item->next) != NULL );
}