-/* timeq.c - NeonServ v5.5
+/* timeq.c - NeonServ v5.6
* Copyright (C) 2011-2012 Philipp Kreil (pk910)
*
* This program is free software: you can redistribute it and/or modify
#include "timeq.h"
#include "IOHandler.h"
+#include "tools.h"
+#include "log.h"
static struct timeq_entry *timeq_events;
#ifdef HAVE_THREADS
switch(event->type) {
case IOEVENT_TIMEOUT:
entry->callback(entry->data);
+ entry->iofd = NULL;
timeq_del(entry);
break;
default:
struct timeq_entry *entry = malloc(sizeof(*entry));
if (!entry)
{
- perror("malloc() failed");
+ printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
return NULL;
}
+ #ifdef HAVE_THREADS
if(!pthread_mutex_initialized) {
THREAD_MUTEX_INIT(synchronized);
pthread_mutex_initialized = 1;
}
+ #endif
gettimeofday(&timeout, NULL);
SYNCHRONIZE(synchronized);
timeout.tv_usec += (useconds % 1000);
entry->name = NULL;
entry->next = timeq_events;
entry->prev = NULL;
- timeq_events->prev = entry;
+ if(timeq_events)
+ timeq_events->prev = entry;
timeq_events = entry;
DESYNCHRONIZE(synchronized);
return entry;
}
int timeq_del(struct timeq_entry* entry) {
+ #ifdef HAVE_THREADS
if(!pthread_mutex_initialized) return 0;
+ #endif
SYNCHRONIZE(synchronized);
if(entry->next)
entry->next->prev = entry->prev;
timeq_events = entry->next;
if(entry->name)
free(entry->name);
- iohandler_close(entry->iofd);
+ if(entry->iofd)
+ iohandler_close(entry->iofd);
free(entry);
DESYNCHRONIZE(synchronized);
return 1;