eloop_none: Use struct dl_list for timeouts

This merges commit eaa3f04b97 changes from
eloop.c into eloop_none.c.

Signed-hostap: Jouni Malinen <j@w1.fi>
This commit is contained in:
Jouni Malinen 2013-02-03 16:45:05 +02:00
parent 5255c9ea0d
commit 5617809c93

View file

@ -1,6 +1,6 @@
/* /*
* Event loop - empty template (basic structure, but no OS specific operations) * Event loop - empty template (basic structure, but no OS specific operations)
* Copyright (c) 2002-2005, Jouni Malinen <j@w1.fi> * Copyright (c) 2002-2009, Jouni Malinen <j@w1.fi>
* *
* This software may be distributed under the terms of the BSD license. * This software may be distributed under the terms of the BSD license.
* See README for more details. * See README for more details.
@ -9,6 +9,7 @@
#include "includes.h" #include "includes.h"
#include "common.h" #include "common.h"
#include "list.h"
#include "eloop.h" #include "eloop.h"
@ -20,11 +21,11 @@ struct eloop_sock {
}; };
struct eloop_timeout { struct eloop_timeout {
struct dl_list list;
struct os_time time; struct os_time time;
void *eloop_data; void *eloop_data;
void *user_data; void *user_data;
eloop_timeout_handler handler; eloop_timeout_handler handler;
struct eloop_timeout *next;
}; };
struct eloop_signal { struct eloop_signal {
@ -38,7 +39,7 @@ struct eloop_data {
int max_sock, reader_count; int max_sock, reader_count;
struct eloop_sock *readers; struct eloop_sock *readers;
struct eloop_timeout *timeout; struct dl_list timeout;
int signal_count; int signal_count;
struct eloop_signal *signals; struct eloop_signal *signals;
@ -55,6 +56,7 @@ static struct eloop_data eloop;
int eloop_init(void) int eloop_init(void)
{ {
memset(&eloop, 0, sizeof(eloop)); memset(&eloop, 0, sizeof(eloop));
dl_list_init(&eloop.timeout);
return 0; return 0;
} }
@ -111,7 +113,7 @@ int eloop_register_timeout(unsigned int secs, unsigned int usecs,
eloop_timeout_handler handler, eloop_timeout_handler handler,
void *eloop_data, void *user_data) void *eloop_data, void *user_data)
{ {
struct eloop_timeout *timeout, *tmp, *prev; struct eloop_timeout *timeout, *tmp;
timeout = (struct eloop_timeout *) malloc(sizeof(*timeout)); timeout = (struct eloop_timeout *) malloc(sizeof(*timeout));
if (timeout == NULL) if (timeout == NULL)
@ -126,29 +128,15 @@ int eloop_register_timeout(unsigned int secs, unsigned int usecs,
timeout->eloop_data = eloop_data; timeout->eloop_data = eloop_data;
timeout->user_data = user_data; timeout->user_data = user_data;
timeout->handler = handler; timeout->handler = handler;
timeout->next = NULL;
if (eloop.timeout == NULL) { /* Maintain timeouts in order of increasing time */
eloop.timeout = timeout; dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
if (os_time_before(&timeout->time, &tmp->time)) {
dl_list_add(tmp->list.prev, &timeout->list);
return 0; return 0;
} }
prev = NULL;
tmp = eloop.timeout;
while (tmp != NULL) {
if (os_time_before(&timeout->time, &tmp->time))
break;
prev = tmp;
tmp = tmp->next;
}
if (prev == NULL) {
timeout->next = eloop.timeout;
eloop.timeout = timeout;
} else {
timeout->next = prev->next;
prev->next = timeout;
} }
dl_list_add_tail(&eloop.timeout, &timeout->list);
return 0; return 0;
} }
@ -157,29 +145,20 @@ int eloop_register_timeout(unsigned int secs, unsigned int usecs,
int eloop_cancel_timeout(eloop_timeout_handler handler, int eloop_cancel_timeout(eloop_timeout_handler handler,
void *eloop_data, void *user_data) void *eloop_data, void *user_data)
{ {
struct eloop_timeout *timeout, *prev, *next; struct eloop_timeout *timeout, *prev;
int removed = 0; int removed = 0;
prev = NULL; dl_list_for_each_safe(timeout, prev, &eloop.timeout,
timeout = eloop.timeout; struct eloop_timeout, list) {
while (timeout != NULL) {
next = timeout->next;
if (timeout->handler == handler && if (timeout->handler == handler &&
(timeout->eloop_data == eloop_data || (timeout->eloop_data == eloop_data ||
eloop_data == ELOOP_ALL_CTX) && eloop_data == ELOOP_ALL_CTX) &&
(timeout->user_data == user_data || (timeout->user_data == user_data ||
user_data == ELOOP_ALL_CTX)) { user_data == ELOOP_ALL_CTX)) {
if (prev == NULL) dl_list_del(&timeout->list);
eloop.timeout = next;
else
prev->next = next;
free(timeout); free(timeout);
removed++; removed++;
} else }
prev = timeout;
timeout = next;
} }
return removed; return removed;
@ -191,14 +170,11 @@ int eloop_is_timeout_registered(eloop_timeout_handler handler,
{ {
struct eloop_timeout *tmp; struct eloop_timeout *tmp;
tmp = eloop.timeout; dl_list_for_each(tmp, &eloop.timeout, struct eloop_timeout, list) {
while (tmp != NULL) {
if (tmp->handler == handler && if (tmp->handler == handler &&
tmp->eloop_data == eloop_data && tmp->eloop_data == eloop_data &&
tmp->user_data == user_data) tmp->user_data == user_data)
return 1; return 1;
tmp = tmp->next;
} }
return 0; return 0;
@ -300,11 +276,14 @@ void eloop_run(void)
struct os_time tv, now; struct os_time tv, now;
while (!eloop.terminate && while (!eloop.terminate &&
(eloop.timeout || eloop.reader_count > 0)) { (!dl_list_empty(&eloop.timeout) || eloop.reader_count > 0)) {
if (eloop.timeout) { struct eloop_timeout *timeout;
timeout = dl_list_first(&eloop.timeout, struct eloop_timeout,
list);
if (timeout) {
os_get_time(&now); os_get_time(&now);
if (os_time_before(&now, &eloop.timeout->time)) if (os_time_before(&now, &timeout->time))
os_time_sub(&eloop.timeout->time, &now, &tv); os_time_sub(&timeout->time, &now, &tv);
else else
tv.sec = tv.usec = 0; tv.sec = tv.usec = 0;
} }
@ -318,16 +297,15 @@ void eloop_run(void)
eloop_process_pending_signals(); eloop_process_pending_signals();
/* check if some registered timeouts have occurred */ /* check if some registered timeouts have occurred */
if (eloop.timeout) { timeout = dl_list_first(&eloop.timeout, struct eloop_timeout,
struct eloop_timeout *tmp; list);
if (timeout) {
os_get_time(&now); os_get_time(&now);
if (!os_time_before(&now, &eloop.timeout->time)) { if (!os_time_before(&now, &timeout->time)) {
tmp = eloop.timeout; dl_list_del(&timeout->list);
eloop.timeout = eloop.timeout->next; timeout->handler(timeout->eloop_data,
tmp->handler(tmp->eloop_data, timeout->user_data);
tmp->user_data); free(timeout);
free(tmp);
} }
} }
@ -361,11 +339,10 @@ void eloop_destroy(void)
{ {
struct eloop_timeout *timeout, *prev; struct eloop_timeout *timeout, *prev;
timeout = eloop.timeout; dl_list_for_each_safe(timeout, prev, &eloop.timeout,
while (timeout != NULL) { struct eloop_timeout, list) {
prev = timeout; dl_list_del(&timeout->list);
timeout = timeout->next; free(timeout);
free(prev);
} }
free(eloop.readers); free(eloop.readers);
free(eloop.signals); free(eloop.signals);