add a new map
[oota-llvm.git] / include / llvm / Support / ThreadSupport-PThreads.h
1 //===-- llvm/Support/ThreadSupport-PThreads.h - PThreads support *- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines pthreads implementations of the generic threading
11 // mechanisms.  Users should never #include this file directly!
12 //
13 //===----------------------------------------------------------------------===//
14
15 // Users should never #include this file directly!  As such, no include guards
16 // are needed.
17
18 #ifndef LLVM_SUPPORT_THREADSUPPORT_H
19 #error "Code should not #include Support/ThreadSupport/PThreads.h directly!"
20 #endif
21
22 #include <pthread.h>
23
24 namespace llvm {
25
26   /// Mutex - This class allows user code to protect variables shared between
27   /// threads.  It implements a "recursive" mutex, to simplify user code.
28   ///
29   class Mutex {
30     pthread_mutex_t mutex;
31     Mutex(const Mutex &);           // DO NOT IMPLEMENT
32     void operator=(const Mutex &);  // DO NOT IMPLEMENT
33   public:
34     Mutex() {
35       // Initialize the mutex as a recursive mutex
36       pthread_mutexattr_t Attr;
37       int errorcode = pthread_mutexattr_init(&Attr);
38       assert(errorcode == 0);
39
40       errorcode = pthread_mutexattr_settype(&Attr, PTHREAD_MUTEX_RECURSIVE);
41       assert(errorcode == 0);
42       
43       errorcode = pthread_mutex_init(&mutex, &Attr);
44       assert(errorcode == 0);
45
46       errorcode = pthread_mutexattr_destroy(&Attr);
47       assert(errorcode == 0);
48     }
49
50     ~Mutex() {
51       int errorcode = pthread_mutex_destroy(&mutex);
52       assert(errorcode == 0);
53     }
54
55     void acquire () {
56       int errorcode = pthread_mutex_lock(&mutex);
57       assert(errorcode == 0);
58     }
59
60     void release () {
61       int errorcode = pthread_mutex_unlock(&mutex);
62       assert(errorcode == 0);
63     }
64   };
65 } // end namespace llvm