Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:42:49

0001 //===-- ProcessRunLock.h ----------------------------------------*- 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 #ifndef LLDB_HOST_PROCESSRUNLOCK_H
0010 #define LLDB_HOST_PROCESSRUNLOCK_H
0011 
0012 #include <cstdint>
0013 #include <ctime>
0014 
0015 #include "lldb/lldb-defines.h"
0016 
0017 /// Enumerations for broadcasting.
0018 namespace lldb_private {
0019 
0020 /// \class ProcessRunLock ProcessRunLock.h "lldb/Host/ProcessRunLock.h"
0021 /// A class used to prevent the process from starting while other
0022 /// threads are accessing its data, and prevent access to its data while it is
0023 /// running.
0024 
0025 class ProcessRunLock {
0026 public:
0027   ProcessRunLock();
0028   ~ProcessRunLock();
0029 
0030   bool ReadTryLock();
0031   bool ReadUnlock();
0032   bool SetRunning();
0033   bool TrySetRunning();
0034   bool SetStopped();
0035 
0036   class ProcessRunLocker {
0037   public:
0038     ProcessRunLocker() = default;
0039 
0040     ~ProcessRunLocker() { Unlock(); }
0041 
0042     // Try to lock the read lock, but only do so if there are no writers.
0043     bool TryLock(ProcessRunLock *lock) {
0044       if (m_lock) {
0045         if (m_lock == lock)
0046           return true; // We already have this lock locked
0047         else
0048           Unlock();
0049       }
0050       if (lock) {
0051         if (lock->ReadTryLock()) {
0052           m_lock = lock;
0053           return true;
0054         }
0055       }
0056       return false;
0057     }
0058 
0059   protected:
0060     void Unlock() {
0061       if (m_lock) {
0062         m_lock->ReadUnlock();
0063         m_lock = nullptr;
0064       }
0065     }
0066 
0067     ProcessRunLock *m_lock = nullptr;
0068 
0069   private:
0070     ProcessRunLocker(const ProcessRunLocker &) = delete;
0071     const ProcessRunLocker &operator=(const ProcessRunLocker &) = delete;
0072   };
0073 
0074 protected:
0075   lldb::rwlock_t m_rwlock;
0076   bool m_running = false;
0077 
0078 private:
0079   ProcessRunLock(const ProcessRunLock &) = delete;
0080   const ProcessRunLock &operator=(const ProcessRunLock &) = delete;
0081 };
0082 
0083 } // namespace lldb_private
0084 
0085 #endif // LLDB_HOST_PROCESSRUNLOCK_H