-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.c
More file actions
52 lines (38 loc) · 1.04 KB
/
thread.c
File metadata and controls
52 lines (38 loc) · 1.04 KB
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
#if CHAPTER >= 9
//
// thread.c -- Defines functions and structures for multithreading.
// Written for JamesM's kernel development tutorials.
//
#include "thread.h"
thread_t *current_thread;
uint32_t next_tid = 0;
void thread_exit ();
extern void _create_thread(int (*)(void*), void*, uint32_t*, thread_t*);
thread_t *init_threading ()
{
thread_t *thread = kmalloc (sizeof (thread_t));
thread->id = next_tid++;
current_thread = thread;
return thread;
}
thread_t *create_thread (int (*fn)(void*), void *arg, uint32_t *stack)
{
thread_t *thread = kmalloc (sizeof (thread_t));
memset (thread, 0, sizeof (thread_t));
thread->id = next_tid++;
*--stack = (uint32_t)arg;
*--stack = (uint32_t)&thread_exit;
*--stack = (uint32_t)fn;
thread->esp = (uint32_t)stack;
thread->ebp = 0;
thread->eflags = 0x200; // Interrupts enabled.
thread_is_ready(thread);
return thread;
}
void thread_exit ()
{
register uint32_t val asm ("eax");
printk ("Thread exited with value %d\n", val);
for (;;) ;
}
#endif // CHAPTER >= 9