ManagedStatic.cpp revision 91e22b0dd3c083ad67fc88a043019f988670c192
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" 10#include "llvm/Config/config.h" 11#include "llvm/Support/Threading.h" 12#ifdef HAVE_PTHREAD_H 13#include <pthread.h> 14#endif 15 16#include "gtest/gtest.h" 17 18using namespace llvm; 19 20namespace { 21 22#ifdef HAVE_PTHREAD_H 23namespace test1 { 24 llvm::ManagedStatic<int> ms; 25 void *helper(void*) { 26 *ms; 27 return NULL; 28 } 29 30 // Valgrind's leak checker complains glibc's stack allocation. 31 // To appease valgrind, we provide our own stack for each thread. 32 void *allocate_stack(pthread_attr_t &a, size_t n = 65536) { 33 void *stack = malloc(n); 34 pthread_attr_init(&a); 35 pthread_attr_setstack(&a, stack, n); 36 return stack; 37 } 38} 39 40TEST(Initialize, MultipleThreads) { 41 // Run this test under tsan: http://code.google.com/p/data-race-test/ 42 43 pthread_attr_t a1, a2; 44 void *p1 = test1::allocate_stack(a1); 45 void *p2 = test1::allocate_stack(a2); 46 47 llvm_start_multithreaded(); 48 pthread_t t1, t2; 49 pthread_create(&t1, &a1, test1::helper, NULL); 50 pthread_create(&t2, &a2, test1::helper, NULL); 51 pthread_join(t1, NULL); 52 pthread_join(t2, NULL); 53 free(p1); 54 free(p2); 55 llvm_stop_multithreaded(); 56} 57#endif 58 59} // anonymous namespace 60