hung pthread_cond_timedwait on uCLinux with v2linux

Jun 27, 2004 2 Replies

hi folks,



I have an application running on ARM946 with uClinux OS and Montavista v2linux. A thread performs a semaphore take (semTake) with timeout which v2linux code implements (with conditional variable) as a call to pthread_cond_timedwait. This call gets hung, I do not get a timeout (ETIMEDOUT) or any other return code. It seems that the thread stack got messed up somehow. If I change the call to a semTake(FOREVER) this does not occure (i.e. the thread lives). If I replace the pthread_cond_timedwait call with a delay of several seconds it does not occure. To disable the problem of stack overflow I created a new thread to test this, here is the code:



unsigned __TASKCONV testTask(void) { static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;



struct timeval now; struct timespec timeout; int retcode;



if (pthread_mutex_lock(&mutex) != 0) { fprintf(stderr, "\r\n pthread_mutex_lock failed %s",strerror(errno)); exit(0); }



while (1) { if (gettimeofday(&now, NULL) != 0) { fprintf(stderr, "\r\n gettimeofday failed %s",strerror(errno)); } timeout.tv_sec = now.tv_sec + 5; timeout.tv_nsec = now.tv_usec * 1000;



fprintf(stderr, "\r\n before pthread_cond_timedwait"); retcode = pthread_cond_timedwait(&cond, &mutex, &timeout); fprintf(stderr, "\r\n after pthread_cond_timedwait");



if (retcode == ETIMEDOUT) { /* timeout occurred */ fprintf(stderr, "\r\n retcode == ETIMEDOUT"); } else { /* operate on x and y */ fprintf(stderr, "\r\n retcode != ETIMEDOUT"); }



/*pthread_mutex_unlock(&mut);*/ } }



When running this thread I only see the "before pthread_cond_timedwait" print! Does anybody know of the same problem I have encountered? What am I doing wrong?



Tx, Ofer


Hi,

there is nothing wrong with pthread_cond_timedwait, but YOU wait so long. You get the actual time and add 5 seconds. So you wait the seconds since

1970 + 5 seconds, and thats pretty to much :).

Change to:

Have a nice day.

Mit freundlichen Grüßen / Best Regards Sebastian Haas

Hi,

According to linux man, the pthread_cond_timedwait call expects absolute time. The base (where abstime=0) is Jan 1970, this meens that if I do as you suggested the pthread_cond_timedwait will terminate at once and return a code of ETIMEDOUT without any wait because the absolute date/hour has allready passed. See the man example...

Ofer

Join the Discussion

Have something to add? Share your thoughts — no account required.

Didn't find your answer?

Ask the community — no account required