1 /* timeq.c - NeonServ v5.6
2 * Copyright (C) 2011-2012 Philipp Kreil (pk910)
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 #include "IOHandler.h"
23 static struct timeq_entry *timeq_events;
25 static int pthread_mutex_initialized = 0;
26 static pthread_mutex_t synchronized;
29 static IOHANDLER_CALLBACK(timeq_callback) {
30 struct timeq_entry *entry = event->iofd->data;
37 entry->callback(entry->data);
46 struct timeq_entry* timeq_add(int seconds, int module_id, timeq_callback_t *callback, void *data) {
47 return timeq_uadd(seconds * 1000, module_id, callback, data);
50 struct timeq_entry* timeq_uadd(int useconds, int module_id, timeq_callback_t *callback, void *data) {
51 struct timeval timeout;
52 struct timeq_entry *entry = malloc(sizeof(*entry));
55 printf_log("main", LOG_ERROR, "%s:%d malloc() failed", __FILE__, __LINE__);
59 if(!pthread_mutex_initialized) {
60 THREAD_MUTEX_INIT(synchronized);
61 pthread_mutex_initialized = 1;
64 gettimeofday(&timeout, NULL);
65 SYNCHRONIZE(synchronized);
66 timeout.tv_usec += (useconds % 1000);
67 timeout.tv_sec += (useconds / 1000);
68 entry->iofd = iohandler_timer(timeout, timeq_callback);
69 entry->iofd->data = entry;
70 entry->module_id = module_id;
71 entry->callback = callback;
74 entry->next = timeq_events;
77 timeq_events->prev = entry;
79 DESYNCHRONIZE(synchronized);
83 struct timeq_entry* timeq_add_name(char *name, int seconds, int module_id, timeq_callback_t *callback, void *data) {
84 return timeq_uadd_name(name, seconds * 1000, module_id, callback, data);
87 struct timeq_entry* timeq_uadd_name(char *name, int useconds, int module_id, timeq_callback_t *callback, void *data) {
88 struct timeq_entry *entry = timeq_uadd(useconds, module_id, callback, data);
89 entry->name = strdup(name);
93 int timeq_del(struct timeq_entry* entry) {
95 if(!pthread_mutex_initialized) return 0;
97 SYNCHRONIZE(synchronized);
99 entry->next->prev = entry->prev;
101 entry->prev->next = entry->next;
103 timeq_events = entry->next;
107 iohandler_close(entry->iofd);
109 DESYNCHRONIZE(synchronized);
113 int timeq_del_name(char *name) {
114 SYNCHRONIZE(synchronized);
115 struct timeq_entry *entry;
117 for(entry = timeq_events; entry; entry = entry->next) {
118 if(entry->name && !stricmp(entry->name, name)) {
119 removed = timeq_del(entry);
123 DESYNCHRONIZE(synchronized);
127 int timeq_name_exists(char *name) {
128 SYNCHRONIZE(synchronized);
129 struct timeq_entry *centry;
130 for(centry = timeq_events; centry; centry = centry->next) {
131 if(centry->name && !stricmp(centry->name, name)) {
132 DESYNCHRONIZE(synchronized);
136 DESYNCHRONIZE(synchronized);
140 void unregister_module_timers(int module_id) {
141 SYNCHRONIZE(synchronized);
142 struct timeq_entry *entry, *next_entry;
143 for(entry = timeq_events; entry; entry = next_entry) {
144 next_entry = entry->next;
145 if(entry->module_id == module_id)
148 DESYNCHRONIZE(synchronized);