2015-04-28 06:08:08 +08:00
|
|
|
// RUN: %clangxx_tsan -O1 %s -o %t && %deflake %run %t | FileCheck %s
|
2014-03-12 17:48:14 +08:00
|
|
|
// CHECK-NOT: unlock of unlocked mutex
|
2013-09-03 23:04:15 +08:00
|
|
|
// CHECK: ThreadSanitizer: data race
|
|
|
|
// CHECK: pthread_cond_signal
|
|
|
|
|
2015-01-21 21:50:02 +08:00
|
|
|
#include "test.h"
|
2013-09-03 23:04:15 +08:00
|
|
|
|
|
|
|
struct Ctx {
|
|
|
|
pthread_mutex_t m;
|
|
|
|
pthread_cond_t c;
|
|
|
|
bool done;
|
|
|
|
};
|
|
|
|
|
|
|
|
void *thr(void *p) {
|
|
|
|
Ctx *c = (Ctx*)p;
|
|
|
|
pthread_mutex_lock(&c->m);
|
|
|
|
c->done = true;
|
|
|
|
pthread_mutex_unlock(&c->m);
|
|
|
|
pthread_cond_signal(&c->c);
|
2015-01-21 21:50:02 +08:00
|
|
|
barrier_wait(&barrier);
|
2013-09-03 23:04:15 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int main() {
|
2015-01-21 21:50:02 +08:00
|
|
|
barrier_init(&barrier, 2);
|
2013-09-03 23:04:15 +08:00
|
|
|
Ctx *c = new Ctx();
|
|
|
|
pthread_mutex_init(&c->m, 0);
|
|
|
|
pthread_cond_init(&c->c, 0);
|
|
|
|
pthread_t th;
|
|
|
|
pthread_create(&th, 0, thr, c);
|
|
|
|
pthread_mutex_lock(&c->m);
|
|
|
|
while (!c->done)
|
|
|
|
pthread_cond_wait(&c->c, &c->m);
|
|
|
|
pthread_mutex_unlock(&c->m);
|
2015-01-21 21:50:02 +08:00
|
|
|
// otherwise it can be reported as use-after-free
|
|
|
|
barrier_wait(&barrier);
|
2013-09-03 23:04:15 +08:00
|
|
|
delete c;
|
|
|
|
pthread_join(th, 0);
|
|
|
|
}
|