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