2 * Event loop - empty template (basic structure, but no OS specific operations)
3 * Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi>
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
19 void (*handler)(int sock, void *eloop_ctx, void *sock_ctx);
22 struct eloop_timeout {
26 void (*handler)(void *eloop_ctx, void *sock_ctx);
27 struct eloop_timeout *next;
33 void (*handler)(int sig, void *eloop_ctx, void *signal_ctx);
38 int max_sock, reader_count;
39 struct eloop_sock *readers;
41 struct eloop_timeout *timeout;
44 struct eloop_signal *signals;
46 int pending_terminate;
49 int reader_table_changed;
52 static struct eloop_data eloop;
57 memset(&eloop, 0, sizeof(eloop));
62 int eloop_register_read_sock(int sock,
63 void (*handler)(int sock, void *eloop_ctx,
65 void *eloop_data, void *user_data)
67 struct eloop_sock *tmp;
69 tmp = (struct eloop_sock *)
70 realloc(eloop.readers,
71 (eloop.reader_count + 1) * sizeof(struct eloop_sock));
75 tmp[eloop.reader_count].sock = sock;
76 tmp[eloop.reader_count].eloop_data = eloop_data;
77 tmp[eloop.reader_count].user_data = user_data;
78 tmp[eloop.reader_count].handler = handler;
81 if (sock > eloop.max_sock)
82 eloop.max_sock = sock;
83 eloop.reader_table_changed = 1;
89 void eloop_unregister_read_sock(int sock)
93 if (eloop.readers == NULL || eloop.reader_count == 0)
96 for (i = 0; i < eloop.reader_count; i++) {
97 if (eloop.readers[i].sock == sock)
100 if (i == eloop.reader_count)
102 if (i != eloop.reader_count - 1) {
103 memmove(&eloop.readers[i], &eloop.readers[i + 1],
104 (eloop.reader_count - i - 1) *
105 sizeof(struct eloop_sock));
107 eloop.reader_count--;
108 eloop.reader_table_changed = 1;
112 int eloop_register_timeout(unsigned int secs, unsigned int usecs,
113 void (*handler)(void *eloop_ctx, void *timeout_ctx),
114 void *eloop_data, void *user_data)
116 struct eloop_timeout *timeout, *tmp, *prev;
118 timeout = (struct eloop_timeout *) malloc(sizeof(*timeout));
121 os_get_time(&timeout->time);
122 timeout->time.sec += secs;
123 timeout->time.usec += usecs;
124 while (timeout->time.usec >= 1000000) {
126 timeout->time.usec -= 1000000;
128 timeout->eloop_data = eloop_data;
129 timeout->user_data = user_data;
130 timeout->handler = handler;
131 timeout->next = NULL;
133 if (eloop.timeout == NULL) {
134 eloop.timeout = timeout;
140 while (tmp != NULL) {
141 if (os_time_before(&timeout->time, &tmp->time))
148 timeout->next = eloop.timeout;
149 eloop.timeout = timeout;
151 timeout->next = prev->next;
152 prev->next = timeout;
159 int eloop_cancel_timeout(void (*handler)(void *eloop_ctx, void *sock_ctx),
160 void *eloop_data, void *user_data)
162 struct eloop_timeout *timeout, *prev, *next;
166 timeout = eloop.timeout;
167 while (timeout != NULL) {
168 next = timeout->next;
170 if (timeout->handler == handler &&
171 (timeout->eloop_data == eloop_data ||
172 eloop_data == ELOOP_ALL_CTX) &&
173 (timeout->user_data == user_data ||
174 user_data == ELOOP_ALL_CTX)) {
176 eloop.timeout = next;
191 int eloop_is_timeout_registered(void (*handler)(void *eloop_ctx,
193 void *eloop_data, void *user_data)
195 struct eloop_timeout *tmp;
198 while (tmp != NULL) {
199 if (tmp->handler == handler &&
200 tmp->eloop_data == eloop_data &&
201 tmp->user_data == user_data)
211 /* TODO: replace with suitable signal handler */
213 static void eloop_handle_signal(int sig)
218 for (i = 0; i < eloop.signal_count; i++) {
219 if (eloop.signals[i].sig == sig) {
220 eloop.signals[i].signaled++;
228 static void eloop_process_pending_signals(void)
232 if (eloop.signaled == 0)
236 if (eloop.pending_terminate) {
237 eloop.pending_terminate = 0;
240 for (i = 0; i < eloop.signal_count; i++) {
241 if (eloop.signals[i].signaled) {
242 eloop.signals[i].signaled = 0;
243 eloop.signals[i].handler(eloop.signals[i].sig,
245 eloop.signals[i].user_data);
251 int eloop_register_signal(int sig,
252 void (*handler)(int sig, void *eloop_ctx,
256 struct eloop_signal *tmp;
258 tmp = (struct eloop_signal *)
259 realloc(eloop.signals,
260 (eloop.signal_count + 1) *
261 sizeof(struct eloop_signal));
265 tmp[eloop.signal_count].sig = sig;
266 tmp[eloop.signal_count].user_data = user_data;
267 tmp[eloop.signal_count].handler = handler;
268 tmp[eloop.signal_count].signaled = 0;
269 eloop.signal_count++;
272 /* TODO: register signal handler */
278 int eloop_register_signal_terminate(void (*handler)(int sig, void *eloop_ctx,
283 /* TODO: for example */
284 int ret = eloop_register_signal(SIGINT, handler, user_data);
286 ret = eloop_register_signal(SIGTERM, handler, user_data);
293 int eloop_register_signal_reconfig(void (*handler)(int sig, void *eloop_ctx,
298 /* TODO: for example */
299 return eloop_register_signal(SIGHUP, handler, user_data);
308 struct os_time tv, now;
310 while (!eloop.terminate &&
311 (eloop.timeout || eloop.reader_count > 0)) {
314 if (os_time_before(&now, &eloop.timeout->time))
315 os_time_sub(&eloop.timeout->time, &now, &tv);
317 tv.sec = tv.usec = 0;
321 * TODO: wait for any event (read socket ready, timeout (tv),
324 os_sleep(1, 0); /* just a dummy wait for testing */
326 eloop_process_pending_signals();
328 /* check if some registered timeouts have occurred */
330 struct eloop_timeout *tmp;
333 if (!os_time_before(&now, &eloop.timeout->time)) {
335 eloop.timeout = eloop.timeout->next;
336 tmp->handler(tmp->eloop_data,
343 eloop.reader_table_changed = 0;
344 for (i = 0; i < eloop.reader_count; i++) {
346 * TODO: call each handler that has pending data to
349 if (0 /* TODO: eloop.readers[i].sock ready */) {
350 eloop.readers[i].handler(
351 eloop.readers[i].sock,
352 eloop.readers[i].eloop_data,
353 eloop.readers[i].user_data);
354 if (eloop.reader_table_changed)
362 void eloop_terminate(void)
368 void eloop_destroy(void)
370 struct eloop_timeout *timeout, *prev;
372 timeout = eloop.timeout;
373 while (timeout != NULL) {
375 timeout = timeout->next;
383 int eloop_terminated(void)
385 return eloop.terminate;
389 void eloop_wait_for_read_sock(int sock)
392 * TODO: wait for the file descriptor to have something available for