• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===//
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 //
10 // This file implements the llvm::sys::Mutex class.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Config/config.h"
15 #include "llvm/Support/Mutex.h"
16 
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only TRULY operating system
19 //===          independent code.
20 //===----------------------------------------------------------------------===//
21 
22 #if !defined(LLVM_ENABLE_THREADS) || LLVM_ENABLE_THREADS == 0
23 // Define all methods as no-ops if threading is explicitly disabled
24 namespace llvm {
25 using namespace sys;
MutexImpl(bool recursive)26 MutexImpl::MutexImpl( bool recursive) { }
~MutexImpl()27 MutexImpl::~MutexImpl() { }
acquire()28 bool MutexImpl::acquire() { return true; }
release()29 bool MutexImpl::release() { return true; }
tryacquire()30 bool MutexImpl::tryacquire() { return true; }
31 }
32 #else
33 
34 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
35 
36 #include <cassert>
37 #include <pthread.h>
38 #include <stdlib.h>
39 
40 namespace llvm {
41 using namespace sys;
42 
43 // Construct a Mutex using pthread calls
MutexImpl(bool recursive)44 MutexImpl::MutexImpl( bool recursive)
45   : data_(0)
46 {
47   // Declare the pthread_mutex data structures
48   pthread_mutex_t* mutex =
49     static_cast<pthread_mutex_t*>(malloc(sizeof(pthread_mutex_t)));
50   pthread_mutexattr_t attr;
51 
52   // Initialize the mutex attributes
53   int errorcode = pthread_mutexattr_init(&attr);
54   assert(errorcode == 0); (void)errorcode;
55 
56   // Initialize the mutex as a recursive mutex, if requested, or normal
57   // otherwise.
58   int kind = ( recursive  ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL );
59   errorcode = pthread_mutexattr_settype(&attr, kind);
60   assert(errorcode == 0);
61 
62 #if !defined(__FreeBSD__) && !defined(__OpenBSD__) && !defined(__NetBSD__) && !defined(__DragonFly__)
63   // Make it a process local mutex
64   errorcode = pthread_mutexattr_setpshared(&attr, PTHREAD_PROCESS_PRIVATE);
65   assert(errorcode == 0);
66 #endif
67 
68   // Initialize the mutex
69   errorcode = pthread_mutex_init(mutex, &attr);
70   assert(errorcode == 0);
71 
72   // Destroy the attributes
73   errorcode = pthread_mutexattr_destroy(&attr);
74   assert(errorcode == 0);
75 
76   // Assign the data member
77   data_ = mutex;
78 }
79 
80 // Destruct a Mutex
~MutexImpl()81 MutexImpl::~MutexImpl()
82 {
83   pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
84   assert(mutex != 0);
85   pthread_mutex_destroy(mutex);
86   free(mutex);
87 }
88 
89 bool
acquire()90 MutexImpl::acquire()
91 {
92   pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
93   assert(mutex != 0);
94 
95   int errorcode = pthread_mutex_lock(mutex);
96   return errorcode == 0;
97 }
98 
99 bool
release()100 MutexImpl::release()
101 {
102   pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
103   assert(mutex != 0);
104 
105   int errorcode = pthread_mutex_unlock(mutex);
106   return errorcode == 0;
107 }
108 
109 bool
tryacquire()110 MutexImpl::tryacquire()
111 {
112   pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
113   assert(mutex != 0);
114 
115   int errorcode = pthread_mutex_trylock(mutex);
116   return errorcode == 0;
117 }
118 
119 }
120 
121 #elif defined(LLVM_ON_UNIX)
122 #include "Unix/Mutex.inc"
123 #elif defined( LLVM_ON_WIN32)
124 #include "Windows/Mutex.inc"
125 #else
126 #warning Neither LLVM_ON_UNIX nor LLVM_ON_WIN32 was set in Support/Mutex.cpp
127 #endif
128 #endif
129