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 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
|
/*
* Copyright (c) 1999-2003 Rinet Corp., Novosibirsk, Russia
*
* Redistribution and use in source forms, with and without modification,
* are permitted provided that this entire comment appears intact.
*
* THIS SOURCE CODE IS PROVIDED ``AS IS'' WITHOUT ANY WARRANTIES OF ANY KIND.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <sys/types.h>
#include <sys/time.h>
#include <time.h>
#include <stdlib.h>
#include <string.h>
#include "events.h"
#define dprintf(x) /* nope */
static EVENT *first_event = 0; /* first system event in table */
/*
* Subtract 2 timeval structs: out = out - in. Out result always greater 0.
*/
void
tv_sub(out, in)
struct timeval *out;
const struct timeval *in;
{
if (out->tv_sec > in->tv_sec) {
out->tv_sec -= in->tv_sec;
if (out->tv_usec >= in->tv_usec) {
out->tv_usec -= in->tv_usec;
} else {
out->tv_sec--;
out->tv_usec += 1000000L - in->tv_usec;
}
} else if (out->tv_sec == in->tv_sec) {
out->tv_sec = 0L;
out->tv_usec -= in->tv_usec;
if (out->tv_usec < 1)
out->tv_usec = 100L; /* not zero anyway */
} else {
out->tv_sec = 0L;
out->tv_usec = 100L; /* not zero anyway */
}
}
void
tv_add(out, in)
struct timeval *out;
const struct timeval *in;
{
out->tv_sec += in->tv_sec;
out->tv_usec += in->tv_usec;
if (out->tv_usec >= 1000000L) {
out->tv_sec++;
out->tv_usec -= 1000000L;
}
}
/*
* Round timeval to seconds.
*/
u_long
tv_round(in)
const struct timeval *in;
{
u_long sec = in->tv_sec;
if (in->tv_usec >= 500000L)
sec++;
return sec;
}
/*
* Return time difference in milliseconds.
*/
u_long
tv_diff(tvp1, tvp2)
const struct timeval *tvp1, *tvp2;
{
struct timeval diff;
if (!timerisset(tvp1) || !timerisset(tvp2))
return 0;
if (timercmp(tvp1, tvp2, >)) {
diff = *tvp1;
tv_sub(&diff, tvp2);
} else {
diff = *tvp2;
tv_sub(&diff, tvp1);
}
return (diff.tv_sec * 1000 + diff.tv_usec / 1000);
}
/*
* Shift the time to be sharp at (12am + N * period), local time.
*/
void
tv_sharp(tvp, period)
struct timeval *tvp;
int period;
{
time_t defect;
struct tm *tm;
if (!tvp) return;
tm = localtime((time_t *)&tvp->tv_sec);
defect = tm->tm_sec + 60 * tm->tm_min + 3600 * tm->tm_hour;
defect %= period;
period -= defect;
if (period < 1) period = 1;
tvp->tv_sec += period;
tvp->tv_usec = (long)random() % 1000000L;
}
/*
* Execute pending event and schedule the next nearest.
*/
int
select_event(tvp)
struct timeval *tvp;
{
EVENT *ep, *next_event;
struct timeval now, gap, earliest;
gettimeofday(&now, 0);
gap.tv_sec = 0;
gap.tv_usec = 1000L; /* 0.001sec grip */
tv_add(&gap, &now);
again:
next_event = 0;
timerclear(&earliest);
for (ep = first_event; ep; ep = ep->next) {
/* skip over the empty slots */
if (!ep->func) continue;
if (timercmp(&gap, &ep->tv, >)) {
void (*func)() = ep->func;
ep->func = 0; /* free event slot before */
dprintf(("-call_event(%p/%p)", func, ep->arg));
(*func)(ep->arg); /* call event function */
goto again;
} else if (!timerisset(&earliest) ||
timercmp(&ep->tv, &earliest, <)) {
earliest = ep->tv;
next_event = ep;
}
}
if (!next_event) { /* no more awaiting events */
dprintf(("select_event: no timeout"));
return 1; /* timeout undefined */
}
if (tvp) {
tv_sub(&earliest, &now);
*tvp = earliest;
dprintf(("=wait_event(%p/%p): timeout=%u.%03d",
next_event->func, next_event->arg,
(unsigned)tvp->tv_sec, (int)(tvp->tv_usec / 1000)));
}
return 0; /* timeout defined */
}
/*
* Add the new system event to be executed at the given time.
*/
int
add_event(tvp, func, arg)
struct timeval *tvp;
void (*func)(void *);
void *arg;
{
EVENT *ep, *prev = 0, *next = 0;
struct timeval now, gap;
if (!tvp) {
gettimeofday(&now, 0);
gap.tv_sec = 0;
gap.tv_usec = 250000L; /* 0.25sec mean a bit later */
tv_add(&gap, &now);
tvp = ⪆
}
/*
* The same event in queue may cause a looping! Prevent it.
*/
for (ep = first_event; ep; ep = ep->next) {
if (ep->func == func && ep->arg == arg) {
ep->tv = *tvp;
dprintf(("=add_event(%p/%p): modify time", func, arg));
return 0;
}
}
/*
* Search for first empty or last event slot.
*/
for (ep = first_event; ep; ep = ep->next) {
if (!ep->func) {
next = ep->next;
break;
}
prev = ep;
}
if (!ep && (ep = (EVENT *)malloc(sizeof(EVENT))) == 0)
return -1;
memset(ep, 0, sizeof(EVENT));
ep->tv = *tvp;
ep->func = func;
ep->arg = arg;
if (next) ep->next = next;
else if (prev) prev->next = ep;
if (!first_event) first_event = ep;
#ifdef notdef
{
char at_time[50];
strftime(at_time, sizeof(at_time), "%T",
localtime((time_t *)&ep->tv.tv_sec));
dprintf(("+add_event(%p/%p): schedule=%s.%03d",
func, arg, at_time, (int)(tvp->tv_usec / 1000)));
}
#endif
return 0;
}
/*
* Remove system event from queue if any.
*/
int
remove_event(func, arg)
void (*func)(void *);
void *arg;
{
int found = 0;
EVENT *ep;
for (ep = first_event; ep; ep = ep->next) {
if ((!func || ep->func == func) && ep->arg == arg) {
ep->func = 0;
found++;
}
}
return found;
}
/*
* Modify existing system event in queue for the new function argument.
*/
int
change_event(func, arg, new_arg)
void (*func)(void *);
void *arg, *new_arg;
{
int found = 0;
EVENT *ep;
for (ep = first_event; ep; ep = ep->next) {
if ((!func || ep->func == func) && ep->arg == arg) {
ep->arg = new_arg;
found++;
}
}
return found;
}
EVENT *
find_event(func, arg)
void (*func)(void *);
void *arg;
{
EVENT *ep;
for (ep = first_event; ep; ep = ep->next) {
if (ep->func == func && ep->arg == arg)
return ep;
}
return 0;
}
void
free_events()
{
EVENT *ep, *next;
ep = first_event;
while (ep) {
next = ep->next;
free(ep);
ep = next;
}
first_event = 0;
}
|