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
|
#include <stdio.h>
#include <stdlib.h>
#include <sys/time.h>
#include <string.h>
#include "triton_p.h"
static pthread_thread_t timer_thr;
static pthread_mutex_t timers_lock=PTHREAD_MUTEX_INITIALIZER;
static LIST_HEAD(timers);
static timespec expire_ts;
static pthread_cond_t cond=PTHREAD_COND_INITIALIZER;
static void tv_add(struct timeval *tv,int msec);
void timer_init(void)
{
}
void timer_run(void)
{
pthread_create(&timer_thr,NULL,timer_thread,NULL);
}
void timer_terminate(void)
{
pthread_cancel(&timer_thr);
pthread_join(&timer_thr);
}
void *timer_thread(void *arg)
{
struct triton_timer_t *t;
struct timeval tv;
pthread_mutex_lock(&timers_lock);
while(1)
{
if (expire_ts.tv_sec)
pthread_cond_timedwait(&cond,&timers_lock,&expire_ts);
else
pthread_cond_wait(&cond,&timers_lock);
gettimeofday(&tv,NULL);
while(1)
{
if (list_empty(&timers))
{
expire_ts.tv_sec=0;
break;
}
t=list_entry(timers.next,typeof(*t),entry);
if (t->expire_tv.tv_sec>tv.tv_sec || (t->expire_tv.tv_sec==tv.tv_sec && t->expire_tv.tv_usec>=tv.tv_usec))
{
expire_ts.tv_sec=t->expire_tv.tv_sec;
expire_ts.tv_nsec=t->expire_tv.tv_usec*1000;
break;
}
list_del(&t->entry3);
pthread_mutex_lock(&t->ctx->lock);
t->pending=1;
list_add_tail(&t->entry2,&t->ctx->pending_timers);
triton_queue_ctx(&t->ctx);
pthread_mutex_unlock(&t->ctx->lock);
}
}
}
void triton_timer_add(struct triton_timer_t *t)
{
struct triton_timer_t *t1;
pthread_mutex_lock(&timers_lock);
list_for_each_entry(t1,&timers,entry3)
{
if (t->expire_tv.tv_sec<t1.expire_tv.tv_sec || (t->expire_tv.tv_sec==t1->expire_tv.tv_sec && t->expire_tv.tv_usec<t1->expire_tv.tv_usec))
break;
}
list_add_tail(&t->entry3,&t1->entry3);
pthread_mutex_unlock(&timers_lock);
}
void triton_timer_del(struct triton_timer_t *t)
{
pthread_mutex_lock(&timers_lock);
pthread_mutex_lock(&t->ctx->lock);
if (t->pending)
list_del(&t->entry2);
else
{
list_del(&t->entry3);
if (t->expire_tv.tv_sec<expire_ts.tv_sec || (t->expire_tv.tv_sec==expire_ts.tv_sec && t->expire_tv.tv_usec<expire_ts.tv_nsec/1000))
pthread_cond_signal(&cond);
}
pthread_mutex_unlock(&t->ctx->lock);
pthread_mutex_unlock(&timers_lock);
}
|