-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwatchdog.c
More file actions
110 lines (99 loc) · 2.89 KB
/
watchdog.c
File metadata and controls
110 lines (99 loc) · 2.89 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
106
107
108
109
110
#include "watchdog.h"
long long current_timestamp() {
struct timeval te;
gettimeofday(&te, NULL); // get current time
long long milliseconds =
te.tv_sec * 1000LL + te.tv_usec / 1000; // calculate milliseconds
// printf("milliseconds: %lld\n", milliseconds);
return milliseconds;
}
void feed_the_dog(pthread_t task_id) {
// last_run = current_timestamp();
TaskListEntry *temp = watchdog.t_list.head;
while (temp != watchdog.t_list.tail) {
if (temp->task_id == task_id) {
temp->last_run = current_timestamp();
printf("TASK %ld fed the Dog!\n", task_id);
}
temp = temp->next;
}
}
void add_to_watchlist(pthread_t task_id, unsigned int timeout,
WDT_CALLBACK cb) {
if (is_exists(task_id)) {
printf("DISCARDING THE TASK BECAUSE IT EXISTS ALREADY!");
return;
}
TaskListEntry *new_entry = (TaskListEntry *)malloc(sizeof(TaskListEntry));
new_entry->last_run = current_timestamp();
new_entry->next = watchdog.t_list.tail;
new_entry->timeout = timeout;
new_entry->prev = watchdog.t_list.tail->prev;
new_entry->task_id = task_id;
new_entry->callback = cb;
watchdog.t_list.tail->prev->next = new_entry;
watchdog.t_list.tail->prev = new_entry;
}
void *watchDog(void *args) {
long long temp_time;
while (1) {
TaskListEntry *temp = watchdog.t_list.head->next;
while (temp != watchdog.t_list.tail) {
temp_time = current_timestamp();
printf("here! %lld\n", temp_time - temp->last_run);
if (temp_time - temp->last_run > temp->timeout) {
if (temp->callback != NULL)
temp->callback(NULL);
printf("the watchdog was not fed well!\n");
fire_the_dog();
}
// temp->last_run = temp_time;
if (temp->next != watchdog.t_list.tail) {
temp = temp->next;
} else {
break;
}
}
RS_SLEEP(watchdog.sleep_time);
}
return NULL;
}
void fire_the_dog() {
if (watchdog.callback != NULL)
watchdog.callback(watchdog.callback_arg);
exit(-1);
};
void init_the_dog(long long sleep_time, WDT_CALLBACK cb) {
TaskListEntry *head = (TaskListEntry *)malloc(sizeof(TaskListEntry));
TaskListEntry *tail = (TaskListEntry *)malloc(sizeof(TaskListEntry));
head->next = tail;
tail->prev = head;
watchdog.t_list.head = head;
watchdog.t_list.tail = tail;
watchdog.sleep_time = sleep_time;
watchdog.callback = cb;
head->prev = NULL;
head->last_run = current_timestamp();
head->task_id = 0;
tail->next = NULL;
tail->last_run = current_timestamp();
tail->task_id = 0;
tail->prev = head;
head->next = tail;
}
int is_exists(long long task_id) {
int found = 0;
TaskListEntry *temp = watchdog.t_list.head->next;
while (temp != watchdog.t_list.tail) {
if (temp->task_id == task_id) {
found = 1;
break;
}
if (temp->next != watchdog.t_list.tail) {
temp = temp->next;
} else {
break;
}
}
return found;
}