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