Poll as long as possible until next timeout the earliest.

This commit is contained in:
Jan Vidar Krey 2010-01-22 23:18:59 +01:00
parent 230ca28b46
commit 4aa65733d0
4 changed files with 27 additions and 26 deletions

View File

@ -95,6 +95,7 @@ int net_backend_initialize()
void net_backend_shutdown()
{
close(g_backend->epfd);
timeout_queue_shutdown(&g_backend->timeout_queue);
net_cleanup_shutdown(g_backend->cleaner);
hub_free(g_backend->conns);
hub_free(g_backend);
@ -106,8 +107,9 @@ void net_backend_shutdown()
int net_backend_process()
{
int n;
LOG_TRACE("epoll_wait: fd=%d, events=%x, max=%zu", g_backend->epfd, g_backend->events, MIN(g_backend->num, EPOLL_EVBUFFER));
int res = epoll_wait(g_backend->epfd, g_backend->events, MIN(g_backend->num, EPOLL_EVBUFFER), 1000);
size_t secs = timeout_queue_get_next_timeout(&g_backend->timeout_queue, g_backend->now);
LOG_TRACE("epoll_wait: fd=%d, events=%x, max=%zu, seconds=%d", g_backend->epfd, g_backend->events, MIN(g_backend->num, EPOLL_EVBUFFER), (int) secs);
int res = epoll_wait(g_backend->epfd, g_backend->events, MIN(g_backend->num, EPOLL_EVBUFFER), secs * 1000);
if (res == -1)
{
LOG_WARN("epoll_wait returned -1");
@ -188,28 +190,6 @@ void net_con_update(struct net_connection* con_, int events)
if (events & NET_EVENT_READ) con->ev.events |= EPOLLIN;
if (events & NET_EVENT_WRITE) con->ev.events |= EPOLLOUT;
#ifdef SSL_SUPPORT
if (events & NET_WANT_SSL_WRITE)
con->flags |= NET_WANT_SSL_WRITE;
else
con->flags &= ~NET_WANT_SSL_WRITE;
if (events & NET_WANT_SSL_READ)
con->flags |= NET_WANT_SSL_READ;
else
con->flags &= ~NET_WANT_SSL_READ;
if (events & NET_WANT_SSL_ACCEPT)
con->flags |= NET_WANT_SSL_ACCEPT;
else
con->flags &= ~NET_WANT_SSL_ACCEPT;
if (events & NET_WANT_SSL_CONNECT)
con->flags |= NET_WANT_SSL_CONNECT;
else
con->flags &= ~NET_WANT_SSL_CONNECT;
#endif /* SSL_SUPPORT */
if (epoll_ctl(g_backend->epfd, EPOLL_CTL_MOD, con->sd, &con->ev) == -1)
{
LOG_TRACE("epoll_ctl() modify failed.");

View File

@ -84,6 +84,7 @@ int net_backend_initialize()
*/
void net_backend_shutdown()
{
timeout_queue_shutdown(&g_backend->timeout_queue);
net_cleanup_shutdown(g_backend->cleaner);
hub_free(g_backend->conns);
hub_free(g_backend);
@ -95,9 +96,14 @@ void net_backend_shutdown()
int net_backend_process()
{
int n, found, maxfd;
struct timeval tval = { 1, 0 };
struct timeval tval;
FD_ZERO(&g_backend->rfds);
FD_ZERO(&g_backend->wfds);
size_t secs = timeout_queue_get_next_timeout(&g_backend->timeout_queue, g_backend->now);
tval.tv_secs = secs;
tval.tv_usecs = 0;
for (n = 0, found = 0; found < g_backend->num && n < g_backend->max; n++)
{
struct net_connection_select* con = g_backend->conns[n];

View File

@ -58,7 +58,7 @@ size_t timeout_queue_process(struct timeout_queue* t, time_t now)
size_t pos;
size_t events = 0;
struct timeout_evt* evt = 0;
for (pos = t->last; pos < now; pos++)
for (pos = t->last; pos <= now; pos++)
{
while ((evt = t->events[pos % t->max]))
{
@ -71,6 +71,19 @@ size_t timeout_queue_process(struct timeout_queue* t, time_t now)
return events;
}
size_t timeout_queue_get_next_timeout(struct timeout_queue* t, time_t now)
{
size_t seconds = 0;
while (t->events[(now + seconds) % t->max] == NULL && seconds < t->max)
{
seconds++;
}
if (seconds == 0)
return 1;
return seconds;
}
void timeout_queue_insert(struct timeout_queue* t, struct timeout_evt* evt, size_t seconds)
{
struct timeout_evt* first;

View File

@ -53,4 +53,6 @@ void timeout_queue_insert(struct timeout_queue*, struct timeout_evt*, size_t sec
void timeout_queue_remove(struct timeout_queue*, struct timeout_evt*);
void timeout_queue_reschedule(struct timeout_queue*, struct timeout_evt*, size_t seconds);
size_t timeout_queue_get_next_timeout(struct timeout_queue*, time_t now);
#endif /* HAVE_UHUB_TIMEOUT_HANDLER_H */