-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.c
44 lines (34 loc) · 962 Bytes
/
timer.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
/* vim: set tabstop=4 shiftwidth=4 softtabstop=4 expandtab: */
#include <sys/timerfd.h>
#include "timer.h"
#include "cache_stats.h"
#include "cache_file.h"
int timer_fd;
int timer_get_fd() {
return timer_fd;
}
void timer_thread_init() {
int epoll_fd = mk_api->sched_worker_info()->epoll_fd;
mk_api->epoll_add(epoll_fd, timer_fd, MK_EPOLL_READ, MK_EPOLL_EDGE_TRIGGERED);
}
void timer_read() {
char time[8];
int cnt = 0;
if ((cnt = read(timer_fd, time, 8)) == 8) {
// handle interval tasks!
cache_stats_tick();
cache_file_tick();
}
}
void timer_process_init() {
struct itimerspec timeout;
struct timespec ts;
ts.tv_nsec = 0;
ts.tv_sec = 1;
timeout.it_interval = ts;
timeout.it_value = ts;
timer_fd = timerfd_create(CLOCK_MONOTONIC, TFD_NONBLOCK | TFD_CLOEXEC);
if (timerfd_settime(timer_fd, 0, &timeout, NULL) < 0) {
perror("setting timerfd failed!\n");
}
}