-
Notifications
You must be signed in to change notification settings - Fork 18
/
uthread.c
108 lines (92 loc) · 2.36 KB
/
uthread.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
107
108
#include "types.h"
#include "stat.h"
#include "user.h"
/* Possible states of a thread; */
#define FREE 0x0
#define RUNNING 0x1
#define RUNNABLE 0x2
#define STACK_SIZE 8192
#define MAX_THREAD 4
typedef struct thread thread_t, *thread_p;
typedef struct mutex mutex_t, *mutex_p;
struct thread {
int sp; /* curent stack pointer */
char stack[STACK_SIZE]; /* the thread's stack */
int state; /* running, runnable, waiting */
};
static thread_t all_thread[MAX_THREAD];
thread_p current_thread;
thread_p next_thread;
extern void thread_switch(void);
void
thread_init(void)
{
current_thread = &all_thread[0];
current_thread->state = RUNNING;
}
static void
thread_schedule(void)
{
thread_p t;
/* Find another runnable thread. */
for (t = all_thread; t < all_thread + MAX_THREAD; t++) {
if (t->state == RUNNABLE && t != current_thread) {
next_thread = t;
break;
}
}
if (t >= all_thread + MAX_THREAD && current_thread->state == RUNNABLE) {
/* The current thread is the only runnable thread; run it. */
next_thread = current_thread;
}
if (next_thread == 0) {
printf(2, "thread_schedule: no runnable threads; deadlock\n");
exit();
}
if (current_thread != next_thread) { /* switch threads? */
next_thread->state = RUNNING;
thread_switch();
} else
next_thread = 0;
}
void
thread_create(void (*func)())
{
thread_p t;
for (t = all_thread; t < all_thread + MAX_THREAD; t++) {
if (t->state == FREE) break;
}
t->sp = (int) (t->stack + STACK_SIZE); // set sp to the top of the stack
t->sp -= 4; // space for return address
* (int *) (t->sp) = (int)func; // push return address on stack
t->sp -= 32; // space for registers that thread_switch will push
t->state = RUNNABLE;
}
void
thread_yield(void)
{
current_thread->state = RUNNABLE;
thread_schedule();
}
static void
mythread(void)
{
int i;
printf(1, "my thread running\n");
for (i = 0; i < 100; i++) {
printf(1, "my thread 0x%x\n", (int) current_thread);
thread_yield();
}
printf(1, "my thread: exit\n");
current_thread->state = FREE;
thread_schedule();
}
int
main(int argc, char *argv[])
{
thread_init();
thread_create(mythread);
thread_create(mythread);
thread_schedule();
return 0;
}