1 //===-- ProcessRunLock.h ----------------------------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef liblldb_ProcessRunLock_h_ 11 #define liblldb_ProcessRunLock_h_ 12 13 // C Includes 14 #include <stdint.h> 15 #include <time.h> 16 17 // C++ Includes 18 // Other libraries and framework includes 19 // Project includes 20 #include "lldb/lldb-defines.h" 21 22 //---------------------------------------------------------------------- 23 /// Enumerations for broadcasting. 24 //---------------------------------------------------------------------- 25 namespace lldb_private { 26 27 //---------------------------------------------------------------------- 28 /// @class ProcessRunLock ProcessRunLock.h "lldb/Host/ProcessRunLock.h" 29 /// @brief A class used to prevent the process from starting while other 30 /// threads are accessing its data, and prevent access to its data while 31 /// it is running. 32 //---------------------------------------------------------------------- 33 34 class ProcessRunLock { 35 public: 36 ProcessRunLock(); 37 ~ProcessRunLock(); 38 39 bool ReadTryLock(); 40 bool ReadUnlock(); 41 bool SetRunning(); 42 bool TrySetRunning(); 43 bool SetStopped(); 44 45 class ProcessRunLocker { 46 public: 47 ProcessRunLocker() : m_lock(nullptr) {} 48 49 ~ProcessRunLocker() { Unlock(); } 50 51 // Try to lock the read lock, but only do so if there are no writers. 52 bool TryLock(ProcessRunLock *lock) { 53 if (m_lock) { 54 if (m_lock == lock) 55 return true; // We already have this lock locked 56 else 57 Unlock(); 58 } 59 if (lock) { 60 if (lock->ReadTryLock()) { 61 m_lock = lock; 62 return true; 63 } 64 } 65 return false; 66 } 67 68 protected: 69 void Unlock() { 70 if (m_lock) { 71 m_lock->ReadUnlock(); 72 m_lock = nullptr; 73 } 74 } 75 76 ProcessRunLock *m_lock; 77 78 private: 79 DISALLOW_COPY_AND_ASSIGN(ProcessRunLocker); 80 }; 81 82 protected: 83 lldb::rwlock_t m_rwlock; 84 bool m_running; 85 86 private: 87 DISALLOW_COPY_AND_ASSIGN(ProcessRunLock); 88 }; 89 90 } // namespace lldb_private 91 92 #endif // liblldb_ProcessRunLock_h_ 93