1 //===- Mutex.cpp - Mutual Exclusion Lock ------------------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
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
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; }
34 #if defined(HAVE_PTHREAD_H) && defined(HAVE_PTHREAD_MUTEX_LOCK)
43 // Construct a Mutex using pthread calls
44 MutexImpl::MutexImpl( bool recursive
)
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
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
76 MutexImpl::~MutexImpl()
78 pthread_mutex_t
* mutex
= static_cast<pthread_mutex_t
*>(data_
);
79 assert(mutex
!= nullptr);
80 pthread_mutex_destroy(mutex
);
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;
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;
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"
121 #warning Neither LLVM_ON_UNIX nor _WIN32 was set in Support/Mutex.cpp