summaryrefslogtreecommitdiff
path: root/test/sanitizer_common/TestCases/Linux/pthread_mutex.cpp
blob: 610958143b472b1a1d0ca6cc6f9c33b817c3d97d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// RUN: %clangxx -O1 %s -o %t && %run %t
// RUN: %clangxx -O1 -DUSE_GLIBC %s -o %t && %run %t
// UNSUPPORTED: android

#include <pthread.h>

#ifdef USE_GLIBC
extern "C" int __pthread_mutex_lock(pthread_mutex_t *__mutex);
extern "C" int __pthread_mutex_unlock(pthread_mutex_t *__mutex);
#define LOCK __pthread_mutex_lock
#define UNLOCK __pthread_mutex_unlock
#else
#define LOCK pthread_mutex_lock
#define UNLOCK pthread_mutex_unlock
#endif

pthread_mutex_t m = PTHREAD_MUTEX_INITIALIZER;
int x;

static void *Start(void *arg) {
  LOCK(&m);
  ++x;
  UNLOCK(&m);
  return nullptr;
}

int main() {
  pthread_t threads[2] = {};
  for (pthread_t &t : threads)
    pthread_create(&t, 0, &Start, 0);
  for (pthread_t &t : threads)
    pthread_join(t, 0);
  return 0;
}