Merge branch 'master' of git://git.pk910.de/IOMultiplexer into IOMultiplexer
[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         timeq_del(entry);
33         break;
34     default:
35         break;
36     }
37 }
38
39 struct timeq_entry* timeq_add(int seconds, int module_id, timeq_callback_t *callback, void *data) {
40     return timeq_uadd(seconds * 1000, module_id, callback, data);
41 }
42
43 struct timeq_entry* timeq_uadd(int useconds, int module_id, timeq_callback_t *callback, void *data) {
44     struct timeval timeout;
45     struct timeq_entry *entry = malloc(sizeof(*entry));
46     if (!entry)
47     {
48         perror("malloc() failed");
49         return NULL;
50     }
51     if(!pthread_mutex_initialized) {
52         THREAD_MUTEX_INIT(synchronized);
53         pthread_mutex_initialized = 1;
54     }
55     gettimeofday(&timeout, NULL);
56     SYNCHRONIZE(synchronized);
57     timeout.tv_usec += (useconds % 1000);
58     timeout.tv_sec += (useconds / 1000);
59     entry->iofd = iohandler_timer(timeout, timeq_callback);
60     entry->iofd->data = entry;
61     entry->module_id = module_id;
62     entry->callback = callback;
63     entry->data = data;
64     entry->name = NULL;
65     entry->next = timeq_events;
66     entry->prev = NULL;
67     if(timeq_events)
68         timeq_events->prev = entry;
69     timeq_events = entry;
70     DESYNCHRONIZE(synchronized);
71     return entry;
72 }
73
74 struct timeq_entry* timeq_add_name(char *name, int seconds, int module_id, timeq_callback_t *callback, void *data) {
75     return timeq_uadd_name(name, seconds * 1000, module_id, callback, data);
76 }
77
78 struct timeq_entry* timeq_uadd_name(char *name, int useconds, int module_id, timeq_callback_t *callback, void *data) {
79     struct timeq_entry *entry = timeq_uadd(useconds, module_id, callback, data);
80     entry->name = strdup(name);
81     return entry;
82 }
83
84 int timeq_del(struct timeq_entry* entry) {
85     if(!pthread_mutex_initialized) return 0;
86     SYNCHRONIZE(synchronized);
87     if(entry->next)
88         entry->next->prev = entry->prev;
89     if(entry->prev)
90         entry->prev->next = entry->next;
91     else
92         timeq_events = entry->next;
93     if(entry->name)
94         free(entry->name);
95     iohandler_close(entry->iofd);
96     free(entry);
97     DESYNCHRONIZE(synchronized);
98     return 1;
99 }
100
101 int timeq_del_name(char *name) {
102     SYNCHRONIZE(synchronized);
103     struct timeq_entry *entry;
104     int removed = 0;
105     for(entry = timeq_events; entry; entry = entry->next) {
106         if(entry->name && !stricmp(entry->name, name)) {
107             removed = timeq_del(entry);
108             break;
109         }
110     }
111     DESYNCHRONIZE(synchronized);
112     return removed;
113 }
114
115 int timeq_name_exists(char *name) {
116     SYNCHRONIZE(synchronized);
117     struct timeq_entry *centry;
118     for(centry = timeq_events; centry; centry = centry->next) {
119         if(centry->name && !stricmp(centry->name, name)) {
120             DESYNCHRONIZE(synchronized);
121             return 1;
122         }
123     }
124     DESYNCHRONIZE(synchronized);
125     return 0;
126 }
127
128 void unregister_module_timers(int module_id) {
129     SYNCHRONIZE(synchronized);
130     struct timeq_entry *entry, *next_entry;
131     for(entry = timeq_events; entry; entry = next_entry) {
132         next_entry = entry->next;
133         if(entry->module_id == module_id)
134             timeq_del(entry);
135     }
136     DESYNCHRONIZE(synchronized);
137 }