tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
- iohandler_events(timer_priority, 0, 0);
- iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ if(timer_priority->constant_timeout) {
+ tdiff.tv_sec = 0;
+ iohandler_set_timeout(timer_priority, &tdiff);
+ iohandler_events(timer_priority, 0, 0);
+ } else {
+ iohandler_events(timer_priority, 0, 0);
+ iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ }
continue;
} else if(tdiff.tv_usec < 0) {
tdiff.tv_sec--;
tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
- iohandler_events(timer_priority, 0, 0);
- iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ if(timer_priority->constant_timeout) {
+ tdiff.tv_sec = 0;
+ iohandler_set_timeout(timer_priority, &tdiff);
+ iohandler_events(timer_priority, 0, 0);
+ } else {
+ iohandler_events(timer_priority, 0, 0);
+ iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ }
continue;
}
break;
tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
- iohandler_events(timer_priority, 0, 0);
- iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ if(timer_priority->constant_timeout) {
+ tdiff.tv_sec = 0;
+ iohandler_set_timeout(timer_priority, &tdiff);
+ iohandler_events(timer_priority, 0, 0);
+ } else {
+ iohandler_events(timer_priority, 0, 0);
+ iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ }
continue;
} else if(tdiff.tv_usec < 0) {
tdiff.tv_sec--;
tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
- iohandler_events(timer_priority, 0, 0);
- iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ if(timer_priority->constant_timeout) {
+ tdiff.tv_sec = 0;
+ iohandler_set_timeout(timer_priority, &tdiff);
+ iohandler_events(timer_priority, 0, 0);
+ } else {
+ iohandler_events(timer_priority, 0, 0);
+ iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ }
continue;
}
break;
tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
- iohandler_events(timer_priority, 0, 0);
- iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ if(timer_priority->constant_timeout) {
+ tdiff.tv_sec = 0;
+ iohandler_set_timeout(timer_priority, &tdiff);
+ iohandler_events(timer_priority, 0, 0);
+ } else {
+ iohandler_events(timer_priority, 0, 0);
+ iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ }
continue;
} else if(tdiff.tv_usec < 0) {
tdiff.tv_sec--;
engine_select_timer_delay_fix = ((engine_select_timer_delay_fix * 19) + timer_fix) / 20;
iohandler_log(IOLOG_DEBUG, "timer delay fix set to: %d us", engine_select_timer_delay_fix);
}
- iohandler_events(timer_priority, 0, 0);
- iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ if(timer_priority->constant_timeout) {
+ tdiff.tv_sec = 0;
+ iohandler_set_timeout(timer_priority, &tdiff);
+ iohandler_events(timer_priority, 0, 0);
+ } else {
+ iohandler_events(timer_priority, 0, 0);
+ iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ }
continue;
}
break;
tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec <= 0)) {
- iohandler_events(timer_priority, 0, 0);
- iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ if(timer_priority->constant_timeout) {
+ tdiff.tv_sec = 0;
+ iohandler_set_timeout(timer_priority, &tdiff);
+ iohandler_events(timer_priority, 0, 0);
+ } else {
+ iohandler_events(timer_priority, 0, 0);
+ iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ }
continue;
}
break;
while(timer_priority) {
tdiff.tv_sec = timer_priority->timeout.tv_sec - now.tv_sec;
tdiff.tv_usec = timer_priority->timeout.tv_usec - now.tv_usec;
- if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec < 1000)) {
- iohandler_events(timer_priority, 0, 0);
- iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ if(tdiff.tv_sec < 0 || (tdiff.tv_sec == 0 && tdiff.tv_usec < 100)) {
+ if(timer_priority->constant_timeout) {
+ tdiff.tv_sec = 0;
+ iohandler_set_timeout(timer_priority, &tdiff);
+ iohandler_events(timer_priority, 0, 0);
+ } else {
+ iohandler_events(timer_priority, 0, 0);
+ iohandler_close(timer_priority); //also sets timer_priority to the next timed element
+ }
continue;
} else if(tdiff.tv_usec < 0) {
tdiff.tv_sec--;
descriptor->next->prev = descriptor->prev;
if(descriptor == timer_priority)
timer_priority = descriptor->next;
- if(timeout) {
+ if(timeout && timeout->tv_sec == 0 && descriptor->constant_timeout) {
+ descriptor->timeout.tv_usec += (descriptor->constant_timeout % 1000) * 1000;
+ descriptor->timeout.tv_sec += (descriptor->constant_timeout / 1000);
+ if(descriptor->timeout.tv_usec > 1000000) {
+ descriptor->timeout.tv_sec += (descriptor->timeout.tv_usec / 1000000);
+ descriptor->timeout.tv_usec %= 1000000;
+ }
+ } else if(timeout) {
descriptor->timeout = *timeout;
if(descriptor->timeout.tv_usec > 1000000) {
descriptor->timeout.tv_usec -= 1000000;
return descriptor;
}
+struct IODescriptor *iohandler_constant_timer(int msec, iohandler_callback *callback) {
+ struct IODescriptor *descriptor;
+ struct timeval timeout;
+ gettimeofday(&timeout, NULL);
+ timeout.tv_usec += (msec % 1000) * 1000;
+ timeout.tv_sec += (msec / 1000);
+ if(timeout.tv_usec > 1000000) {
+ timeout.tv_sec += (timeout.tv_usec / 1000000);
+ timeout.tv_usec %= 1000000;
+ }
+ descriptor = iohandler_add(-1, IOTYPE_TIMER, &timeout, callback);
+ if(!descriptor) {
+ iohandler_log(IOLOG_ERROR, "could not allocate memory for IODescriptor in %s:%d", __FILE__, __LINE__);
+ return NULL;
+ }
+ descriptor->constant_timeout = msec;
+ iohandler_log(IOLOG_DEBUG, "added timer descriptor (sec: %d; usec: %d)", timeout.tv_sec, timeout.tv_usec);
+ return descriptor;
+}
+
struct IODescriptor *iohandler_connect(const char *hostname, unsigned int port, int ssl, const char *bindhost, iohandler_callback *callback) {
return iohandler_connect_flags(hostname, port, ssl, bindhost, callback, IOHANDLER_CONNECT_IPV4 | IOHANDLER_CONNECT_IPV6);
}
enum IOType type;
enum IOStatus state;
struct timeval timeout;
+ int constant_timeout;
iohandler_callback *callback;
struct IOBuffer readbuf;
struct IOBuffer writebuf;
struct IODescriptor *iohandler_add(int sockfd, enum IOType type, struct timeval *timeout, iohandler_callback *callback);
struct IODescriptor *iohandler_timer(struct timeval timeout, iohandler_callback *callback);
+struct IODescriptor *iohandler_constant_timer(int msec, iohandler_callback *callback);
struct IODescriptor *iohandler_connect(const char *hostname, unsigned int port, int ssl, const char *bind, iohandler_callback *callback);
struct IODescriptor *iohandler_connect_flags(const char *hostname, unsigned int port, int ssl, const char *bindhost, iohandler_callback *callback, int flags);
struct IODescriptor *iohandler_listen(const char *hostname, unsigned int port, iohandler_callback *callback);
static struct timeval test_clock1, test_clock2;
static int timercount;
-static void add_timer(int ms) {
+void add_timer(int ms) {
struct timeval timeout;
gettimeofday(&timeout, NULL);
timeout.tv_usec += (ms % 1000) * 1000;
gettimeofday(&test_clock1, NULL);
gettimeofday(&test_clock2, NULL);
- add_timer(TEST_DURATION);
+ //add_timer(TEST_DURATION);
+ iohandler_constant_timer(TEST_DURATION, io_callback);
timercount = 0;
printf("[timer 0] %ld.%ld\n", test_clock1.tv_sec, test_clock1.tv_usec);
double diff2;
switch(event->type) {
case IOEVENT_TIMEOUT:
- add_timer(TEST_DURATION);
+ //add_timer(TEST_DURATION);
timercount++;
gettimeofday(&curr_time, NULL);
diff1 = (curr_time.tv_sec - test_clock1.tv_sec) * 1000 + ((curr_time.tv_usec - test_clock1.tv_usec) / 1000);