]> git.proxmox.com Git - rustc.git/blame - src/llvm/unittests/Support/ManagedStatic.cpp
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / unittests / Support / ManagedStatic.cpp
CommitLineData
223e47cc
LB
1//===- llvm/unittest/Support/ManagedStatic.cpp - ManagedStatic tests ------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9#include "llvm/Support/ManagedStatic.h"
223e47cc 10#include "llvm/Config/config.h"
970d7e83 11#include "llvm/Support/Threading.h"
223e47cc
LB
12#ifdef HAVE_PTHREAD_H
13#include <pthread.h>
14#endif
15
16#include "gtest/gtest.h"
17
18using namespace llvm;
19
20namespace {
21
1a4d82fc
JJ
22#if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H) && \
23 !__has_feature(memory_sanitizer)
223e47cc
LB
24namespace test1 {
25 llvm::ManagedStatic<int> ms;
26 void *helper(void*) {
27 *ms;
1a4d82fc 28 return nullptr;
223e47cc 29 }
970d7e83
LB
30
31 // Valgrind's leak checker complains glibc's stack allocation.
32 // To appease valgrind, we provide our own stack for each thread.
33 void *allocate_stack(pthread_attr_t &a, size_t n = 65536) {
34 void *stack = malloc(n);
35 pthread_attr_init(&a);
36#if defined(__linux__)
37 pthread_attr_setstack(&a, stack, n);
38#endif
39 return stack;
40 }
223e47cc
LB
41}
42
43TEST(Initialize, MultipleThreads) {
44 // Run this test under tsan: http://code.google.com/p/data-race-test/
45
970d7e83
LB
46 pthread_attr_t a1, a2;
47 void *p1 = test1::allocate_stack(a1);
48 void *p2 = test1::allocate_stack(a2);
49
223e47cc 50 pthread_t t1, t2;
1a4d82fc
JJ
51 pthread_create(&t1, &a1, test1::helper, nullptr);
52 pthread_create(&t2, &a2, test1::helper, nullptr);
53 pthread_join(t1, nullptr);
54 pthread_join(t2, nullptr);
970d7e83
LB
55 free(p1);
56 free(p2);
223e47cc
LB
57}
58#endif
59
60} // anonymous namespace