moved timeq loop to the "main thread" and made it more precise
[NeonServV5.git] / src / timeq.c
1 /* timeq.c - NeonServ v5.3
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, timeq_callback_t *callback, void *data) {
48     return timeq_uadd(seconds * 1000, callback, data);
49 }
50
51 struct timeq_entry* timeq_uadd(int useconds, 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->callback = callback;
65     entry->data = data;
66     entry->name = NULL;
67     struct timeq_entry *next, *prev = NULL;
68     for(next = timeq_events; next; next = next->next) {
69         if(timeval_is_bigger(next->execute, now))
70             break;
71         else
72             prev = next;
73     }
74     if(prev == NULL) {
75         entry->next = timeq_events;
76         timeq_events = entry;
77     } else {
78         entry->next = next;
79         prev->next = entry;
80     }
81     DESYNCHRONIZE(synchronized);
82     return entry;
83 }
84
85 struct timeq_entry* timeq_add_name(char *name, int seconds, timeq_callback_t *callback, void *data) {
86     return timeq_uadd_name(name, seconds * 1000, callback, data);
87 }
88
89 struct timeq_entry* timeq_uadd_name(char *name, int useconds, timeq_callback_t *callback, void *data) {
90     SYNCHRONIZE(synchronized);
91     struct timeq_entry *entry = timeq_uadd(useconds, callback, data);
92     entry->name = strdup(name);
93     DESYNCHRONIZE(synchronized);
94     return entry;
95 }
96
97 int timeq_del(struct timeq_entry* entry) {
98     SYNCHRONIZE(synchronized);
99     struct timeq_entry *centry, *last = NULL;
100     for(centry = timeq_events; centry; centry = centry->next) {
101         if(centry == entry) {
102             if(last)
103                 last->next = centry->next;
104             else
105                 timeq_events = centry->next;
106             if(centry->name)
107                 free(centry->name);
108             free(centry);
109             DESYNCHRONIZE(synchronized);
110             return 1;
111         } else {
112             last = centry;
113         }
114     }
115     DESYNCHRONIZE(synchronized);
116     return 0;
117 }
118
119 int timeq_del_name(char *name) {
120     SYNCHRONIZE(synchronized);
121     struct timeq_entry *centry, *last = NULL;
122     for(centry = timeq_events; centry; centry = centry->next) {
123         if(centry->name && !stricmp(centry->name, name)) {
124             if(last)
125                 last->next = centry->next;
126             else
127                 timeq_events = centry->next;
128             free(centry->name);
129             free(centry);
130             DESYNCHRONIZE(synchronized);
131             return 1;
132         } else {
133             last = centry;
134         }
135     }
136     DESYNCHRONIZE(synchronized);
137     return 0;
138 }
139
140 int timeq_name_exists(char *name) {
141     SYNCHRONIZE(synchronized);
142     struct timeq_entry *centry;
143     for(centry = timeq_events; centry; centry = centry->next) {
144         if(centry->name && !stricmp(centry->name, name)) {
145             DESYNCHRONIZE(synchronized);
146             return 1;
147         }
148     }
149     DESYNCHRONIZE(synchronized);
150     return 0;
151 }