public inbox for ecos-discuss@sourceware.org
 help / color / mirror / Atom feed
* [ECOS] Help with pthread_cond_timedwait
@ 2012-05-15 15:34 Graves, Daniel (GE Healthcare)
  2012-05-15 19:06 ` Christophe Coutand
  0 siblings, 1 reply; 2+ messages in thread
From: Graves, Daniel (GE Healthcare) @ 2012-05-15 15:34 UTC (permalink / raw)
  To: ecos-discuss

Hello,

I've been developing some communication software that needs to wait for a message over a serial port.  I implemented it using pthread_cond_timedwait under Linux using 2.6.34 kernel.  Basically when a message comes over the serial port the pthread_cond_timedwait is signaled.  It waits for 200ms if it is not signaled in time.  This code has worked really well in Linux, but I needed to port it over to eCos.  At that point the exact same code would timeout immediately even though the serial messages were signaling it on time.  The error 360 for ETIMEDOUT was reporting and it was clear that it wasn't waiting long enough.  As a last desperate attempt to get this to work I changed the code to use cyg_cond_timed_wait and cyg_cond_mutex from eCos.  That worked for me and timeouts were no longer occurring.  However, I would like to know what is wrong with pthread_cond_timedwait, because it seems like there is a bug.  But after looking at the code for each function they both basically end up calling Cyg_Condition_Variable's wait methods.  Below is a snippet of the code I'm referring to.  I've searched through this mailing list for any similar topics.  There was one but that one focused on clock_gettime.  Unfortunately I do not know which version of eCos I'm using as I have just joined the project where we're using eCos.  I will try to find that out.

.....
  struct timespec waittime;
  struct timeval abstime;
.....

    gettimeofday(&abstime,NULL);
   //we want to wait 200 msec before declaring timeout
    abstime.tv_usec += 2000000;

    if(abstime.tv_usec >= 1000000)
    {
      abstime.tv_sec += 1;
      abstime.tv_usec = abstime.tv_usec % 1000000;
    }
    waittime.tv_sec = abstime.tv_sec;
    waittime.tv_nsec = abstime.tv_usec*1000;
    int cond_result = pthread_cond_timedwait(&ackcond,&readlock,&waittime);
    if ( cond_result != 0 )
    {
        printf("SerialComm:Failed to set the conditional variable, error = %d\n", cond_result);
    }
    else
    {
        #if DEBUG
        printf("SerialComm:Successfully set conditional variable ackcond\n");
        #endif
    }

......

Thanks,

Daniel P Graves


--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 2+ messages in thread

* RE: [ECOS] Help with pthread_cond_timedwait
  2012-05-15 15:34 [ECOS] Help with pthread_cond_timedwait Graves, Daniel (GE Healthcare)
@ 2012-05-15 19:06 ` Christophe Coutand
  0 siblings, 0 replies; 2+ messages in thread
From: Christophe Coutand @ 2012-05-15 19:06 UTC (permalink / raw)
  To: Graves, Daniel (GE Healthcare), ecos-discuss

You can possibly log the current time (cyg_current_time()) in pthread_cond_timedwait() before the call to:

((Cyg_Condition_Variable *)cond)->wait( *(Cyg_Mutex *)mutex, ticks );

and compare with the ticks value, that way you can rule out any issue with the time/ticks conversion.

Christophe

-----Original Message-----
From: ecos-discuss-owner@ecos.sourceware.org [mailto:ecos-discuss-owner@ecos.sourceware.org] On Behalf Of Graves, Daniel (GE Healthcare)
Sent: 15. mai 2012 17:34
To: ecos-discuss@sourceware.org
Subject: [ECOS] Help with pthread_cond_timedwait

Hello,

I've been developing some communication software that needs to wait for a message over a serial port.  I implemented it using pthread_cond_timedwait under Linux using 2.6.34 kernel.  Basically when a message comes over the serial port the pthread_cond_timedwait is signaled.  It waits for 200ms if it is not signaled in time.  This code has worked really well in Linux, but I needed to port it over to eCos.  At that point the exact same code would timeout immediately even though the serial messages were signaling it on time.  The error 360 for ETIMEDOUT was reporting and it was clear that it wasn't waiting long enough.  As a last desperate attempt to get this to work I changed the code to use cyg_cond_timed_wait and cyg_cond_mutex from eCos.  That worked for me and timeouts were no longer occurring.  However, I would like to know what is wrong with pthread_cond_timedwait, because it seems like there is a bug.  But after looking at the code for each function they both basically end up calling Cyg_Condition_Variable's wait methods.  Below is a snippet of the code I'm referring to.  I've searched through this mailing list for any similar topics.  There was one but that one focused on clock_gettime.  Unfortunately I do not know which version of eCos I'm using as I have just joined the project where we're using eCos.  I will try to find that out.

.....
  struct timespec waittime;
  struct timeval abstime;
.....

    gettimeofday(&abstime,NULL);
   //we want to wait 200 msec before declaring timeout
    abstime.tv_usec += 2000000;

    if(abstime.tv_usec >= 1000000)
    {
      abstime.tv_sec += 1;
      abstime.tv_usec = abstime.tv_usec % 1000000;
    }
    waittime.tv_sec = abstime.tv_sec;
    waittime.tv_nsec = abstime.tv_usec*1000;
    int cond_result = pthread_cond_timedwait(&ackcond,&readlock,&waittime);
    if ( cond_result != 0 )
    {
        printf("SerialComm:Failed to set the conditional variable, error = %d\n", cond_result);
    }
    else
    {
        #if DEBUG
        printf("SerialComm:Successfully set conditional variable ackcond\n");
        #endif
    }

......

Thanks,

Daniel P Graves


-- 
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss


--
Before posting, please read the FAQ: http://ecos.sourceware.org/fom/ecos
and search the list archive: http://ecos.sourceware.org/ml/ecos-discuss

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2012-05-15 19:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-05-15 15:34 [ECOS] Help with pthread_cond_timedwait Graves, Daniel (GE Healthcare)
2012-05-15 19:06 ` Christophe Coutand

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).