1254721Semaste//===-- ProcessRunLock.h ----------------------------------------*- C++ -*-===// 2254721Semaste// 3254721Semaste// The LLVM Compiler Infrastructure 4254721Semaste// 5254721Semaste// This file is distributed under the University of Illinois Open Source 6254721Semaste// License. See LICENSE.TXT for details. 7254721Semaste// 8254721Semaste//===----------------------------------------------------------------------===// 9254721Semaste 10254721Semaste#ifndef liblldb_ProcessRunLock_h_ 11254721Semaste#define liblldb_ProcessRunLock_h_ 12254721Semaste#if defined(__cplusplus) 13254721Semaste 14263363Semaste#include "lldb/lldb-defines.h" 15254721Semaste#include "lldb/Host/Mutex.h" 16254721Semaste#include "lldb/Host/Condition.h" 17254721Semaste#include <stdint.h> 18254721Semaste#include <time.h> 19254721Semaste 20254721Semaste//---------------------------------------------------------------------- 21254721Semaste/// Enumerations for broadcasting. 22254721Semaste//---------------------------------------------------------------------- 23254721Semastenamespace lldb_private { 24254721Semaste 25254721Semaste//---------------------------------------------------------------------- 26254721Semaste/// @class ProcessRunLock ProcessRunLock.h "lldb/Host/ProcessRunLock.h" 27254721Semaste/// @brief A class used to prevent the process from starting while other 28254721Semaste/// threads are accessing its data, and prevent access to its data while 29254721Semaste/// it is running. 30254721Semaste//---------------------------------------------------------------------- 31254721Semaste 32254721Semasteclass ProcessRunLock 33254721Semaste{ 34254721Semastepublic: 35263363Semaste ProcessRunLock(); 36263363Semaste ~ProcessRunLock(); 37263363Semaste bool ReadTryLock (); 38263363Semaste bool ReadUnlock (); 39263363Semaste bool SetRunning (); 40263363Semaste bool TrySetRunning (); 41263363Semaste bool SetStopped (); 42263363Semastepublic: 43254721Semaste class ProcessRunLocker 44254721Semaste { 45254721Semaste public: 46254721Semaste ProcessRunLocker () : 47254721Semaste m_lock (NULL) 48254721Semaste { 49254721Semaste } 50254721Semaste 51254721Semaste ~ProcessRunLocker() 52254721Semaste { 53254721Semaste Unlock(); 54254721Semaste } 55254721Semaste 56254721Semaste // Try to lock the read lock, but only do so if there are no writers. 57254721Semaste bool 58254721Semaste TryLock (ProcessRunLock *lock) 59254721Semaste { 60254721Semaste if (m_lock) 61254721Semaste { 62254721Semaste if (m_lock == lock) 63254721Semaste return true; // We already have this lock locked 64254721Semaste else 65254721Semaste Unlock(); 66254721Semaste } 67254721Semaste if (lock) 68254721Semaste { 69254721Semaste if (lock->ReadTryLock()) 70254721Semaste { 71254721Semaste m_lock = lock; 72254721Semaste return true; 73254721Semaste } 74254721Semaste } 75254721Semaste return false; 76254721Semaste } 77254721Semaste 78254721Semaste protected: 79254721Semaste void 80254721Semaste Unlock () 81254721Semaste { 82254721Semaste if (m_lock) 83254721Semaste { 84254721Semaste m_lock->ReadUnlock(); 85254721Semaste m_lock = NULL; 86254721Semaste } 87254721Semaste } 88254721Semaste 89254721Semaste ProcessRunLock *m_lock; 90254721Semaste private: 91254721Semaste DISALLOW_COPY_AND_ASSIGN(ProcessRunLocker); 92254721Semaste }; 93254721Semaste 94254721Semasteprotected: 95263363Semaste lldb::rwlock_t m_rwlock; 96254721Semaste bool m_running; 97254721Semasteprivate: 98254721Semaste DISALLOW_COPY_AND_ASSIGN(ProcessRunLock); 99254721Semaste}; 100254721Semaste 101254721Semaste} // namespace lldb_private 102254721Semaste 103254721Semaste#endif // #if defined(__cplusplus) 104254721Semaste#endif // #ifndef liblldb_ProcessRunLock_h_ 105