Clean up THTTPD watch handling

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@2036 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo 2009-09-11 18:21:57 +00:00
parent c2b1e91c14
commit c8dec461fc
10 changed files with 154 additions and 131 deletions

View File

@ -297,7 +297,7 @@ int net_close(int sockfd)
return OK; return OK;
errout: errout:
*get_errno_ptr() = err; errno = err;
return ERROR; return ERROR;
} }

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* net/netdev_findbyaddr.c * net/netdev_findbyaddr.c
* *
* Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* net/netdev_txnotify.c * net/netdev_txnotify.c
* *
* Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* *
* Redistribution and use in source and binary forms, with or without * Redistribution and use in source and binary forms, with or without

View File

@ -1,7 +1,7 @@
/**************************************************************************** /****************************************************************************
* net/uip/uip_listen.c * net/uip/uip_listen.c
* *
* Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* *
* A direct leverage of logic from uIP which also has b BSD style license * A direct leverage of logic from uIP which also has b BSD style license

View File

@ -347,7 +347,7 @@ void uip_tcpfree(struct uip_conn *conn)
* *
* Description: * Description:
* Find a connection structure that is the appropriate * Find a connection structure that is the appropriate
* connection to be used withi the provided TCP/IP header * connection to be used with the provided TCP/IP header
* *
* Assumptions: * Assumptions:
* This function is called from UIP logic at interrupt level * This function is called from UIP logic at interrupt level

View File

@ -161,7 +161,7 @@ void uip_tcpinput(struct uip_driver_s *dev)
conn = uip_tcpaccept(pbuf); conn = uip_tcpaccept(pbuf);
if (conn) if (conn)
{ {
/* The connection structure was successfully allocated. Now see /* The connection structure was successfully allocated. Now see if
* there is an application waiting to accept the connection (or at * there is an application waiting to accept the connection (or at
* least queue it it for acceptance). * least queue it it for acceptance).
*/ */

View File

@ -2,7 +2,7 @@
* net/uip/uip_tcppoll.c * net/uip/uip_tcppoll.c
* Poll for the availability of TCP TX data * Poll for the availability of TCP TX data
* *
* Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. * Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <spudmonkey@racsa.co.cr> * Author: Gregory Nutt <spudmonkey@racsa.co.cr>
* *
* Adapted for NuttX from logic in uIP which also has a BSD-like license: * Adapted for NuttX from logic in uIP which also has a BSD-like license:
@ -97,7 +97,7 @@ void uip_tcppoll(struct uip_driver_s *dev, struct uip_conn *conn)
uint8 result; uint8 result;
/* Verify that the connection is established and if the connection has /* Verify that the connection is established and if the connection has
* oustanding (unacknowledged) sent data. * no outstanding (unacknowledged) sent data.
*/ */
if ((conn->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED && if ((conn->tcpstateflags & UIP_TS_MASK) == UIP_ESTABLISHED &&

View File

@ -285,10 +285,13 @@ int fdwatch(struct fdwatch_s *fw, long timeout_msecs)
{ {
/* Is there activity on this descriptor? */ /* Is there activity on this descriptor? */
if (fw->pollfds[i].revents & (POLLIN | POLLOUT | POLLERR | POLLHUP | POLLNVAL)) if (fw->pollfds[i].revents & (POLLIN | POLLERR | POLLHUP | POLLNVAL))
{ {
/* Yes... save it in a shorter list */ /* Yes... save it in a shorter list */
nvdbg("pollndx: %d fd: %d revents: %04x\n",
i, fw->pollfds[i].fd, fw->pollfds[i].revents);
fw->ready[fw->nactive++] = fw->pollfds[i].fd; fw->ready[fw->nactive++] = fw->pollfds[i].fd;
if (fw->nactive == ret) if (fw->nactive == ret)
{ {

View File

@ -565,8 +565,7 @@ static void clear_connection(struct connect_s *conn, struct timeval *tv)
conn->linger_timer = NULL; conn->linger_timer = NULL;
conn->hc->should_linger = FALSE; conn->hc->should_linger = FALSE;
} }
else if (conn->hc->should_linger)
if (conn->hc->should_linger)
{ {
fdwatch_del_fd(fw, conn->hc->conn_fd); fdwatch_del_fd(fw, conn->hc->conn_fd);
conn->conn_state = CNST_LINGERING; conn->conn_state = CNST_LINGERING;
@ -893,14 +892,7 @@ int thttpd_main(int argc, char **argv)
if (conn) if (conn)
{ {
hc = conn->hc; hc = conn->hc;
if (!fdwatch_check_fd(fw, hc->conn_fd)) if (fdwatch_check_fd(fw, hc->conn_fd))
{
/* Something went wrong */
nvdbg("Clearing connection\n");
clear_connection(conn, &tv);
}
else
{ {
nvdbg("Handle conn_state %d\n", conn->conn_state); nvdbg("Handle conn_state %d\n", conn->conn_state);
switch (conn->conn_state) switch (conn->conn_state)

View File

@ -60,7 +60,11 @@
static Timer *timers[HASH_SIZE]; static Timer *timers[HASH_SIZE];
static Timer *free_timers; static Timer *free_timers;
static int alloc_count, active_count, free_count; #if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
static int alloc_count;
static int active_count;
static int free_count;
#endif
/**************************************************************************** /****************************************************************************
* Public Data * Public Data
@ -72,7 +76,7 @@ ClientData JunkClientData;
* Private Functions * Private Functions
****************************************************************************/ ****************************************************************************/
static unsigned int hash(Timer *t) static unsigned int hash(Timer *tmr)
{ {
/* We can hash on the trigger time, even though it can change over the /* We can hash on the trigger time, even though it can change over the
* life of a timer via the periodic bit. * life of a timer via the periodic bit.
@ -80,97 +84,97 @@ static unsigned int hash(Timer *t)
* the hash and moves the timer to the appropriate list. * the hash and moves the timer to the appropriate list.
*/ */
return ((unsigned int)t->time.tv_sec ^ return ((unsigned int)tmr->time.tv_sec ^
(unsigned int)t->time.tv_usec) % HASH_SIZE; (unsigned int)tmr->time.tv_usec) % HASH_SIZE;
} }
static void l_add(Timer * t) static void l_add(Timer *tmr)
{ {
int h = t->hash; int h = tmr->hash;
register Timer *t2; register Timer *tmr2;
register Timer *t2prev; register Timer *tmr2prev;
t2 = timers[h]; tmr2 = timers[h];
if (t2 == NULL) if (tmr2 == NULL)
{ {
/* The list is empty. */ /* The list is empty. */
timers[h] = t; timers[h] = tmr;
t->prev = t->next = NULL; tmr->prev = tmr->next = NULL;
} }
else else
{ {
if (t->time.tv_sec < t2->time.tv_sec || if (tmr->time.tv_sec < tmr2->time.tv_sec ||
(t->time.tv_sec == t2->time.tv_sec && (tmr->time.tv_sec == tmr2->time.tv_sec &&
t->time.tv_usec <= t2->time.tv_usec)) tmr->time.tv_usec <= tmr2->time.tv_usec))
{ {
/* The new timer goes at the head of the list. */ /* The new timer goes at the head of the list. */
timers[h] = t; timers[h] = tmr;
t->prev = NULL; tmr->prev = NULL;
t->next = t2; tmr->next = tmr2;
t2->prev = t; tmr2->prev = tmr;
} }
else else
{ {
/* Walk the list to find the insertion point. */ /* Walk the list to find the insertion point. */
for (t2prev = t2, t2 = t2->next; t2 != NULL; for (tmr2prev = tmr2, tmr2 = tmr2->next; tmr2 != NULL;
t2prev = t2, t2 = t2->next) tmr2prev = tmr2, tmr2 = tmr2->next)
{ {
if (t->time.tv_sec < t2->time.tv_sec || if (tmr->time.tv_sec < tmr2->time.tv_sec ||
(t->time.tv_sec == t2->time.tv_sec && (tmr->time.tv_sec == tmr2->time.tv_sec &&
t->time.tv_usec <= t2->time.tv_usec)) tmr->time.tv_usec <= tmr2->time.tv_usec))
{ {
/* Found it. */ /* Found it. */
t2prev->next = t; tmr2prev->next = tmr;
t->prev = t2prev; tmr->prev = tmr2prev;
t->next = t2; tmr->next = tmr2;
t2->prev = t; tmr2->prev = tmr;
return; return;
} }
} }
/* Oops, got to the end of the list. Add to tail. */ /* Oops, got to the end of the list. Add to tail. */
t2prev->next = t; tmr2prev->next = tmr;
t->prev = t2prev; tmr->prev = tmr2prev;
t->next = NULL; tmr->next = NULL;
} }
} }
} }
static void l_remove(Timer * t) static void l_remove(Timer *tmr)
{ {
int h = t->hash; int h = tmr->hash;
if (t->prev == NULL) if (tmr->prev == NULL)
{ {
timers[h] = t->next; timers[h] = tmr->next;
} }
else else
{ {
t->prev->next = t->next; tmr->prev->next = tmr->next;
} }
if (t->next != NULL) if (tmr->next != NULL)
{ {
t->next->prev = t->prev; tmr->next->prev = tmr->prev;
} }
} }
static void l_resort(Timer * t) static void l_resort(Timer *tmr)
{ {
/* Remove the timer from its old list. */ /* Remove the timer from its old list. */
l_remove(t); l_remove(tmr);
/* Recompute the hash. */ /* Recompute the hash. */
t->hash = hash(t); tmr->hash = hash(tmr);
/* And add it back in to its new list, sorted correctly. */ /* And add it back in to its new list, sorted correctly. */
l_add(t); l_add(tmr);
} }
/**************************************************************************** /****************************************************************************
@ -187,66 +191,78 @@ void tmr_init(void)
} }
free_timers = NULL; free_timers = NULL;
alloc_count = active_count = free_count = 0; #if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
alloc_count = 0;
active_count = 0;
free_count = 0;
#endif
} }
Timer *tmr_create(struct timeval *nowP, TimerProc * timer_proc, Timer *tmr_create(struct timeval *now, TimerProc *timer_proc,
ClientData client_data, long msecs, int periodic) ClientData client_data, long msecs, int periodic)
{ {
Timer *t; Timer *tmr;
if (free_timers != NULL) if (free_timers != NULL)
{ {
t = free_timers; tmr = free_timers;
free_timers = t->next; free_timers = tmr->next;
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
--free_count; --free_count;
#endif
} }
else else
{ {
t = (Timer*)httpd_malloc(sizeof(Timer)); tmr = (Timer*)httpd_malloc(sizeof(Timer));
if (!t) if (!tmr)
{ {
return NULL; return NULL;
} }
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
alloc_count++; alloc_count++;
#endif
} }
t->timer_proc = timer_proc; tmr->timer_proc = timer_proc;
t->client_data = client_data; tmr->client_data = client_data;
t->msecs = msecs; tmr->msecs = msecs;
t->periodic = periodic; tmr->periodic = periodic;
if (nowP != NULL) if (now != NULL)
{ {
t->time = *nowP; tmr->time = *now;
} }
else else
{ {
(void)gettimeofday(&t->time, NULL); (void)gettimeofday(&tmr->time, NULL);
} }
t->time.tv_sec += msecs / 1000L; tmr->time.tv_sec += msecs / 1000L;
t->time.tv_usec += (msecs % 1000L) * 1000L; tmr->time.tv_usec += (msecs % 1000L) * 1000L;
if (t->time.tv_usec >= 1000000L) if (tmr->time.tv_usec >= 1000000L)
{ {
t->time.tv_sec += t->time.tv_usec / 1000000L; tmr->time.tv_sec += tmr->time.tv_usec / 1000000L;
t->time.tv_usec %= 1000000L; tmr->time.tv_usec %= 1000000L;
} }
t->hash = hash(t); tmr->hash = hash(tmr);
/* Add the new timer to the proper active list. */ /* Add the new timer to the proper active list. */
l_add(t); l_add(tmr);
++active_count; #if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
return t; active_count++;
#endif
nvdbg("Return: %p\n", tmr);
return tmr;
} }
long tmr_mstimeout(struct timeval *nowP) long tmr_mstimeout(struct timeval *now)
{ {
int h; int h;
int gotone; int gotone;
long msecs, m; long msecs, m;
register Timer *t; register Timer *tmr;
gotone = 0; gotone = 0;
msecs = 0; msecs = 0;
@ -257,11 +273,11 @@ long tmr_mstimeout(struct timeval *nowP)
for (h = 0; h < HASH_SIZE; ++h) for (h = 0; h < HASH_SIZE; ++h)
{ {
t = timers[h]; tmr = timers[h];
if (t != NULL) if (tmr != NULL)
{ {
m = (t->time.tv_sec - nowP->tv_sec) * 1000L + m = (tmr->time.tv_sec - now->tv_sec) * 1000L +
(t->time.tv_usec - nowP->tv_usec) / 1000L; (tmr->time.tv_usec - now->tv_usec) / 1000L;
if (!gotone) if (!gotone)
{ {
msecs = m; msecs = m;
@ -287,74 +303,86 @@ long tmr_mstimeout(struct timeval *nowP)
return msecs; return msecs;
} }
void tmr_run(struct timeval *nowP) void tmr_run(struct timeval *now)
{ {
int h; int h;
Timer *t; Timer *tmr;
Timer *next; Timer *next;
for (h = 0; h < HASH_SIZE; ++h) for (h = 0; h < HASH_SIZE; ++h)
for (t = timers[h]; t != NULL; t = next)
{ {
next = t->next; for (tmr = timers[h]; tmr != NULL; tmr = next)
{
next = tmr->next;
/* Since the lists are sorted, as soon as we find a timer * that isn't /* Since the lists are sorted, as soon as we find a timer * that isn'tmr
* ready yet, we can go on to the next list * ready yet, we can go on to the next list
*/ */
if (t->time.tv_sec > nowP->tv_sec || if (tmr->time.tv_sec > now->tv_sec ||
(t->time.tv_sec == nowP->tv_sec && t->time.tv_usec > nowP->tv_usec)) (tmr->time.tv_sec == now->tv_sec && tmr->time.tv_usec > now->tv_usec))
{ {
break; break;
} }
(t->timer_proc)(t->client_data, nowP); (tmr->timer_proc)(tmr->client_data, now);
if (t->periodic) if (tmr->periodic)
{ {
/* Reschedule. */ /* Reschedule. */
t->time.tv_sec += t->msecs / 1000L; tmr->time.tv_sec += tmr->msecs / 1000L;
t->time.tv_usec += (t->msecs % 1000L) * 1000L; tmr->time.tv_usec += (tmr->msecs % 1000L) * 1000L;
if (t->time.tv_usec >= 1000000L) if (tmr->time.tv_usec >= 1000000L)
{ {
t->time.tv_sec += t->time.tv_usec / 1000000L; tmr->time.tv_sec += tmr->time.tv_usec / 1000000L;
t->time.tv_usec %= 1000000L; tmr->time.tv_usec %= 1000000L;
} }
l_resort(t); l_resort(tmr);
} }
else else
{ {
tmr_cancel(t); tmr_cancel(tmr);
}
} }
} }
} }
void tmr_cancel(Timer * t) void tmr_cancel(Timer *tmr)
{ {
nvdbg("tmr: %p\n", tmr);
/* Remove it from its active list. */ /* Remove it from its active list. */
l_remove(t); l_remove(tmr);
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
active_count--; active_count--;
#endif
/* And put it on the free list. */ /* And put it on the free list. */
t->next = free_timers; tmr->next = free_timers;
free_timers = t; free_timers = tmr;
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
free_count++; free_count++;
t->prev = NULL; #endif
tmr->prev = NULL;
} }
void tmr_cleanup(void) void tmr_cleanup(void)
{ {
Timer *t; Timer *tmr;
while (free_timers != NULL) while (free_timers != NULL)
{ {
t = free_timers; tmr = free_timers;
free_timers = t->next; free_timers = tmr->next;
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
free_count--; free_count--;
httpd_free((void*)t); #endif
httpd_free((void*)tmr);
#if defined(CONFIG_DEBUG) && defined(CONFIG_DEBUG_NET)
alloc_count--; alloc_count--;
#endif
} }
} }