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 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
|
/* Variables globales */
typedef struct Mon_Signal_ {
int Type ;
double NextCall ;
double Interval ;
double IntervalOrigin ;
void *fct ;
void *data ;
} Mon_Signal ;
static MonSignal Signaux[2] ;
static int NSignaux = 0 ;
/*
* U p d a t e s _ N e x t C a l l
*
*/
static void Updates_NextCall ( void )
{
int i, n = 0, j=0 ;
double d, ref ;
struct itimerval rttimer;
struct itimerval old_rttimer;
#ifdef DEBUG
fprintf (stderr, " UPDATE ");
#endif
if ( NSignaux != 0 )
{
/* Computes the closest expiration date */
d = DBL_MAX ;
for ( i = 0 ; i < NSignaux ; i++ )
{
n = 1 ;
if ( Signaux[i].NextCall < d )
d = Signaux[i].NextCall ;
#ifdef DEBUG
fprintf (stderr, " i %d tocomp %f",i,Signaux[i].NextCall);
#endif
}
/* Now sets the timer to the next expiration date */
if ( ! n )
{
#ifdef DEBUG
fprintf (stderr, " Disarms because no good signal anymore !!!");
#endif
rttimer.it_value.tv_sec = 0 ;
rttimer.it_value.tv_usec = 0 ;
NSignaux = 0 ;
}
else
{
ref = Mon_GetClock();
if ( d <= ref )
{
rttimer.it_value.tv_sec = 0 ;
rttimer.it_value.tv_usec = 100 ;
}
else
j = 1 ;
}
rttimer.it_interval.tv_sec = 0 ;
rttimer.it_interval.tv_usec = 0 ;
signal ( SIGALRM, (void (*)())Timer_Expiration );
if ( j )
{
LASTCALL = Mon_GetClock(); ;
d = d - LASTCALL ;
i = (int)d ;
j = (int)(((d - (double)i)*1000000.0)+0.5) ;
rttimer.it_value.tv_sec = i ;
rttimer.it_value.tv_usec = j ;
if ( (rttimer.it_value.tv_sec == 0) && (rttimer.it_value.tv_usec < 100) )
rttimer.it_value.tv_usec = 100 ;
#ifdef DEBUG
fprintf (stderr, " Arms at %d s %d musecs at %f pour %f",rttimer.it_value.tv_sec,rttimer.it_value.tv_usec,LASTCALL,(d+LASTCALL));
#endif
}
setitimer (ITIMER_REAL, &rttimer, &old_rttimer);
}
else
{
#ifdef DEBUG
fprintf (stderr, " Disarms because blocked or no signal !!!");
#endif
rttimer.it_value.tv_sec = 0 ;
rttimer.it_value.tv_usec = 0 ;
rttimer.it_interval.tv_sec = 0 ;
rttimer.it_interval.tv_usec = 0 ;
signal ( SIGALRM, (void (*)())Timer_Expiration );
setitimer (ITIMER_REAL, &rttimer, &old_rttimer);
}
}
/*
* T i m e r _ E x p i r a t i o n
*
*/
static void Timer_Expiration ( void )
{
int i, j ;
double ref ;
ref = Mon_GetClock() ;
#ifdef DEBUG
fprintf (stderr, " Expires a %f [%f]",ref,ref-LASTCALL);
#endif
/*
--- Loops on the signals
*/
for ( i = 0 ; i < NSignaux ; i++ )
{
#ifdef DEBUG
fprintf (stderr, " i %d comp %f ref %f",i,(Signaux[i].NextCall-ref),ref);
#endif
/*
Looks if this this signal has or should have expired
*/
if ( (fabs(ref - Signaux[i].NextCall) < 0.000100) || (ref > Signals[i].NextCall) )
{
/* Now calls the registered function */
if ( Signaux[i].fct != (MCore_Proc *)NULL )
{
#ifdef DEBUG
fprintf (stderr, " Calls fct signal %d",i);
#endif
j = Signaux[i].fct( Signaux[i].data ) ;
}
/* If this signal has to stay, rearms it for the next pass */
...............
}
}
/*
--- Destroys signals and resets next call if any remains
*/
#ifdef DEBUG
fprintf (stderr, " Destroy?");
#endif
/* i = Checks_For_Destroyed_Signals(); */
#ifdef DEBUG
fprintf (stderr, " Update?");
#endif
Updates_NextCall();
return ;
}
/*
* A d d T i m e r
*
*/
int AddTimer ( int type, double start, double interval, void *proc, void *clientdata )
{
int i, Type, start_realsecs, start_realmusecs, day_of_week ;
double start_micro, interval_micro, d ;
time_t now ;
struct tm *tbroken ;
if ( (type < 0) || (type > 1) )
Type = 0 ;
else
Type = type ;
i = NSignaux ;
/*
--- If we use some predefined values, computes now the real values
*/
/* Starting value */
start_micro = start ;
/* Interval value */
interval_micro = interval ;
/*
--- Computes what it makes in real
*/
start_realsecs = (int)start_micro ;
start_realmusecs = (int)((start_micro - (double)start_realsecs)*1000000.0) ;
/*
--- Stores the values into the buffer
*/
d = Mon_GetClock() ;
Signaux[i].NextCall = d + (double)start_realsecs + ((double)start_realmusecs/1000000.) ;
Signaux[i].Interval = interval_micro ;
Signaux[i].IntervalOrigin = interval ;
Signaux[i].fct = proc ;
Signaux[i].data = clientdata ;
/*
--- Now starts the timer
*/
NSignaux = NSignaux + 1 ;
Updates_NextCall ();
#ifdef DEBUG
fprintf (stderr, " Add a signal (%f s [%f][clock %f] at %g interval) => %d signals",
(Signaux[i].NextCall - d),Signaux[i].NextCall,d,interval,NSignaux);
#endif
return SUCCESS ;
} |
Partager