Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:44:32

0001 //===- llvm/Support/Mutex.h - Mutex Operating System Concept -----*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file declares the llvm::sys::Mutex class.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_SUPPORT_MUTEX_H
0014 #define LLVM_SUPPORT_MUTEX_H
0015 
0016 #include "llvm/Support/Threading.h"
0017 #include <cassert>
0018 #include <mutex>
0019 
0020 namespace llvm
0021 {
0022   namespace sys
0023   {
0024     /// SmartMutex - A mutex with a compile time constant parameter that
0025     /// indicates whether this mutex should become a no-op when we're not
0026     /// running in multithreaded mode.
0027     template<bool mt_only>
0028     class SmartMutex {
0029       std::recursive_mutex impl;
0030       unsigned acquired = 0;
0031 
0032     public:
0033       bool lock() {
0034         if (!mt_only || llvm_is_multithreaded()) {
0035           impl.lock();
0036           return true;
0037         }
0038         // Single-threaded debugging code.  This would be racy in
0039         // multithreaded mode, but provides not basic checks in single
0040         // threaded mode.
0041         ++acquired;
0042         return true;
0043       }
0044 
0045       bool unlock() {
0046         if (!mt_only || llvm_is_multithreaded()) {
0047           impl.unlock();
0048           return true;
0049         }
0050         // Single-threaded debugging code.  This would be racy in
0051         // multithreaded mode, but provides not basic checks in single
0052         // threaded mode.
0053         assert(acquired && "Lock not acquired before release!");
0054         --acquired;
0055         return true;
0056       }
0057 
0058       bool try_lock() {
0059         if (!mt_only || llvm_is_multithreaded())
0060           return impl.try_lock();
0061         return true;
0062       }
0063     };
0064 
0065     /// Mutex - A standard, always enforced mutex.
0066     typedef SmartMutex<false> Mutex;
0067 
0068     template <bool mt_only>
0069     using SmartScopedLock = std::lock_guard<SmartMutex<mt_only>>;
0070 
0071     typedef SmartScopedLock<false> ScopedLock;
0072   }
0073 }
0074 
0075 #endif