|
From: <sv...@va...> - 2007-11-05 10:22:22
|
Author: sewardj
Date: 2007-11-05 10:22:20 +0000 (Mon, 05 Nov 2007)
New Revision: 7097
Log:
Avoid hanging (or apparently so) when running on very slow machines.
Modified:
branches/THRCHECK/thrcheck/tests/tc23_bogus_condwait.c
Modified: branches/THRCHECK/thrcheck/tests/tc23_bogus_condwait.c
===================================================================
--- branches/THRCHECK/thrcheck/tests/tc23_bogus_condwait.c 2007-11-05 03:37:43 UTC (rev 7096)
+++ branches/THRCHECK/thrcheck/tests/tc23_bogus_condwait.c 2007-11-05 10:22:20 UTC (rev 7097)
@@ -5,11 +5,14 @@
#include <pthread.h>
#include <assert.h>
#include <unistd.h>
+#include <semaphore.h>
pthread_mutex_t mx[4];
pthread_cond_t cv;
pthread_rwlock_t rwl;
+sem_t quit_now;
+
void* rescue_me ( void* uu )
{
/* wait for, and unblock, the first wait */
@@ -24,18 +27,19 @@
sleep(1);
pthread_cond_signal( &cv );
- /* wait for grabber and main, then unblock the fourth wait */
- sleep(1+1);
+ /* wait for, and unblock, the fourth wait */
+ sleep(1);
pthread_cond_signal( &cv );
+ sem_wait( &quit_now );
return NULL;
}
void* grab_the_lock ( void* uu )
{
int r= pthread_mutex_lock( &mx[2] ); assert(!r);
- /* tc correctly complains that the thread is exiting whilst still
- holding a lock. A bit tricky to fix - we just live with it. */
+ sem_wait( &quit_now );
+ r= pthread_mutex_unlock( &mx[2] ); assert(!r);
return NULL;
}
@@ -52,9 +56,12 @@
r= pthread_cond_init(&cv, NULL); assert(!r);
r= pthread_rwlock_init(&rwl, NULL); assert(!r);
- r= pthread_create( &my_rescuer, NULL, rescue_me, NULL );
- assert(!r);
+ r= sem_init( &quit_now, 0,0 ); assert(!r);
+ r= pthread_create( &grabber, NULL, grab_the_lock, NULL ); assert(!r);
+ sleep(1); /* let the grabber get there first */
+
+ r= pthread_create( &my_rescuer, NULL, rescue_me, NULL ); assert(!r);
/* Do stupid things and hope that rescue_me gets us out of
trouble */
@@ -68,10 +75,11 @@
r= pthread_cond_wait(&cv, (pthread_mutex_t*)&rwl );
/* mx is held by someone else. */
- r= pthread_create( &grabber, NULL, grab_the_lock, NULL ); assert(!r);
- sleep(1); /* let the grabber get there first */
r= pthread_cond_wait(&cv, &mx[2] );
+ r= sem_post( &quit_now ); assert(!r);
+ r= sem_post( &quit_now ); assert(!r);
+
r= pthread_join( my_rescuer, NULL ); assert(!r);
r= pthread_join( grabber, NULL ); assert(!r);
return 0;
|