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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
|
/*
* Ivy, C interface
*
* Copyright (C) 1997-2000
* Centre d'Études de la Navigation Aérienne
*
* Timers used in select based main loop
*
* Authors: François-Régis Colin <fcolin@cena.fr>
*
* $Id$
*
* Please refer to file version.h for the
* copyright notice regarding this software
*/
/* Module de gestion des timers autour d'un select */
#include <stdio.h>
#include <sys/types.h>
#include <time.h>
#include <stdlib.h>
#include <memory.h>
#ifdef WIN32
#include <windows.h>
#else
#include <sys/time.h>
#endif
#include "list.h"
#include "timer.h"
#define BIGVALUE 2147483647
#define MILLISEC 1000
static struct timeval *timeoutptr = NULL;
static struct timeval selectTimeout = { BIGVALUE, 0 };
/* la prochaine echeance */
static unsigned long nextTimeout = BIGVALUE;
struct _timer {
struct _timer *next;
int repeat;
unsigned long period;
unsigned long when;
TimerCb callback;
void *user_data;
};
/* liste des timers */
TimerId timers = NULL;
static long currentTime()
{
unsigned long current;
#ifdef WIN32
current = GetTickCount();
#else
struct timeval stamp;
gettimeofday( &stamp, NULL );
current = stamp.tv_sec * MILLISEC + stamp.tv_usec/MILLISEC;
#endif
return current;
}
static void SetNewTimeout( unsigned long current, unsigned long when )
{
unsigned long time;
time = when - current;
nextTimeout = when;
selectTimeout.tv_sec = time / MILLISEC;
selectTimeout.tv_usec = (time - selectTimeout.tv_sec* MILLISEC) * MILLISEC;
if ( timeoutptr == NULL )
timeoutptr = &selectTimeout;
/*printf("New timeout %lu\n", time );*/
}
static void AdjTimeout(unsigned long current)
{
unsigned long newTimeout;
TimerId timer;
if ( timers )
{
/* recherche de la plus courte echeance dans la liste */
newTimeout = timers->when ; /* remise a la premiere valeur */
IVY_LIST_EACH( timers , timer )
{
if ( timer->when < newTimeout )
newTimeout = timer->when;
}
SetNewTimeout( current, newTimeout );
}
else
{
timeoutptr = NULL;
}
}
/* API */
TimerId TimerRepeatAfter( int count, long time, TimerCb cb, void *user_data )
{
unsigned long stamp;
TimerId timer;
/* si y a rien a faire et ben on fait rien */
if ( cb == NULL ) return NULL;
IVY_LIST_ADD( timers, timer )
if ( timer )
{
timer->repeat = count;
timer->callback = cb;
timer->user_data = user_data;
stamp = currentTime();
timer->period = time;
timer->when = stamp + time;
if ( (timer->when < nextTimeout) || (timeoutptr == NULL))
SetNewTimeout( stamp, timer->when );
}
return timer;
}
void TimerRemove( TimerId timer )
{
unsigned long stamp;
if ( !timer ) return;
IVY_LIST_REMOVE( timers, timer );
stamp = currentTime();
AdjTimeout(stamp);
}
void TimerModify( TimerId timer, long time )
{
unsigned long stamp;
if ( !timer ) return;
stamp = currentTime();
timer->period = time;
timer->when = stamp + time;
AdjTimeout(stamp);
}
/* Interface avec select */
struct timeval *TimerGetSmallestTimeout()
{
return timeoutptr;
}
void TimerScan()
{
unsigned long stamp;
TimerId timer;
TimerId next;
unsigned long delta;
int timer_echu = 0;
stamp = currentTime();
/* recherche des timers echu dans la liste */
IVY_LIST_EACH_SAFE( timers , timer, next )
{
if ( timer->when <= stamp )
{
timer_echu++;
delta = stamp - timer->when;
/* call callback */
(*timer->callback)( timer, timer->user_data, delta );
if ( timer->repeat == TIMER_LOOP || --(timer->repeat) )
{
timer->when = stamp + timer->period;
}
else
{
IVY_LIST_REMOVE( timers, timer );
}
}
}
/* recalcul du prochain timeout */
AdjTimeout( stamp );
}
|