-
Notifications
You must be signed in to change notification settings - Fork 1
/
task.c
159 lines (143 loc) · 3.93 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
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#include "task.h"
#include "tty.h"
#include "eth.h"
#include "timer.h"
#include "string.h"
static struct task _idle_task;
struct task *const idle_task = &_idle_task;
static struct task *tasks[NUM_TASKS];
static size_t num_task = 0;
static size_t num_waiting_task = 0;
void init_task()
{
create_task(idle_task, 0 /* don't care */, 0 /* don't care */);
set_current(idle_task);
}
void create_task(struct task *task, uintptr_t stack, void *entry)
{
task->regs.STACK_REG = stack;
task->regs.RETURN_ADDR_REG = (uintptr_t)entry;
task->regs.THREAD_POINTER_REG = (uintptr_t)task;
task->timer.timeout = 1;
task->wait_packet.is_waiting = false;
add_task(task);
}
void add_task(struct task *task)
{
tasks[num_task] = task;
++num_task;
}
static struct task *find_task_waiting_for(uint64_t wait)
{
for (size_t i = 0; i < num_task; ++i)
{
if (tasks[i]->wait & wait)
{
return tasks[i];
}
}
return 0;
}
static int switch_to_task_waiting_for(uint64_t wait)
{
struct task *next = find_task_waiting_for(wait);
if (!next) return 0;
switch_to(current, next);
return 1;
}
static bool task_poll_udp(struct task *task)
{
if (!task->wait_packet.is_waiting) return true;
if ((rdcycle() - task->wait_packet.start) >= task->wait_packet.delta)
{
task->wait_packet.is_waiting = false;
return true;
}
return false;
}
void idle_entry()
{
size_t rr = 0;
while (1)
{
if (tty_poll_recv()) switch_to_task_waiting_for(WAIT_TTY_RECV);
if (tty_poll_send()) switch_to_task_waiting_for(WAIT_TTY_SEND);
if (eth_poll_recv()) switch_to_task_waiting_for(WAIT_ETH_RECV);
if (eth_poll_send()) switch_to_task_waiting_for(WAIT_ETH_SEND);
if (num_waiting_task < num_task)
{
for (size_t i = 0; i < num_task; ++i)
{
struct task *task = tasks[rr];
++rr;
if (rr == num_task) rr = 0;
if (task != current && !task->wait
&& poll_timer(&task->timer) && task_poll_udp(task))
{
switch_to(current, task);
break;
}
}
}
}
}
void sched_yield()
{
switch_to(current, idle_task);
}
void wait(uint64_t w)
{
current->wait |= w;
++num_waiting_task;
sched_yield();
--num_waiting_task;
current->wait &= ~w;
}
void sleep(uint64_t delta)
{
set_timer(¤t->timer, delta);
sched_yield();
}
void exit()
{
wait(WAIT_EXIT);
}
static bool task_match_udp(struct task *task, uint16_t dport, ipv6_addr_t saddr, uint16_t sport)
{
return (task->wait_packet.dport == 0 || task->wait_packet.dport == dport)
&& (ipv6_is_unspecified(task->wait_packet.expected_saddr)
|| ipv6_eq(task->wait_packet.expected_saddr, saddr))
&& (task->wait_packet.expected_sport == 0 || task->wait_packet.expected_sport == sport);
}
static struct task *task_find_udp(uint16_t dport, ipv6_addr_t saddr, uint16_t sport)
{
for (size_t i = 0; i < num_task; ++i)
{
struct task *task = tasks[i];
if (task->wait_packet.is_waiting && task_match_udp(task, dport, saddr, sport))
{
return task;
}
}
return 0;
}
void task_dispatch_udp(uint16_t dport, ipv6_addr_t saddr, uint16_t sport, void *buff, size_t len)
{
struct task *target_task = task_find_udp(dport, saddr, sport);
if (!target_task)
{
sched_yield();
target_task = task_find_udp(dport, saddr, sport);
}
if (!target_task)
{
return;
}
target_task->wait_packet.is_waiting = false;
*target_task->wait_packet.saddr = saddr;
*target_task->wait_packet.sport = sport;
size_t copy_len = target_task->wait_packet.len;
if (len < copy_len) copy_len = len;
memcpy(target_task->wait_packet.buff, buff, copy_len);
*target_task->wait_packet.out_len = copy_len;
}