Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 1 | //===- llvm/Support/Mutex.h - Mutex Operating System Concept -----*- C++ -*-===// |
| 2 | // |
Andrew Walbran | 16937d0 | 2019-10-22 13:54:20 +0100 | [diff] [blame] | 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 |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file declares the llvm::sys::Mutex class. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #ifndef LLVM_SUPPORT_MUTEX_H |
| 14 | #define LLVM_SUPPORT_MUTEX_H |
| 15 | |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 16 | #include "llvm/Support/Threading.h" |
| 17 | #include <cassert> |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 18 | #include <mutex> |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 19 | |
| 20 | namespace llvm |
| 21 | { |
| 22 | namespace sys |
| 23 | { |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 24 | /// SmartMutex - A mutex with a compile time constant parameter that |
| 25 | /// indicates whether this mutex should become a no-op when we're not |
| 26 | /// running in multithreaded mode. |
| 27 | template<bool mt_only> |
| 28 | class SmartMutex { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 29 | std::recursive_mutex impl; |
| 30 | unsigned acquired = 0; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 31 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 32 | public: |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 33 | bool lock() { |
| 34 | if (!mt_only || llvm_is_multithreaded()) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 35 | impl.lock(); |
| 36 | return true; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 37 | } else { |
| 38 | // Single-threaded debugging code. This would be racy in |
| 39 | // multithreaded mode, but provides not sanity checks in single |
| 40 | // threaded mode. |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 41 | ++acquired; |
| 42 | return true; |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | bool unlock() { |
| 47 | if (!mt_only || llvm_is_multithreaded()) { |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 48 | impl.unlock(); |
| 49 | return true; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 50 | } else { |
| 51 | // Single-threaded debugging code. This would be racy in |
| 52 | // multithreaded mode, but provides not sanity checks in single |
| 53 | // threaded mode. |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 54 | assert(acquired && "Lock not acquired before release!"); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 55 | --acquired; |
| 56 | return true; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | bool try_lock() { |
| 61 | if (!mt_only || llvm_is_multithreaded()) |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 62 | return impl.try_lock(); |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 63 | else return true; |
| 64 | } |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 65 | }; |
| 66 | |
| 67 | /// Mutex - A standard, always enforced mutex. |
| 68 | typedef SmartMutex<false> Mutex; |
| 69 | |
Olivier Deprez | f4ef2d0 | 2021-04-20 13:36:24 +0200 | [diff] [blame] | 70 | template <bool mt_only> |
| 71 | using SmartScopedLock = std::lock_guard<SmartMutex<mt_only>>; |
Andrew Scull | 5e1ddfa | 2018-08-14 10:06:54 +0100 | [diff] [blame] | 72 | |
| 73 | typedef SmartScopedLock<false> ScopedLock; |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | #endif |