-
Notifications
You must be signed in to change notification settings - Fork 4
/
ktimer.c
66 lines (56 loc) · 1.3 KB
/
ktimer.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
#define _GNU_SOURCE
#include <unistd.h>
#include <pthread.h>
#include "ktimer.h"
enum {
S_TIMER_STARTING,
S_TIMER_STARTED,
S_TIMER_STOPPING,
S_TIMER_STOPPED,
};
static void *__ktimer(void *arg)
{
struct ktimer *t = (struct ktimer*)arg;
t->state = S_TIMER_STARTED;
for(;;) {
usleep(1000*t->period_ms);
spin_pdr_lock(&t->lock);
if(t->state == S_TIMER_STOPPING)
break;
spin_pdr_unlock(&t->lock);
t->callback(t->callback_arg);
}
t->state = S_TIMER_STOPPED;
spin_pdr_unlock(&t->lock);
}
void ktimer_init(struct ktimer *t, void (*callback)(void*), void* arg)
{
t->state = S_TIMER_STOPPED;
t->callback = callback;
t->callback_arg = arg;
spin_pdr_init(&t->lock);
}
int ktimer_start(struct ktimer *t, unsigned int period_ms)
{
if(t->state != S_TIMER_STOPPED)
return -1;
t->state = S_TIMER_STARTING;
t->period_ms = period_ms;
pthread_t thread;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
pthread_create(&thread, &attr, __ktimer, t);
}
int ktimer_stop(struct ktimer *t)
{
if(t->state != S_TIMER_STARTED)
return -1;
spin_pdr_lock(&t->lock);
t->state = S_TIMER_STOPPING;
spin_pdr_unlock(&t->lock);
while(t->state != S_TIMER_STOPPED) {
pthread_yield();
cpu_relax();
}
}