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 #include <stdint.h> 14 #include <time.h> 15 16 #include "lldb/lldb-defines.h" 17 18 //---------------------------------------------------------------------- 19 /// Enumerations for broadcasting. 20 //---------------------------------------------------------------------- 21 namespace lldb_private { 22 23 //---------------------------------------------------------------------- 24 /// @class ProcessRunLock ProcessRunLock.h "lldb/Host/ProcessRunLock.h" 25 /// A class used to prevent the process from starting while other 26 /// threads are accessing its data, and prevent access to its data while it is 27 /// running. 28 //---------------------------------------------------------------------- 29 30 class ProcessRunLock { 31 public: 32 ProcessRunLock(); 33 ~ProcessRunLock(); 34 35 bool ReadTryLock(); 36 bool ReadUnlock(); 37 bool SetRunning(); 38 bool TrySetRunning(); 39 bool SetStopped(); 40 41 class ProcessRunLocker { 42 public: ProcessRunLocker()43 ProcessRunLocker() : m_lock(nullptr) {} 44 ~ProcessRunLocker()45 ~ProcessRunLocker() { Unlock(); } 46 47 // Try to lock the read lock, but only do so if there are no writers. TryLock(ProcessRunLock * lock)48 bool TryLock(ProcessRunLock *lock) { 49 if (m_lock) { 50 if (m_lock == lock) 51 return true; // We already have this lock locked 52 else 53 Unlock(); 54 } 55 if (lock) { 56 if (lock->ReadTryLock()) { 57 m_lock = lock; 58 return true; 59 } 60 } 61 return false; 62 } 63 64 protected: Unlock()65 void Unlock() { 66 if (m_lock) { 67 m_lock->ReadUnlock(); 68 m_lock = nullptr; 69 } 70 } 71 72 ProcessRunLock *m_lock; 73 74 private: 75 DISALLOW_COPY_AND_ASSIGN(ProcessRunLocker); 76 }; 77 78 protected: 79 lldb::rwlock_t m_rwlock; 80 bool m_running; 81 82 private: 83 DISALLOW_COPY_AND_ASSIGN(ProcessRunLock); 84 }; 85 86 } // namespace lldb_private 87 88 #endif // liblldb_ProcessRunLock_h_ 89