00001
00016 #include "includes.h"
00017
00018 #include "common.h"
00019 #include "eloop.h"
00020
00021
00022 struct eloop_sock {
00023 int sock;
00024 void *eloop_data;
00025 void *user_data;
00026 void (*handler)(int sock, void *eloop_ctx, void *sock_ctx);
00027 };
00028
00029 struct eloop_timeout {
00030 struct os_time time;
00031 void *eloop_data;
00032 void *user_data;
00033 void (*handler)(void *eloop_ctx, void *sock_ctx);
00034 struct eloop_timeout *next;
00035 };
00036
00037 struct eloop_signal {
00038 int sig;
00039 void *user_data;
00040 void (*handler)(int sig, void *eloop_ctx, void *signal_ctx);
00041 int signaled;
00042 };
00043
00044 struct eloop_data {
00045 void *user_data;
00046
00047 int max_sock, reader_count;
00048 struct eloop_sock *readers;
00049
00050 struct eloop_timeout *timeout;
00051
00052 int signal_count;
00053 struct eloop_signal *signals;
00054 int signaled;
00055 int pending_terminate;
00056
00057 int terminate;
00058 int reader_table_changed;
00059 };
00060
00061 static struct eloop_data eloop;
00062
00063
00064 int eloop_init(void *user_data)
00065 {
00066 memset(&eloop, 0, sizeof(eloop));
00067 eloop.user_data = user_data;
00068 return 0;
00069 }
00070
00071
00072 int eloop_register_read_sock(int sock,
00073 void (*handler)(int sock, void *eloop_ctx,
00074 void *sock_ctx),
00075 void *eloop_data, void *user_data)
00076 {
00077 struct eloop_sock *tmp;
00078
00079 tmp = (struct eloop_sock *)
00080 realloc(eloop.readers,
00081 (eloop.reader_count + 1) * sizeof(struct eloop_sock));
00082 if (tmp == NULL)
00083 return -1;
00084
00085 tmp[eloop.reader_count].sock = sock;
00086 tmp[eloop.reader_count].eloop_data = eloop_data;
00087 tmp[eloop.reader_count].user_data = user_data;
00088 tmp[eloop.reader_count].handler = handler;
00089 eloop.reader_count++;
00090 eloop.readers = tmp;
00091 if (sock > eloop.max_sock)
00092 eloop.max_sock = sock;
00093 eloop.reader_table_changed = 1;
00094
00095 return 0;
00096 }
00097
00098
00099 void eloop_unregister_read_sock(int sock)
00100 {
00101 int i;
00102
00103 if (eloop.readers == NULL || eloop.reader_count == 0)
00104 return;
00105
00106 for (i = 0; i < eloop.reader_count; i++) {
00107 if (eloop.readers[i].sock == sock)
00108 break;
00109 }
00110 if (i == eloop.reader_count)
00111 return;
00112 if (i != eloop.reader_count - 1) {
00113 memmove(&eloop.readers[i], &eloop.readers[i + 1],
00114 (eloop.reader_count - i - 1) *
00115 sizeof(struct eloop_sock));
00116 }
00117 eloop.reader_count--;
00118 eloop.reader_table_changed = 1;
00119 }
00120
00121
00122 int eloop_register_timeout(unsigned int secs, unsigned int usecs,
00123 void (*handler)(void *eloop_ctx, void *timeout_ctx),
00124 void *eloop_data, void *user_data)
00125 {
00126 struct eloop_timeout *timeout, *tmp, *prev;
00127
00128 timeout = (struct eloop_timeout *) malloc(sizeof(*timeout));
00129 if (timeout == NULL)
00130 return -1;
00131 os_get_time(&timeout->time);
00132 timeout->time.sec += secs;
00133 timeout->time.usec += usecs;
00134 while (timeout->time.usec >= 1000000) {
00135 timeout->time.sec++;
00136 timeout->time.usec -= 1000000;
00137 }
00138 timeout->eloop_data = eloop_data;
00139 timeout->user_data = user_data;
00140 timeout->handler = handler;
00141 timeout->next = NULL;
00142
00143 if (eloop.timeout == NULL) {
00144 eloop.timeout = timeout;
00145 return 0;
00146 }
00147
00148 prev = NULL;
00149 tmp = eloop.timeout;
00150 while (tmp != NULL) {
00151 if (os_time_before(&timeout->time, &tmp->time))
00152 break;
00153 prev = tmp;
00154 tmp = tmp->next;
00155 }
00156
00157 if (prev == NULL) {
00158 timeout->next = eloop.timeout;
00159 eloop.timeout = timeout;
00160 } else {
00161 timeout->next = prev->next;
00162 prev->next = timeout;
00163 }
00164
00165 return 0;
00166 }
00167
00168
00169 int eloop_cancel_timeout(void (*handler)(void *eloop_ctx, void *sock_ctx),
00170 void *eloop_data, void *user_data)
00171 {
00172 struct eloop_timeout *timeout, *prev, *next;
00173 int removed = 0;
00174
00175 prev = NULL;
00176 timeout = eloop.timeout;
00177 while (timeout != NULL) {
00178 next = timeout->next;
00179
00180 if (timeout->handler == handler &&
00181 (timeout->eloop_data == eloop_data ||
00182 eloop_data == ELOOP_ALL_CTX) &&
00183 (timeout->user_data == user_data ||
00184 user_data == ELOOP_ALL_CTX)) {
00185 if (prev == NULL)
00186 eloop.timeout = next;
00187 else
00188 prev->next = next;
00189 free(timeout);
00190 removed++;
00191 } else
00192 prev = timeout;
00193
00194 timeout = next;
00195 }
00196
00197 return removed;
00198 }
00199
00200
00201 int eloop_is_timeout_registered(void (*handler)(void *eloop_ctx,
00202 void *timeout_ctx),
00203 void *eloop_data, void *user_data)
00204 {
00205 struct eloop_timeout *tmp;
00206
00207 tmp = eloop.timeout;
00208 while (tmp != NULL) {
00209 if (tmp->handler == handler &&
00210 tmp->eloop_data == eloop_data &&
00211 tmp->user_data == user_data)
00212 return 1;
00213
00214 tmp = tmp->next;
00215 }
00216
00217 return 0;
00218 }
00219
00220
00221
00222 #if 0
00223 static void eloop_handle_signal(int sig)
00224 {
00225 int i;
00226
00227 eloop.signaled++;
00228 for (i = 0; i < eloop.signal_count; i++) {
00229 if (eloop.signals[i].sig == sig) {
00230 eloop.signals[i].signaled++;
00231 break;
00232 }
00233 }
00234 }
00235 #endif
00236
00237
00238 static void eloop_process_pending_signals(void)
00239 {
00240 int i;
00241
00242 if (eloop.signaled == 0)
00243 return;
00244 eloop.signaled = 0;
00245
00246 if (eloop.pending_terminate) {
00247 eloop.pending_terminate = 0;
00248 }
00249
00250 for (i = 0; i < eloop.signal_count; i++) {
00251 if (eloop.signals[i].signaled) {
00252 eloop.signals[i].signaled = 0;
00253 eloop.signals[i].handler(eloop.signals[i].sig,
00254 eloop.user_data,
00255 eloop.signals[i].user_data);
00256 }
00257 }
00258 }
00259
00260
00261 int eloop_register_signal(int sig,
00262 void (*handler)(int sig, void *eloop_ctx,
00263 void *signal_ctx),
00264 void *user_data)
00265 {
00266 struct eloop_signal *tmp;
00267
00268 tmp = (struct eloop_signal *)
00269 realloc(eloop.signals,
00270 (eloop.signal_count + 1) *
00271 sizeof(struct eloop_signal));
00272 if (tmp == NULL)
00273 return -1;
00274
00275 tmp[eloop.signal_count].sig = sig;
00276 tmp[eloop.signal_count].user_data = user_data;
00277 tmp[eloop.signal_count].handler = handler;
00278 tmp[eloop.signal_count].signaled = 0;
00279 eloop.signal_count++;
00280 eloop.signals = tmp;
00281
00282
00283
00284 return 0;
00285 }
00286
00287
00288 int eloop_register_signal_terminate(void (*handler)(int sig, void *eloop_ctx,
00289 void *signal_ctx),
00290 void *user_data)
00291 {
00292 #if 0
00293
00294 int ret = eloop_register_signal(SIGINT, handler, user_data);
00295 if (ret == 0)
00296 ret = eloop_register_signal(SIGTERM, handler, user_data);
00297 return ret;
00298 #endif
00299 return 0;
00300 }
00301
00302
00303 int eloop_register_signal_reconfig(void (*handler)(int sig, void *eloop_ctx,
00304 void *signal_ctx),
00305 void *user_data)
00306 {
00307 #if 0
00308
00309 return eloop_register_signal(SIGHUP, handler, user_data);
00310 #endif
00311 return 0;
00312 }
00313
00314
00315 void eloop_run(void)
00316 {
00317 int i;
00318 struct os_time tv, now;
00319
00320 while (!eloop.terminate &&
00321 (eloop.timeout || eloop.reader_count > 0)) {
00322 if (eloop.timeout) {
00323 os_get_time(&now);
00324 if (os_time_before(&now, &eloop.timeout->time))
00325 os_time_sub(&eloop.timeout->time, &now, &tv);
00326 else
00327 tv.sec = tv.usec = 0;
00328 }
00329
00330
00331
00332
00333
00334 os_sleep(1, 0);
00335
00336 eloop_process_pending_signals();
00337
00338
00339 if (eloop.timeout) {
00340 struct eloop_timeout *tmp;
00341
00342 os_get_time(&now);
00343 if (!os_time_before(&now, &eloop.timeout->time)) {
00344 tmp = eloop.timeout;
00345 eloop.timeout = eloop.timeout->next;
00346 tmp->handler(tmp->eloop_data,
00347 tmp->user_data);
00348 free(tmp);
00349 }
00350
00351 }
00352
00353 eloop.reader_table_changed = 0;
00354 for (i = 0; i < eloop.reader_count; i++) {
00355
00356
00357
00358
00359 if (0 ) {
00360 eloop.readers[i].handler(
00361 eloop.readers[i].sock,
00362 eloop.readers[i].eloop_data,
00363 eloop.readers[i].user_data);
00364 if (eloop.reader_table_changed)
00365 break;
00366 }
00367 }
00368 }
00369 }
00370
00371
00372 void eloop_terminate(void)
00373 {
00374 eloop.terminate = 1;
00375 }
00376
00377
00378 void eloop_destroy(void)
00379 {
00380 struct eloop_timeout *timeout, *prev;
00381
00382 timeout = eloop.timeout;
00383 while (timeout != NULL) {
00384 prev = timeout;
00385 timeout = timeout->next;
00386 free(prev);
00387 }
00388 free(eloop.readers);
00389 free(eloop.signals);
00390 }
00391
00392
00393 int eloop_terminated(void)
00394 {
00395 return eloop.terminate;
00396 }
00397
00398
00399 void eloop_wait_for_read_sock(int sock)
00400 {
00401
00402
00403
00404
00405 }
00406
00407
00408 void * eloop_get_user_data(void)
00409 {
00410 return eloop.user_data;
00411 }
00412