[llvm-exegesis] [NFC] Fixing typo.
[llvm-complete.git] / lib / Support / Mutex.cpp
blob69b7b8126ab1cef22ad3c6880601cfaec4b81603
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.
11 //===----------------------------------------------------------------------===//
13 #include "llvm/Support/Mutex.h"
14 #include "llvm/Config/config.h"
15 #include "llvm/Support/ErrorHandling.h"
17 //===----------------------------------------------------------------------===//
18 //=== WARNING: Implementation here must contain only TRULY operating system
19 //=== independent code.
20 //===----------------------------------------------------------------------===//
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) { }
27 MutexImpl::~MutexImpl() { }
28 bool MutexImpl::acquire() { return true; }
29 bool MutexImpl::release() { return true; }
30 bool MutexImpl::tryacquire() { return true; }
32 #else
34 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
36 #include <cassert>
37 #include <pthread.h>
38 #include <stdlib.h>
40 namespace llvm {
41 using namespace sys;
43 // Construct a Mutex using pthread calls
44 MutexImpl::MutexImpl( bool recursive)
45 : data_(nullptr)
47 // Declare the pthread_mutex data structures
48 pthread_mutex_t* mutex =
49 static_cast<pthread_mutex_t*>(safe_malloc(sizeof(pthread_mutex_t)));
51 pthread_mutexattr_t attr;
53 // Initialize the mutex attributes
54 int errorcode = pthread_mutexattr_init(&attr);
55 assert(errorcode == 0); (void)errorcode;
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);
63 // Initialize the mutex
64 errorcode = pthread_mutex_init(mutex, &attr);
65 assert(errorcode == 0);
67 // Destroy the attributes
68 errorcode = pthread_mutexattr_destroy(&attr);
69 assert(errorcode == 0);
71 // Assign the data member
72 data_ = mutex;
75 // Destruct a Mutex
76 MutexImpl::~MutexImpl()
78 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
79 assert(mutex != nullptr);
80 pthread_mutex_destroy(mutex);
81 free(mutex);
84 bool
85 MutexImpl::acquire()
87 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
88 assert(mutex != nullptr);
90 int errorcode = pthread_mutex_lock(mutex);
91 return errorcode == 0;
94 bool
95 MutexImpl::release()
97 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
98 assert(mutex != nullptr);
100 int errorcode = pthread_mutex_unlock(mutex);
101 return errorcode == 0;
104 bool
105 MutexImpl::tryacquire()
107 pthread_mutex_t* mutex = static_cast<pthread_mutex_t*>(data_);
108 assert(mutex != nullptr);
110 int errorcode = pthread_mutex_trylock(mutex);
111 return errorcode == 0;
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