1 //===-- llvm/Support/ThreadSupport-PThreads.h - PThreads support *- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines pthreads implementations of the generic threading
11 // mechanisms. Users should never #include this file directly!
13 //===----------------------------------------------------------------------===//
15 // Users should never #include this file directly! As such, no include guards
18 #ifndef LLVM_SUPPORT_THREADSUPPORT_H
19 #error "Code should not #include Support/ThreadSupport/PThreads.h directly!"
26 /// Mutex - This class allows user code to protect variables shared between
27 /// threads. It implements a "recursive" mutex, to simplify user code.
30 pthread_mutex_t mutex;
31 Mutex(const Mutex &); // DO NOT IMPLEMENT
32 void operator=(const Mutex &); // DO NOT IMPLEMENT
35 // Initialize the mutex as a recursive mutex
36 pthread_mutexattr_t Attr;
37 int errorcode = pthread_mutexattr_init(&Attr);
38 assert(errorcode == 0);
40 errorcode = pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
41 assert(errorcode == 0);
43 errorcode = pthread_mutex_init(&mutex, &Attr);
44 assert(errorcode == 0);
46 errorcode = pthread_mutexattr_destroy(&Attr);
47 assert(errorcode == 0);
51 int errorcode = pthread_mutex_destroy(&mutex);
52 assert(errorcode == 0);
56 int errorcode = pthread_mutex_lock(&mutex);
57 assert(errorcode == 0);
61 int errorcode = pthread_mutex_unlock(&mutex);
62 assert(errorcode == 0);
65 } // end namespace llvm