-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_timer.c
69 lines (61 loc) · 1.37 KB
/
test_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
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
#include "windows.h"
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include "test_timer.h"
struct test_timer *head_handle;
int test_timer_init(void)
{
head_handle = NULL;
return 0;
}
int test_timer_start(struct test_timer *handle, int timeout)
{
struct test_timer *target = head_handle;
handle->timeout = test_timer_get_ticks() + timeout;
while (target)
{
if (target == handle)
return -1; // already exist.
target = target->next;
}
handle->next = head_handle;
head_handle = handle;
return 0;
}
void test_timer_stop(struct test_timer *handle)
{
struct test_timer **curr;
for (curr = &head_handle; *curr;)
{
struct test_timer *entry = *curr;
if (entry == handle)
{
*curr = entry->next;
return;
}
else
{
curr = &entry->next;
}
}
}
void test_timer_polling(void)
{
struct test_timer *target;
for (target = head_handle; target; target = target->next)
{
if (target->timeout <= test_timer_get_ticks())
{
target->callback(target->arg);
}
}
}
void test_timer_print(void)
{
struct test_timer *target;
for (target = head_handle; target; target = target->next)
{
printf("cur: %d, timeout: %d\n", test_timer_get_ticks(), target->timeout);
}
}