|
From: <sv...@va...> - 2012-01-19 19:56:53
|
Author: bart
Date: 2012-01-19 19:52:15 +0000 (Thu, 19 Jan 2012)
New Revision: 12346
Log:
drd: Fix a race condition in the pthread_create() intercept.
Avoid that the futex wake call in DRD_(sema_up)() can get invoked after the semaphore has
already been destroyed. This is most likely the real fix for the bug described in the
commit message of r12332.
Modified:
trunk/drd/drd_pthread_intercepts.c
trunk/drd/tests/fp_race_xml.stderr.exp
Modified: trunk/drd/drd_pthread_intercepts.c
===================================================================
--- trunk/drd/drd_pthread_intercepts.c 2012-01-18 09:47:12 UTC (rev 12345)
+++ trunk/drd/drd_pthread_intercepts.c 2012-01-19 19:52:15 UTC (rev 12346)
@@ -136,7 +136,8 @@
/* Local data structures. */
typedef struct {
- volatile int counter;
+ pthread_mutex_t mutex;
+ int counter;
} DrdSema;
typedef struct
@@ -180,18 +181,22 @@
static void DRD_(sema_init)(DrdSema* sema)
{
DRD_IGNORE_VAR(sema->counter);
+ pthread_mutex_init(&sema->mutex, NULL);
sema->counter = 0;
}
static void DRD_(sema_destroy)(DrdSema* sema)
{
+ pthread_mutex_destroy(&sema->mutex);
}
static void DRD_(sema_down)(DrdSema* sema)
{
int res = ENOSYS;
+ pthread_mutex_lock(&sema->mutex);
while (sema->counter == 0) {
+ pthread_mutex_unlock(&sema->mutex);
#ifdef HAVE_USABLE_LINUX_FUTEX_H
if (syscall(__NR_futex, (UWord)&sema->counter,
FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0) == 0)
@@ -207,17 +212,21 @@
*/
if (res != 0 && res != EWOULDBLOCK)
sched_yield();
+ pthread_mutex_lock(&sema->mutex);
}
sema->counter--;
+ pthread_mutex_unlock(&sema->mutex);
}
static void DRD_(sema_up)(DrdSema* sema)
{
+ pthread_mutex_lock(&sema->mutex);
sema->counter++;
#ifdef HAVE_USABLE_LINUX_FUTEX_H
syscall(__NR_futex, (UWord)&sema->counter,
FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1);
#endif
+ pthread_mutex_unlock(&sema->mutex);
}
/**
Modified: trunk/drd/tests/fp_race_xml.stderr.exp
===================================================================
--- trunk/drd/tests/fp_race_xml.stderr.exp 2012-01-18 09:47:12 UTC (rev 12345)
+++ trunk/drd/tests/fp_race_xml.stderr.exp 2012-01-19 19:52:15 UTC (rev 12346)
@@ -79,7 +79,7 @@
</other_segment_end>
</error>
- <trace><text>drd_post_thread_join joiner = 1, joinee = 2, new vc: [ 1: 4, 2: 1 ]</text></trace>
+ <trace><text>drd_post_thread_join joiner = 1, joinee = 2, new vc: [ 1: 8, 2: 3 ]</text></trace>
<trace><text>drd_thread_finished tid = 1</text></trace>
<status>
|