avoid pthread_mutex_timedlock in valgrind
This commit is contained in:
@@ -135,46 +135,80 @@ MutexImpl_Pthreads::~MutexImpl_Pthreads()
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#if defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) && defined(CRASH_HANDLER)
|
||||||
|
static bool UseTimedlock()
|
||||||
|
{
|
||||||
|
#if defined(CPU_X86) && defined(__GNUC__)
|
||||||
|
/* Valgrind crashes and burns on pthread_mutex_timedlock. */
|
||||||
|
static int under_valgrind = -1;
|
||||||
|
if( under_valgrind == -1 )
|
||||||
|
{
|
||||||
|
unsigned int magic[8] = { 0x00001001, 0, 0, 0, 0, 0, 0, 0 };
|
||||||
|
asm(
|
||||||
|
"mov %1, %%eax\n"
|
||||||
|
"mov $0, %%edx\n"
|
||||||
|
"rol $29, %%eax\n"
|
||||||
|
"rol $3, %%eax\n"
|
||||||
|
"ror $27, %%eax\n"
|
||||||
|
"ror $5, %%eax\n"
|
||||||
|
"rol $13, %%eax\n"
|
||||||
|
"rol $19, %%eax\n"
|
||||||
|
"mov %%edx, %0\t"
|
||||||
|
: "=r" (under_valgrind): "r" (magic): "eax", "edx" );
|
||||||
|
}
|
||||||
|
|
||||||
|
if( under_valgrind )
|
||||||
|
return false;
|
||||||
|
#endif
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
bool MutexImpl_Pthreads::Lock()
|
bool MutexImpl_Pthreads::Lock()
|
||||||
{
|
{
|
||||||
#if defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) && defined(CRASH_HANDLER)
|
#if defined(HAVE_PTHREAD_MUTEX_TIMEDLOCK) && defined(CRASH_HANDLER)
|
||||||
int len = 10; /* seconds */
|
if( UseTimedlock() )
|
||||||
int tries = 2;
|
|
||||||
|
|
||||||
while( tries-- )
|
|
||||||
{
|
{
|
||||||
/* Wait for ten seconds. If it takes longer than that, we're probably deadlocked. */
|
int len = 10; /* seconds */
|
||||||
timeval tv;
|
int tries = 2;
|
||||||
gettimeofday( &tv, NULL );
|
|
||||||
|
|
||||||
timespec ts;
|
while( tries-- )
|
||||||
ts.tv_sec = tv.tv_sec + len;
|
|
||||||
ts.tv_nsec = tv.tv_usec * 1000;
|
|
||||||
int ret = pthread_mutex_timedlock( &mutex, &ts );
|
|
||||||
switch( ret )
|
|
||||||
{
|
{
|
||||||
case 0:
|
/* Wait for ten seconds. If it takes longer than that, we're probably deadlocked. */
|
||||||
return true;
|
timeval tv;
|
||||||
|
gettimeofday( &tv, NULL );
|
||||||
|
|
||||||
case EINTR:
|
timespec ts;
|
||||||
/* Ignore it. */
|
ts.tv_sec = tv.tv_sec + len;
|
||||||
++tries;
|
ts.tv_nsec = tv.tv_usec * 1000;
|
||||||
continue;
|
int ret = pthread_mutex_timedlock( &mutex, &ts );
|
||||||
|
switch( ret )
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
return true;
|
||||||
|
|
||||||
case ETIMEDOUT:
|
case EINTR:
|
||||||
/* Timed out. Probably deadlocked. Try again one more time, with a smaller
|
/* Ignore it. */
|
||||||
* timeout, just in case we're debugging and happened to stop while waiting
|
++tries;
|
||||||
* on the mutex. */
|
continue;
|
||||||
len = 1;
|
|
||||||
break;
|
|
||||||
|
|
||||||
default:
|
case ETIMEDOUT:
|
||||||
FAIL_M( ssprintf("pthread_mutex_timedlock: %s", strerror(errno)) );
|
/* Timed out. Probably deadlocked. Try again one more time, with a smaller
|
||||||
|
* timeout, just in case we're debugging and happened to stop while waiting
|
||||||
|
* on the mutex. */
|
||||||
|
len = 1;
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
FAIL_M( ssprintf("pthread_mutex_timedlock: %s", strerror(errno)) );
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
#else
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
int ret;
|
int ret;
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
@@ -185,7 +219,6 @@ bool MutexImpl_Pthreads::Lock()
|
|||||||
ASSERT_M( ret == 0, ssprintf("pthread_mutex_lock: %s", strerror(errno)) );
|
ASSERT_M( ret == 0, ssprintf("pthread_mutex_lock: %s", strerror(errno)) );
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool MutexImpl_Pthreads::TryLock()
|
bool MutexImpl_Pthreads::TryLock()
|
||||||
|
|||||||
Reference in New Issue
Block a user