summaryrefslogtreecommitdiff
path: root/src/timer.c
blob: 7f9394a87b29003359ea4107ca45e7b0018c8e01 (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 *	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.dgac.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;
        unsigned char mark2Remove;
	};

/* 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 ltime;
	ltime = (when <= current) ? 0 : when - current;
	nextTimeout = when;
	selectTimeout.tv_sec = ltime / MILLISEC;
	selectTimeout.tv_usec = (ltime - selectTimeout.tv_sec* MILLISEC) * MILLISEC;
	if ( timeoutptr == NULL )
				timeoutptr = &selectTimeout;
	/*printf("New timeout %lu\n", ltime );*/
}
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->mark2Remove) && (timer->when < newTimeout  ))
		    newTimeout = timer->when;
		}
	SetNewTimeout( current, newTimeout );
	}
	else
	{
	timeoutptr = NULL;
	}
}

/* API */

TimerId TimerRepeatAfter( int count, long ltime, 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_START( timers, timer )
		timer->repeat = count;
		timer->callback = cb;
		timer->user_data = user_data;
		stamp = currentTime();
		timer->period = ltime;
		timer->when =  stamp + ltime;
		timer->mark2Remove = 0;
		if ( (timer->when < nextTimeout) || (timeoutptr == NULL))
			SetNewTimeout( stamp, timer->when );
	IVY_LIST_ADD_END( timers, timer )
	return timer;
}
void TimerRemove( TimerId timer )
{
	unsigned long stamp;
	if (( !timer ) || (timer->mark2Remove)) return;
	//	IVY_LIST_REMOVE( timers, timer );
	timer->mark2Remove = 1;
	stamp = currentTime();
	AdjTimeout(stamp);
}
void TimerModify( TimerId timer, long ltime )
{
	unsigned long stamp;
	if (( !timer ) || (timer->mark2Remove)) return;

	stamp = currentTime();
	timer->period = ltime;
	timer->when = stamp + ltime;
	AdjTimeout(stamp);
}
/* Interface avec select */

struct timeval *TimerGetSmallestTimeout()
{
	unsigned long stamp;
	/* recalcul du prochain timeout */
	stamp = currentTime();
	AdjTimeout( stamp );
	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->mark2Remove) )
	    {
	      timer_echu++;
	      delta = stamp - timer->when;
	      /* call callback */
	      (*timer->callback)( timer, timer->user_data, delta );
	    }
	}

	IVY_LIST_EACH_SAFE( timers , timer, next )
	{
	  if (timer->mark2Remove) {
	    IVY_LIST_REMOVE( timers, timer );
	  }
	  else if ( timer->when <= stamp )
	    {
	      if ( timer->repeat == TIMER_LOOP || --(timer->repeat) )
		{
		  timer->when = stamp + timer->period;
		}
	      else
		{
		  IVY_LIST_REMOVE( timers, timer );
		}
	    }
	}
	
}