*** VERSION 5.4.0 ***
[NeonServV5.git] / src / timeq.c
1 /* timeq.c - NeonServ v5.4
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
20 static struct timeq_entry *timeq_events;
21 #ifdef HAVE_THREADS
22 static pthread_mutex_t synchronized;
23 #endif
24
25 void init_timeq() {
26     THREAD_MUTEX_INIT(synchronized);
27 }
28
29 void timeq_tick() {
30     SYNCHRONIZE(synchronized);
31     struct timeq_entry *entry, *next;
32     struct timeval now;
33     gettimeofday(&now, NULL);
34     for(entry = timeq_events; entry; entry = next) {
35         if(!timeval_is_bigger(entry->execute, now)) {
36             next = entry->next;
37             if(timeq_events == entry)
38                 timeq_events = next;
39             entry->callback(entry->data);
40             free(entry);
41         } else
42             break;
43     }
44     DESYNCHRONIZE(synchronized);
45 }
46
47 struct timeq_entry* timeq_add(int seconds, int module_id, timeq_callback_t *callback, void *data) {
48     return timeq_uadd(seconds * 1000, module_id, callback, data);
49 }
50
51 struct timeq_entry* timeq_uadd(int useconds, int module_id, timeq_callback_t *callback, void *data) {
52     struct timeval now;
53     gettimeofday(&now, NULL);
54     struct timeq_entry *entry = malloc(sizeof(*entry));
55     if (!entry)
56     {
57         perror("malloc() failed");
58         return NULL;
59     }
60     SYNCHRONIZE(synchronized);
61     now.tv_usec += (useconds % 1000);
62     now.tv_sec += (useconds / 1000);
63     entry->execute = now;
64     entry->module_id = module_id;
65     entry->callback = callback;
66     entry->data = data;
67     entry->name = NULL;
68     struct timeq_entry *next, *prev = NULL;
69     for(next = timeq_events; next; next = next->next) {
70         if(timeval_is_bigger(next->execute, now))
71             break;
72         else
73             prev = next;
74     }
75     if(prev == NULL) {
76         entry->next = timeq_events;
77         timeq_events = entry;
78     } else {
79         entry->next = next;
80         prev->next = entry;
81     }
82     DESYNCHRONIZE(synchronized);
83     return entry;
84 }
85
86 struct timeq_entry* timeq_add_name(char *name, int seconds, int module_id, timeq_callback_t *callback, void *data) {
87     return timeq_uadd_name(name, seconds * 1000, module_id, callback, data);
88 }
89
90 struct timeq_entry* timeq_uadd_name(char *name, int useconds, int module_id, timeq_callback_t *callback, void *data) {
91     SYNCHRONIZE(synchronized);
92     struct timeq_entry *entry = timeq_uadd(useconds, module_id, callback, data);
93     entry->name = strdup(name);
94     DESYNCHRONIZE(synchronized);
95     return entry;
96 }
97
98 int timeq_del(struct timeq_entry* entry) {
99     SYNCHRONIZE(synchronized);
100     struct timeq_entry *centry, *last = NULL;
101     for(centry = timeq_events; centry; centry = centry->next) {
102         if(centry == entry) {
103             if(last)
104                 last->next = centry->next;
105             else
106                 timeq_events = centry->next;
107             if(centry->name)
108                 free(centry->name);
109             free(centry);
110             DESYNCHRONIZE(synchronized);
111             return 1;
112         } else {
113             last = centry;
114         }
115     }
116     DESYNCHRONIZE(synchronized);
117     return 0;
118 }
119
120 int timeq_del_name(char *name) {
121     SYNCHRONIZE(synchronized);
122     struct timeq_entry *centry, *last = NULL;
123     for(centry = timeq_events; centry; centry = centry->next) {
124         if(centry->name && !stricmp(centry->name, name)) {
125             if(last)
126                 last->next = centry->next;
127             else
128                 timeq_events = centry->next;
129             free(centry->name);
130             free(centry);
131             DESYNCHRONIZE(synchronized);
132             return 1;
133         } else {
134             last = centry;
135         }
136     }
137     DESYNCHRONIZE(synchronized);
138     return 0;
139 }
140
141 int timeq_name_exists(char *name) {
142     SYNCHRONIZE(synchronized);
143     struct timeq_entry *centry;
144     for(centry = timeq_events; centry; centry = centry->next) {
145         if(centry->name && !stricmp(centry->name, name)) {
146             DESYNCHRONIZE(synchronized);
147             return 1;
148         }
149     }
150     DESYNCHRONIZE(synchronized);
151     return 0;
152 }
153
154 void unregister_module_timers(int module_id) {
155     SYNCHRONIZE(synchronized);
156     struct timeq_entry *centry, *next, *last = NULL;
157     for(centry = timeq_events; centry; centry = next) {
158         next = centry->next;
159         if(centry->module_id == module_id) {
160             if(last)
161                 last->next = centry->next;
162             else
163                 timeq_events = centry->next;
164             free(centry->name);
165             free(centry);
166         } else
167             last = centry;
168     }
169     DESYNCHRONIZE(synchronized);
170 }