-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.c
More file actions
105 lines (97 loc) · 2.73 KB
/
timer.c
File metadata and controls
105 lines (97 loc) · 2.73 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
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
#include "global_define.h"
#include "timer.h"
#include "multi_task.h"
static struct TIMERCTL timerctl;
void init_pit() {
io_out8(PIT_CTRL, 0x34);
io_out8(PIT_CNT0, 0x9c);
io_out8(PIT_CNT0, 0x2e);
timerctl.count = 0;
for (int i = 0; i < MAX_TIMER; ++i) {
timerctl.timer[i].flags = 0; //未使用
timerctl.timer[i].fifo = 0; //队列必须初始化为0
}
}
/**
* 分配一个时钟中断对象
*/
struct TIMER *timer_alloc() {
for (int i = 0; i < MAX_TIMER; i++) {
if (timerctl.timer[i].flags == 0) {
timerctl.timer[i].flags = TIMER_FLAGS_ALLOC;
return &timerctl.timer[i];
}
}
return 0;
}
/**
* 释放一个时钟中断对象
*/
void timer_free(struct TIMER *timer) {
timer->flags = 0;
return;
}
/**
* 初始化时钟中断对象
*/
void timer_init(struct TIMER *timer, struct FIFO8 *fifo, unsigned char data) {
timer->fifo = fifo;
timer->data = data;
return;
}
/**
* 设置超时时间片
*/
void timer_setTime(struct TIMER *timer, unsigned int timerout) {
// int eflags;
// eflags = io_load_eflags();
// io_cli(); //暂停接收中断信号
timer->timeout = timerout;
timer->flags = TIMER_FLAGS_USING;
// io_store_eflags(eflags); //恢复接收中断信号
return;
}
void timer_settime(struct TIMER *timer, unsigned int timerout) {
// int eflags;
// eflags = io_load_eflags();
// io_cli(); //暂停接收中断信号
timer->timeout = timerout;
timer->flags = TIMER_FLAGS_USING;
// io_store_eflags(eflags); //恢复接收中断信号
return;
}
/**
* 获取时钟中断控制器结构体对象
*/
struct TIMERCTL *getTimerController() {
return &timerctl;
}
/**
* 处理时钟中断函数
*/
void intHandlerForTimer(char *esp) {
//multi_task_init(); //初始化任务调度时钟
//struct TIMER *mt_timer = getMultiTaskTimer();
io_out8(PIC0_OCW2, 0x20); //重复进行中断
timerctl.count++;
int ts = 0;
for (int i = 0; i < MAX_TIMER; ++i) {
if (timerctl.timer[i].flags == TIMER_FLAGS_USING) {
timerctl.timer[i].timeout--;
if (timerctl.timer[i].timeout == 0) {
timerctl.timer[i].flags = TIMER_FLAGS_ALLOC;
fifo8_put(timerctl.timer[i].fifo, timerctl.timer[i].data); //激活超时事件
//判断是否为进程切换时间片超时
if (&timerctl.timer[i] == getTaskTimer()) {
ts = 1;
}
}
}
//如果进程切换时间片不为0,开始切换进程
if (ts == 1) {
task_switch();
//taskswitch9();
}
}
return;
}