[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [lwip-users] use of struct timeval in sys_jiffies, UNIX port
From: |
Jonathan Larmour |
Subject: |
Re: [lwip-users] use of struct timeval in sys_jiffies, UNIX port |
Date: |
Thu, 19 Jun 2008 16:30:28 +0100 |
User-agent: |
Thunderbird 1.5.0.12 (X11/20070530) |
Kieran Mansley wrote:
> On Thu, 2008-06-19 at 16:37 +0200, address@hidden wrote:
>> My compiler complained about tv.tv_sec being used before it was
>> initialized, and upon inspection I must say I don't really understand
>> what is happening.
>>
>> Depending on the compiler, isn't the value of tv_sec at best
>> initialized to zero, otherwise unpredictable? And the result from
>> gettimeofday is not used for anything?
>>
>> From sys_arch.c, UNIX port, latest version in CVS.
>>
>> unsigned long
>> sys_jiffies(void)
>> {
>> struct timeval tv;
>> unsigned long sec = tv.tv_sec;
>> long usec = tv.tv_usec;
>>
>> gettimeofday(&tv,NULL);
>
> I agree that this looks wrong. Should be a straightforward thing to fix
> though. Could you file a bug?
I think that's not the only issue in that function; it goes on to return this:
return HZ * sec + usec;
with HZ probably 100 (and if anything else, higher).
But on a unix system, sec is around 1.2 billion, and so multiplying by 100
will overflow. It's probably not a big deal as in practice it will wrap,
and so still increase monotonically, but it's also easy to fix by
subtracting starttime.tv_sec so that's what I'm checking in as well. I
think the fix is obvious, so no need to file a bug
Jifl
Index: sys_arch.c
===================================================================
RCS file: /sources/lwip/contrib/ports/unix/sys_arch.c,v
retrieving revision 1.21
diff -u -5 -p -r1.21 sys_arch.c
--- sys_arch.c 30 May 2008 11:41:51 -0000 1.21
+++ sys_arch.c 19 Jun 2008 15:29:50 -0000
@@ -585,14 +585,16 @@ sys_arch_unprotect(sys_prot_t pval)
unsigned long
sys_jiffies(void)
{
struct timeval tv;
- unsigned long sec = tv.tv_sec;
- long usec = tv.tv_usec;
+ unsigned long sec;
+ long usec;
gettimeofday(&tv,NULL);
+ sec = tv.tv_sec - starttime.tv_sec;
+ usec = tv.tv_usec;
if (sec >= (MAX_JIFFY_OFFSET / HZ))
return MAX_JIFFY_OFFSET;
usec += 1000000L / HZ - 1;
usec /= 1000000L / HZ;
--
eCosCentric Limited http://www.eCosCentric.com/ The eCos experts
Barnwell House, Barnwell Drive, Cambridge, UK. Tel: +44 1223 245571
Registered in England and Wales: Reg No 4422071.
------["Si fractum non sit, noli id reficere"]------ Opinions==mine