-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathtimer.c
More file actions
85 lines (73 loc) · 2.32 KB
/
timer.c
File metadata and controls
85 lines (73 loc) · 2.32 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
#include <stdio.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/time.h>
#include <string.h>
#include <assert.h>
#include "timer.h"
#include "uthread_inner.h"
static struct timer_wheel timer;
void *
create_timewheel(void* arg) {
// printf("create timewheel successfully\n");
timer.current = 0;
/* 打印时间轮 */
// printf("tick里的槽位情况: ");
// int i = 0;
// for (i = 0; i < 10; ++i) {
// if (timer.slot[i])
// printf("%ld: ", timer.slot[i]->ut->p->tid);
// }
signal(SIGALRM, tick);
/* 时间轮时间间隔为10ms */
struct itimerval new_value, old_value;
new_value.it_value.tv_sec = 0;
new_value.it_value.tv_usec = 10000;
new_value.it_interval.tv_sec = 0;
new_value.it_interval.tv_usec = 10000;
setitimer(ITIMER_REAL, &new_value, &old_value);
// printf("时间轮初始化完毕\n");
for(;;){
sleep(1000);
}
}
void
tick(int signo) {
// printf("tick\n");
struct timer_node **cur = &timer.slot[timer.current];
// printf("current: %d\n", timer.current);
while (*cur) {
struct timer_node *curr = *cur;
// printf("tick rotation: %d\n", curr->rotation);
// printf("tick ut: %ld\n", curr->ut->id);
if (curr->rotation > 0) {
curr->rotation--;
cur = &curr->next;
} else {
// printf("线程id: %ld\n", curr->ut->p->tid);
if (curr->ut->is_wating_yield_signal == 1) {
// printf("向主线程发出指示yield的信号\n");
assert(pthread_kill(curr->ut->p->tid, SIGUSR1) == 0);
// printf("after\n");
}
*cur = curr->next;
free(curr);
curr = NULL; // NOTE!!!
// printf("free掉了\n");
}
}
timer.current = (timer.current + 1) % TIME_WHEEL_SIZE;
}
void
add_timer(int len, struct uthread *ut) {
int pos = (len + timer.current) % TIME_WHEEL_SIZE;
// printf("pos:%d\n", pos);
struct timer_node *node = (struct timer_node*)malloc(sizeof(struct timer_node));
// 插入到对应格子的链表头部即可, O(1)复杂度
// 头插法
node->rotation = len / TIME_WHEEL_SIZE;
node->ut = ut;
node->next = timer.slot[pos];
timer.slot[pos] = node;
}