fixed some missing includes
[NeonServV5.git] / src / timeq.c
1 /* timeq.c - NeonServ v5.6
2  * Copyright (C) 2011-2012  Philipp Kreil (pk910)
3  * 
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.
8  * 
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.
13  * 
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/>. 
16  */
17
18 #include "timeq.h"
19 #include "IOHandler.h"
20 #include "tools.h"
21
22 static struct timeq_entry *timeq_events;
23 #ifdef HAVE_THREADS
24 static int pthread_mutex_initialized = 0;
25 static pthread_mutex_t synchronized;
26 #endif
27
28 static IOHANDLER_CALLBACK(timeq_callback) {
29     struct timeq_entry *entry = event->iofd->data;
30     switch(event->type) {
31     case IOEVENT_TIMEOUT:
32         entry->callback(entry->data);
33         entry->iofd = NULL;
34         timeq_del(entry);
35         break;
36     default:
37         break;
38     }
39 }
40
41 struct timeq_entry* timeq_add(int seconds, int module_id, timeq_callback_t *callback, void *data) {
42     return timeq_uadd(seconds * 1000, module_id, callback, data);
43 }
44
45 struct timeq_entry* timeq_uadd(int useconds, int module_id, timeq_callback_t *callback, void *data) {
46     struct timeval timeout;
47     struct timeq_entry *entry = malloc(sizeof(*entry));
48     if (!entry)
49     {
50         perror("malloc() failed");
51         return NULL;
52     }
53     #ifdef HAVE_THREADS
54     if(!pthread_mutex_initialized) {
55         THREAD_MUTEX_INIT(synchronized);
56         pthread_mutex_initialized = 1;
57     }
58     #endif
59     gettimeofday(&timeout, NULL);
60     SYNCHRONIZE(synchronized);
61     timeout.tv_usec += (useconds % 1000);
62     timeout.tv_sec += (useconds / 1000);
63     entry->iofd = iohandler_timer(timeout, timeq_callback);
64     entry->iofd->data = entry;
65     entry->module_id = module_id;
66     entry->callback = callback;
67     entry->data = data;
68     entry->name = NULL;
69     entry->next = timeq_events;
70     entry->prev = NULL;
71     if(timeq_events)
72         timeq_events->prev = entry;
73     timeq_events = entry;
74     DESYNCHRONIZE(synchronized);
75     return entry;
76 }
77
78 struct timeq_entry* timeq_add_name(char *name, int seconds, int module_id, timeq_callback_t *callback, void *data) {
79     return timeq_uadd_name(name, seconds * 1000, module_id, callback, data);
80 }
81
82 struct timeq_entry* timeq_uadd_name(char *name, int useconds, int module_id, timeq_callback_t *callback, void *data) {
83     struct timeq_entry *entry = timeq_uadd(useconds, module_id, callback, data);
84     entry->name = strdup(name);
85     return entry;
86 }
87
88 int timeq_del(struct timeq_entry* entry) {
89     #ifdef HAVE_THREADS
90     if(!pthread_mutex_initialized) return 0;
91     #endif
92     SYNCHRONIZE(synchronized);
93     if(entry->next)
94         entry->next->prev = entry->prev;
95     if(entry->prev)
96         entry->prev->next = entry->next;
97     else
98         timeq_events = entry->next;
99     if(entry->name)
100         free(entry->name);
101     if(entry->iofd)
102         iohandler_close(entry->iofd);
103     free(entry);
104     DESYNCHRONIZE(synchronized);
105     return 1;
106 }
107
108 int timeq_del_name(char *name) {
109     SYNCHRONIZE(synchronized);
110     struct timeq_entry *entry;
111     int removed = 0;
112     for(entry = timeq_events; entry; entry = entry->next) {
113         if(entry->name && !stricmp(entry->name, name)) {
114             removed = timeq_del(entry);
115             break;
116         }
117     }
118     DESYNCHRONIZE(synchronized);
119     return removed;
120 }
121
122 int timeq_name_exists(char *name) {
123     SYNCHRONIZE(synchronized);
124     struct timeq_entry *centry;
125     for(centry = timeq_events; centry; centry = centry->next) {
126         if(centry->name && !stricmp(centry->name, name)) {
127             DESYNCHRONIZE(synchronized);
128             return 1;
129         }
130     }
131     DESYNCHRONIZE(synchronized);
132     return 0;
133 }
134
135 void unregister_module_timers(int module_id) {
136     SYNCHRONIZE(synchronized);
137     struct timeq_entry *entry, *next_entry;
138     for(entry = timeq_events; entry; entry = next_entry) {
139         next_entry = entry->next;
140         if(entry->module_id == module_id)
141             timeq_del(entry);
142     }
143     DESYNCHRONIZE(synchronized);
144 }