/*
* Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
* Copyright (c) 1995-1999 by Internet Software Consortium
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
* OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/* ev_timers.c - implement timers for the eventlib
* vix 09sep95 [initial]
*/
if (ctx->cur != NULL &&
ctx->cur->type == Timer &&
ctx->cur->u.timer.this == del) {
evPrintf(ctx, 8, "deferring delete of timer (executing)\n");
/*
* Setting the interval to zero ensures that evDrop() will
* clean up the timer.
*/
del->inter = evConsTime(0, 0);
return (0);
}
if (heap_element(ctx->timers, del->index) != del)
EV_ERR(ENOENT);
if (heap_delete(ctx->timers, del->index) < 0)
return (-1);
FREE(del);
if (ctx->debug > 7) {
evPrintf(ctx, 7, "timers after evClearTimer:\n");
(void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
}
return (0);
}
int
evConfigTimer(evContext opaqueCtx,
evTimerID id,
const char *param,
int value
) {
evContext_p *ctx = opaqueCtx.opaque;
evTimer *timer = id.opaque;
int result=0;
UNUSED(value);
if (heap_element(ctx->timers, timer->index) != timer)
EV_ERR(ENOENT);
if (strcmp(param, "rate") == 0)
timer->mode |= EV_TMR_RATE;
else if (strcmp(param, "interval") == 0)
timer->mode &= ~EV_TMR_RATE;
else
EV_ERR(EINVAL);
return (result);
}
int
evResetTimer(evContext opaqueCtx,
evTimerID id,
evTimerFunc func,
void *uap,
struct timespec due,
struct timespec inter
) {
evContext_p *ctx = opaqueCtx.opaque;
evTimer *timer = id.opaque;
struct timespec old_due;
int result=0;
if (heap_element(ctx->timers, timer->index) != timer)
EV_ERR(ENOENT);
#ifdef __hpux
/*
* tv_sec and tv_nsec are unsigned.
*/
if (due.tv_nsec >= BILLION)
EV_ERR(EINVAL);
if (inter.tv_nsec >= BILLION)
EV_ERR(EINVAL);
#else
if (due.tv_sec < 0 || due.tv_nsec < 0 || due.tv_nsec >= BILLION)
EV_ERR(EINVAL);
switch (evCmpTime(due, old_due)) {
case -1:
result = heap_increased(ctx->timers, timer->index);
break;
case 0:
result = 0;
break;
case 1:
result = heap_decreased(ctx->timers, timer->index);
break;
}
if (ctx->debug > 7) {
evPrintf(ctx, 7, "timers after evResetTimer:\n");
(void) heap_for_each(ctx->timers, print_timer, (void *)ctx);
}
idle = evSubTime(ctx->lastEventTime, this->lastTouched);
if (evCmpTime(idle, this->max_idle) >= 0) {
(this->func)(opaqueCtx, this->uap, this->timer->due,
this->max_idle);
/*
* Setting the interval to zero will cause the timer to
* be cleaned up in evDrop().
*/
this->timer->inter = evConsTime(0, 0);
FREE(this);
} else {
/* evDrop() will reschedule the timer. */
this->timer->inter = evSubTime(this->max_idle, idle);
}
}