summaryrefslogtreecommitdiffstats
path: root/lldb/source/Host/windows/ProcessRunLock.cpp
blob: 25d034b605ea954f93caac90725783f499d94997 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#ifdef _WIN32

#include "lldb/Host/ProcessRunLock.h"
#include "lldb/Host/windows/windows.h"

namespace lldb_private {

    // Windows has slim read-writer lock support on Vista and higher, so we
    // will attempt to load the APIs.  If they exist, we will use them, and
    // if not, we will fall back on critical sections.  When we drop support
    // for XP, we can stop lazy-loading these APIs and just use them directly.
#if defined(__MINGW32__)
    // Taken from WinNT.h
    typedef struct _RTL_SRWLOCK {
        PVOID Ptr;
    } RTL_SRWLOCK, *PRTL_SRWLOCK;

    // Taken from WinBase.h
    typedef RTL_SRWLOCK SRWLOCK, *PSRWLOCK;
#endif


    typedef struct Win32RWLOCK
    {
        long int readlockcount;
        HANDLE writable;
        CRITICAL_SECTION writelock;
        long int writelocked;
    } Win32RWLOCK;

    typedef Win32RWLOCK* PWin32RWLOCK;

    static VOID (WINAPI *fpInitializeSRWLock)(PSRWLOCK lock) = NULL;
    static VOID (WINAPI *fpAcquireSRWLockExclusive)(PSRWLOCK lock) = NULL;
    static VOID (WINAPI *fpAcquireSRWLockShared)(PSRWLOCK lock) = NULL;
    static VOID (WINAPI *fpReleaseSRWLockExclusive)(PSRWLOCK lock) = NULL;
    static VOID (WINAPI *fpReleaseSRWLockShared)(PSRWLOCK lock) = NULL;
    static BOOL (WINAPI *fpTryAcquireSRWLockExclusive)(PSRWLOCK lock) = NULL;
    static BOOL (WINAPI *fpTryAcquireSRWLockShared)(PSRWLOCK lock) = NULL;

    static bool sHasSRW = false;

    static bool loadSRW()
    {
        static bool sChecked = false;
        if (!sChecked)
        {
            sChecked = true;
            return false;

            HMODULE hLib = ::LoadLibrary(TEXT("Kernel32"));
            if (hLib)
            {
                fpInitializeSRWLock =
                    (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
                    "InitializeSRWLock");
                fpAcquireSRWLockExclusive =
                    (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
                    "AcquireSRWLockExclusive");
                fpAcquireSRWLockShared =
                    (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
                    "AcquireSRWLockShared");
                fpReleaseSRWLockExclusive =
                    (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
                    "ReleaseSRWLockExclusive");
                fpReleaseSRWLockShared =
                    (VOID (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
                    "ReleaseSRWLockShared");
                fpTryAcquireSRWLockExclusive =
                    (BOOL (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
                    "TryAcquireSRWLockExclusive");
                fpTryAcquireSRWLockShared =
                    (BOOL (WINAPI *)(PSRWLOCK))::GetProcAddress(hLib,
                    "TryAcquireSRWLockShared");

                ::FreeLibrary(hLib);

                if (fpInitializeSRWLock != NULL) {
                    sHasSRW = true;
                }
            }
        }
        return sHasSRW;
    }

    ProcessRunLock::ProcessRunLock ()
        : m_running(false)
    {
        if (loadSRW())
        {
            m_rwlock = calloc(1, sizeof(SRWLOCK));
            fpInitializeSRWLock(static_cast<PSRWLOCK>(m_rwlock));
        }
        else
        {
            m_rwlock = calloc(1, sizeof(Win32RWLOCK));
            static_cast<PWin32RWLOCK>(m_rwlock)->readlockcount = 0;
            static_cast<PWin32RWLOCK>(m_rwlock)->writable = CreateEvent(NULL, true, true, NULL);
            InitializeCriticalSection(&static_cast<PWin32RWLOCK>(m_rwlock)->writelock);
        }
    }

    ProcessRunLock::~ProcessRunLock ()
    {
        if (!sHasSRW)
        {
            CloseHandle(static_cast<PWin32RWLOCK>(m_rwlock)->writable);
            DeleteCriticalSection(&static_cast<PWin32RWLOCK>(m_rwlock)->writelock);
        }
        free(m_rwlock);
    }

    bool ReadLock (lldb::rwlock_t rwlock)
    {
        if (sHasSRW)
        {
            fpAcquireSRWLockShared(static_cast<PSRWLOCK>(rwlock));
            return true;
        }
        else
        {
            EnterCriticalSection(&static_cast<PWin32RWLOCK>(rwlock)->writelock);
            InterlockedIncrement(&static_cast<PWin32RWLOCK>(rwlock)->readlockcount);
            ResetEvent(static_cast<PWin32RWLOCK>(rwlock)->writable);
            LeaveCriticalSection(&static_cast<PWin32RWLOCK>(rwlock)->writelock);
            return true;
        }
    }

    bool ProcessRunLock::ReadTryLock ()
    {
        ReadLock(m_rwlock);
        if (m_running == false)
            return true;
        ReadUnlock();
        return false;
    }

    bool ProcessRunLock::ReadUnlock ()
    {
        if (sHasSRW)
        {
            fpReleaseSRWLockShared(static_cast<PSRWLOCK>(m_rwlock));
            return true;
        }
        else
        {
            unsigned long int value = InterlockedDecrement(&static_cast<PWin32RWLOCK>(m_rwlock)->readlockcount);
            assert(((int)value) >= 0);
            if (value == 0)
                SetEvent(static_cast<PWin32RWLOCK>(m_rwlock)->writable);
            return true;
        }
    }

    bool WriteLock(lldb::rwlock_t rwlock)
    {
        if (sHasSRW)
        {
            fpAcquireSRWLockExclusive(static_cast<PSRWLOCK>(rwlock));
            return true;
        }
        else
        {
            EnterCriticalSection(&static_cast<PWin32RWLOCK>(rwlock)->writelock);
            WaitForSingleObject(static_cast<PWin32RWLOCK>(rwlock)->writable, INFINITE);
            int res = InterlockedExchange(&static_cast<PWin32RWLOCK>(rwlock)->writelocked, 1);
            assert(res == 0);
            return true;
        }
    }

    bool WriteTryLock(lldb::rwlock_t rwlock)
    {
        if (sHasSRW)
        {
            return fpTryAcquireSRWLockExclusive(static_cast<PSRWLOCK>(rwlock)) != 0;
        }
        else
        {
            if (TryEnterCriticalSection(&static_cast<PWin32RWLOCK>(rwlock)->writelock)) {
                if (WaitForSingleObject(static_cast<PWin32RWLOCK>(rwlock)->writable, 0)) {
                    LeaveCriticalSection(&static_cast<PWin32RWLOCK>(rwlock)->writelock);
                    return false;
                }
                int res = InterlockedExchange(&static_cast<PWin32RWLOCK>(rwlock)->writelocked, 1);
                assert(res == 0);
                return true;
            }
            return false;
        }
    }

    bool WriteUnlock(lldb::rwlock_t rwlock)
    {
        if (sHasSRW)
        {
            fpReleaseSRWLockExclusive(static_cast<PSRWLOCK>(rwlock));
            return true;
        }
        else
        {
            int res = InterlockedExchange(&static_cast<PWin32RWLOCK>(rwlock)->writelocked, 0);
            if (res == 1) {
                LeaveCriticalSection(&static_cast<PWin32RWLOCK>(rwlock)->writelock);
                return true;
            }
            return false;
        }
    }

    bool ProcessRunLock::SetRunning ()
    {
        WriteLock(m_rwlock);
        m_running = true;
        WriteUnlock(m_rwlock);
        return true;
    }

    bool ProcessRunLock::TrySetRunning ()
    {
        if (WriteTryLock(m_rwlock))
        {
            bool r = !m_running;
            m_running = true;
            WriteUnlock(m_rwlock);
            return r;
        }
        return false;
    }

    bool ProcessRunLock::SetStopped ()
    {
        WriteLock(m_rwlock);
        m_running = false;
        WriteUnlock(m_rwlock);
        return true;
    }
}

#endif
OpenPOWER on IntegriCloud