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