lwip-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[lwip-devel] [bug #19167] tcp timeout handler can cause crash being invo


From: Dmitry Potapov
Subject: [lwip-devel] [bug #19167] tcp timeout handler can cause crash being invoked by sys_sem_wait()
Date: Thu, 01 Mar 2007 11:32:14 +0000
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.0.10) Gecko/20070216 Firefox/1.5.0.10

Follow-up Comment #12, bug #19167 (project lwip):

Timer overflow:
~~~~~~~~~~~~~~
I think practically all systems have some function that returns of number of
ticks, and this function always overflow correctly.

The problem is to convert ticks to milliseconds. In some case, the
converation is very straightforward, like:
msecs = ticks * N;
(for many systems N will be 10).
And this will work correctly with overflow.

However, on some systems, you need to something more complex:
msecs = ticks * M / N;
In general, it will not work correctly with 32-bits. So you need to write:
msecs = (u32_t)(((u64_t)ticks) * M / N);
because it is inside of sys_arch_time_now(), it is usually not a problem. 

However, the code above will not work if a tick less than 1ms or if the timer
counter wraps earlier than 49.71 days (e.g., the original tick counter is
16-bit). So there is an alternative solution, which always works and does not
require 64-bit operation:

static u32_t remembered_ticks, remembered_ms;
u32_t sys_arch_time_now(void)
{       
        u32_t now, now_ms;
        u32_t delta, delta_ms;
        now = get_ticks();              /* get get_ticks wraps correctly */
        SYS_ARCH_DECL_PROTECT(lev);
        SYS_ARCH_PROTECT(lev);
        delta = now - remembered_ticks; /* so delta is always correct */
        remembered_ticks = now;
        delta_ms = delta * M / N;
        now_ms = remembered_ms + delta_ms;
        remembered_ms = now_ms;
        SYS_ARCH_UNPROTECT(lev);
        return now_ms;
}       
The botton line, sys_arch_time_now() can be implemented for any platform, but
the most straightforward approach (ticks*M/N) does not always work.

To #11: I fully support removing timeouts from sys_sem_wait. I think only
that it would be better to fix timeout handling first.


    _______________________________________________________

Reply to this item at:

  <http://savannah.nongnu.org/bugs/?19167>

_______________________________________________
  Message sent via/by Savannah
  http://savannah.nongnu.org/





reply via email to

[Prev in Thread] Current Thread [Next in Thread]