-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtimer_thread.c
60 lines (51 loc) · 1.37 KB
/
timer_thread.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
/*
* timer_thread.h
* Source file for timer thread
* The main job of timer thread is to wake up periodically to check
* out the queues and to see if some timers fires for the stuffs in
* the queue. If so, do the sth and remove the object from the queue.
* Sep 26, 2013
* root@davejingtian.org
* http://davejingtian.org
*/
#include <stdio.h>
#include <pthread.h>
#include "timer_queue.h"
#include "timer_thread.h"
extern pthread_mutex_t timer_queue_mutex;
/* Methods */
void *timer_thread_main(void *data)
{
int i;
int j;
timer_queue_msg *tqm;
while (1)
{
/* Lock */
pthread_mutex_lock(&timer_queue_mutex);
printf("timer thread: got the timer queue lock [%lu]\n", time(NULL));
/* Go thru the queues */
for (i = 0; i < TIMER_QUEUE_NUM_MAX; i++)
{
tqm = tq_get_head_msg(i);
for (j = 0; j < tq_get_msg_num(i); j++)
{
if (tqm[j].timer <= time(NULL))
{
printf("timer thread: remove the msg at time [%lu] in queue [%d]\n", time(NULL), i);
tq_display_msg(&(tqm[j]));
if (tqm[j].type == TIMER_QUEUE_MSG_TYPE_MAC)
tq_del_msg_on_str(tqm[j].type, tqm[j].mac, i);
else
tq_del_msg_on_str(tqm[j].type, tqm[j].ipv4, i);
}
else
break;
}
}
/* Unlock */
pthread_mutex_unlock(&timer_queue_mutex);
printf("timer thread: released the timer queue lock [%lu]\n", time(NULL));
sleep(TIMER_THREAD_WAKEUP_TIME);
}
}