X-Git-Url: http://git.pk910.de/?a=blobdiff_plain;f=src%2Ftimeq.c;h=17b190488381576cfa9837b7c24709061258dc0c;hb=55831bf424312a6908ca07a904f288fba0919a9a;hp=41ed8a0e5b5153e31d018c6276df1ac0778864b2;hpb=a6c87d63788efe5f3a015d388bca0d788e6807f9;p=NeonServV5.git diff --git a/src/timeq.c b/src/timeq.c index 41ed8a0..17b1904 100644 --- a/src/timeq.c +++ b/src/timeq.c @@ -18,8 +18,16 @@ #include "timeq.h" static struct timeq_entry *timeq_events; +#ifdef HAVE_THREADS +static pthread_mutex_t synchronized; +#endif + +void init_timeq() { + THREAD_MUTEX_INIT(synchronized); +} void timeq_tick() { + SYNCHRONIZE(synchronized); struct timeq_entry *entry, *next; time_t now = time(0); for(entry = timeq_events; entry; entry = next) { @@ -32,6 +40,7 @@ void timeq_tick() { } else break; } + DESYNCHRONIZE(synchronized); } struct timeq_entry* timeq_add(int seconds, timeq_callback_t *callback, void *data) { @@ -42,6 +51,7 @@ struct timeq_entry* timeq_add(int seconds, timeq_callback_t *callback, void *dat perror("malloc() failed"); return NULL; } + SYNCHRONIZE(synchronized); entry->execute = now + seconds; entry->callback = callback; entry->data = data; @@ -60,16 +70,20 @@ struct timeq_entry* timeq_add(int seconds, timeq_callback_t *callback, void *dat entry->next = next; prev->next = entry; } + DESYNCHRONIZE(synchronized); return entry; } struct timeq_entry* timeq_add_name(char *name, int seconds, timeq_callback_t *callback, void *data) { + SYNCHRONIZE(synchronized); struct timeq_entry *entry = timeq_add(seconds, callback, data); entry->name = strdup(name); + DESYNCHRONIZE(synchronized); return entry; } int timeq_del(struct timeq_entry* entry) { + SYNCHRONIZE(synchronized); struct timeq_entry *centry, *last = NULL; for(centry = timeq_events; centry; centry = centry->next) { if(centry == entry) { @@ -80,15 +94,18 @@ int timeq_del(struct timeq_entry* entry) { if(centry->name) free(centry->name); free(centry); + DESYNCHRONIZE(synchronized); return 1; } else { last = centry; } } + DESYNCHRONIZE(synchronized); return 0; } int timeq_del_name(char *name) { + SYNCHRONIZE(synchronized); struct timeq_entry *centry, *last = NULL; for(centry = timeq_events; centry; centry = centry->next) { if(centry->name && !stricmp(centry->name, name)) { @@ -98,20 +115,25 @@ int timeq_del_name(char *name) { timeq_events = centry->next; free(centry->name); free(centry); + DESYNCHRONIZE(synchronized); return 1; } else { last = centry; } } + DESYNCHRONIZE(synchronized); return 0; } int timeq_name_exists(char *name) { + SYNCHRONIZE(synchronized); struct timeq_entry *centry; for(centry = timeq_events; centry; centry = centry->next) { if(centry->name && !stricmp(centry->name, name)) { + DESYNCHRONIZE(synchronized); return 1; } } + DESYNCHRONIZE(synchronized); return 0; }