LLVM  9.0.0svn
Mutex.cpp
Go to the documentation of this file.
1 //===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements the llvm::sys::Mutex class.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/Support/Mutex.h"
14 #include "llvm/Config/config.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;
26 MutexImpl::MutexImpl( bool recursive) { }
28 bool MutexImpl::acquire() { return true; }
29 bool MutexImpl::release() { return true; }
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
44 MutexImpl::MutexImpl( bool recursive)
45  : data_(nullptr)
46 {
47  // Declare the pthread_mutex data structures
48  pthread_mutex_t* mutex =
49  static_cast<pthread_mutex_t*>(safe_malloc(sizeof(pthread_mutex_t)));
50 
51  pthread_mutexattr_t attr;
52 
53  // Initialize the mutex attributes
54  int errorcode = pthread_mutexattr_init(&attr);
55  assert(errorcode == 0); (void)errorcode;
56 
57  // Initialize the mutex as a recursive mutex, if requested, or normal
58  // otherwise.
59  int kind = ( recursive ? PTHREAD_MUTEX_RECURSIVE : PTHREAD_MUTEX_NORMAL );
60  errorcode = pthread_mutexattr_settype(&attr, kind);
61  assert(errorcode == 0);
62 
63  // Initialize the mutex
64  errorcode = pthread_mutex_init(mutex, &attr);
65  assert(errorcode == 0);
66 
67  // Destroy the attributes
68  errorcode = pthread_mutexattr_destroy(&attr);
69  assert(errorcode == 0);
70 
71  // Assign the data member
72  data_ = mutex;
73 }
74 
75 // Destruct a Mutex
77 {
78  pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
79  assert(mutex != nullptr);
80  pthread_mutex_destroy(mutex);
81  free(mutex);
82 }
83 
84 bool
86 {
87  pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
88  assert(mutex != nullptr);
89 
90  int errorcode = pthread_mutex_lock(mutex);
91  return errorcode == 0;
92 }
93 
94 bool
96 {
97  pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
98  assert(mutex != nullptr);
99 
100  int errorcode = pthread_mutex_unlock(mutex);
101  return errorcode == 0;
102 }
103 
104 bool
106 {
107  pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
108  assert(mutex != nullptr);
109 
110  int errorcode = pthread_mutex_trylock(mutex);
111  return errorcode == 0;
112 }
113 
114 }
115 
116 #elif defined(LLVM_ON_UNIX)
117 #include "Unix/Mutex.inc"
118 #elif defined( _WIN32)
119 #include "Windows/Mutex.inc"
120 #else
121 #warning Neither LLVM_ON_UNIX nor _WIN32 was set in Support/Mutex.cpp
122 #endif
123 #endif
bool acquire()
Attempts to unconditionally acquire the lock.
Definition: Mutex.cpp:85
This class represents lattice values for constants.
Definition: AllocatorList.h:23
bool release()
Attempts to release the lock.
Definition: Mutex.cpp:95
MutexImpl(bool recursive=true)
Initializes the lock but doesn&#39;t acquire it.
Definition: Mutex.cpp:44
LLVM_ATTRIBUTE_RETURNS_NONNULL void * safe_malloc(size_t Sz)
Definition: MemAlloc.h:25
~MutexImpl()
Releases and removes the lock Destructor.
Definition: Mutex.cpp:76
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
bool tryacquire()
Attempts to acquire the lock without blocking.
Definition: Mutex.cpp:105