reference, declarationdefinition
definition → references, declarations, derived classes, virtual overrides
reference to multiple definitions → definitions
unreferenced
    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
   35
   36
   37
   38
   39
   40
   41
   42
   43
   44
   45
   46
   47
   48
   49
   50
// Test that the deadlock detector can find a deadlock that actually happened.
// Currently we will fail to report such a deadlock because we check for
// cycles in lock-order graph after pthread_mutex_lock.

// RUN: %clangxx_tsan %s -o %t
// RUN: not %run %t 2>&1 | FileCheck %s
// XFAIL: *
#include <pthread.h>
#include <stdio.h>
#include <unistd.h>

pthread_mutex_t mu1, mu2;
pthread_barrier_t barrier;

void *Thread(void *p) {
  // mu2 => mu1
  pthread_mutex_lock(&mu2);
  pthread_barrier_wait(&barrier);
  pthread_mutex_lock(&mu1);
  // CHECK: ThreadSanitizer: lock-order-inversion (potential deadlock)
  pthread_mutex_unlock(&mu1);
  pthread_mutex_unlock(&mu2);
  return p;
}

int main() {
  pthread_mutex_init(&mu1, NULL);
  pthread_mutex_init(&mu2, NULL);
  pthread_barrier_init(&barrier, 0, 2);

  fprintf(stderr, "This test is going to deadlock and die in 3 seconds\n");
  alarm(3);

  pthread_t t;
  pthread_create(&t, 0, Thread, 0);

  // mu1 => mu2
  pthread_mutex_lock(&mu1);
  pthread_barrier_wait(&barrier);
  pthread_mutex_lock(&mu2);
  pthread_mutex_unlock(&mu2);
  pthread_mutex_unlock(&mu1);

  pthread_join(t, 0);

  pthread_mutex_destroy(&mu1);
  pthread_mutex_destroy(&mu2);
  pthread_barrier_destroy(&barrier);
  fprintf(stderr, "FAILED\n");
}