Hi,

The same false positive appears when mixing mutexes and semaphores. Test
attached.
As with cond vars, we are able to transition Excl(T1)->Excl(T2), but can not
do ShM(T1,T2)->Excl(T2).

As I understand, fixing this properly will require storing SegmentID in all
shadow words, not only in those that are in Exclusive state.
This will hardly squeeze into 32 bits though, need 64... :(

--kcc

#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <stdio.h>

// This test is race free, but helgrind 3.3.0 reports a race

// main().........................worker()
// 1. start(worker)
// 2. lock(MU)
// 3. write(GLOB)
// 4. unlock(MU)
//                                a. lock(MU)
//                                b. write(GLOB)
//                                c. unlock(MU)
// 5. post(SEM)--------\
//                      \------>  d. wait(SEM)
//                                e. write(GLOB)
// 6. join(worker)
// 7. read(GLOB)



int GLOB = 0;
sem_t SEM;
pthread_mutex_t MU = PTHREAD_MUTEX_INITIALIZER;

void *worker(void *) {
  pthread_mutex_lock(&MU);
  GLOB = 22;
  pthread_mutex_unlock(&MU);
  sem_wait(&SEM);
  GLOB = 2; // RACE is reported here, but shouldn't
  return NULL;
}

int main() {

  sem_init(&SEM, 0, 0);
  pthread_t thread;
  pthread_create(&thread, NULL, worker, NULL);

  pthread_mutex_lock(&MU);
  GLOB = 11;
  pthread_mutex_unlock(&MU);

  sem_post(&SEM);

  pthread_join(thread, NULL);
  printf("glob=%d\n", GLOB);
}





On Dec 7, 2007 1:30 PM, Konstantin Serebryany <
[EMAIL PROTECTED]> wrote:

> >> Last night's patch (happens-before-cvhack.patch) also makes this, q2.cc
> ,
> >> run without race warnings.
>
> Amazing! --happens-before=cvhack does help with this patch!
> From the comments it does look 'insanely inefficient', but it's better
> than nothing. :)
> I'll try other tests with cond vars.
>
>
> >> Are you referring to the VALGRIND_HG_POST_WAIT that is introduced
> >> at the bottom of page 31 of Arndt Muehlenfeld's PhD thesis, or some
> >> other thing?
> Can you give me a link to Arndt's PhD? I can't find it :(
>
> --kcc
>
>
>
>
> On Dec 7, 2007 12:44 PM, Julian Seward < [EMAIL PROTECTED]> wrote:
>
> >
> > On Thursday 06 December 2007 09:53, Konstantin Serebryany wrote:
> >
> > > I've modified my test (attached, q2.cc), hope it will be helpful :)
> > > It now has N worker threads. If N >= 2 the race is reported even for
> > GLOB1.
> >
> > Last night's patch (happens-before-cvhack.patch ) also makes this, q2.cc
> > ,
> > run without race warnings.
> >
> >
> > > I did not find VALGRIND_HG_POST_WAIT anywhere in valgrind nor in the
> > net.
> > > Is it supposed to be used like this?
> > >
> > >   #include "helgrind.h"
> > >   ...
> > >   pthread_mutex_lock(&MU);
> > >      while (COND != n_tasks) {
> > >       pthread_cond_wait(&CV, &MU);
> > >     }
> > >     VALGRIND_HG_POST_WAIT(&CV)
> > >   pthread_mutex_unlock(&MU);
> >
> > What is the behaviour of VALGRIND_HG_POST_WAIT supposed to be?
> >
> > Are you referring to the VALGRIND_HG_POST_WAIT that is introduced
> > at the bottom of page 31 of Arndt Muehlenfeld's PhD thesis, or some
> > other thing?
> >
> > J
> >
>
>
#include <pthread.h>
#include <semaphore.h>
#include <unistd.h>
#include <stdio.h>

// This test is race free, but helgrind 3.3.0 reports a race

// main().........................worker()
// 1. start(worker)               
// 2. lock(MU)
// 3. write(GLOB)
// 4. unlock(MU)
//                                a. lock(MU)
//                                b. write(GLOB)
//                                c. unlock(MU)
// 5. post(SEM)--------\
//                      \------>  d. wait(SEM)
//                                e. write(GLOB)
// 6. join(worker) 
// 7. read(GLOB)



int GLOB = 0;
sem_t SEM;
pthread_mutex_t MU = PTHREAD_MUTEX_INITIALIZER;

void *worker(void *) {
  pthread_mutex_lock(&MU);
  GLOB = 22;
  pthread_mutex_unlock(&MU);
  sem_wait(&SEM);
  GLOB = 2; // RACE is reported here, but shouldn't
  return NULL;
}

int main() {
  
  sem_init(&SEM, 0, 0);
  pthread_t thread;
  pthread_create(&thread, NULL, worker, NULL);

  pthread_mutex_lock(&MU);
  GLOB = 11;
  pthread_mutex_unlock(&MU);

  sem_post(&SEM);

  pthread_join(thread, NULL);
  printf("glob=%d\n", GLOB);
}
-------------------------------------------------------------------------
SF.Net email is sponsored by:
Check out the new SourceForge.net Marketplace.
It's the best place to buy or sell services
for just about anything Open Source.
http://ad.doubleclick.net/clk;164216239;13503038;w?http://sf.net/marketplace
_______________________________________________
Valgrind-developers mailing list
Valgrind-developers@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/valgrind-developers

Reply via email to