Welcome to mirror list, hosted at ThFree Co, Russian Federation.

RWLock.cpp « Runtime « Native « src - github.com/mono/corert.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: efcb1c1ab3b4c73b8ed019a7924bb348b9eee66d (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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//
// Copyright (c) Microsoft Corporation.  All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
//

//
// RWLock.cpp -- adapted from CLR SimpleRWLock.cpp
//
#include "common.h"
#include "CommonTypes.h"
#include "CommonMacros.h"
#include "daccess.h"
#include "PalRedhawkCommon.h"
#include "PalRedhawk.h"
#include "assert.h"
#include "slist.h"
#include "gcrhinterface.h"
#include "varint.h"
#include "regdisplay.h"
#include "StackFrameIterator.h"
#include "thread.h"
#include "holder.h"
#include "Crst.h"
#include "event.h"
#include "RWLock.h"
#include "threadstore.h"
#include "RuntimeInstance.h"

// Configurable constants used across our spin locks
// Initialization here is necessary so that we have meaningful values before the runtime is started
// These initial values were selected to match the defaults, but anything reasonable is close enough
struct SpinConstants
{
    UInt32 uInitialDuration;
    UInt32 uMaximumDuration;
    UInt32 uBackoffFactor;
    UInt32 uRepetitions;
} g_SpinConstants = { 
    50,        // dwInitialDuration 
    40000,     // dwMaximumDuration - ideally (20000 * max(2, numProc))
    3,         // dwBackoffFactor
    10         // dwRepetitions
};

ReaderWriterLock::ReadHolder::ReadHolder(ReaderWriterLock * pLock, bool fAcquireLock) :
    m_pLock(pLock)
{
#ifndef DACCESS_COMPILE
    m_fLockAcquired = fAcquireLock;
    if (fAcquireLock)
        m_pLock->AcquireReadLock();
#else
    UNREFERENCED_PARAMETER(fAcquireLock);
#endif // !DACCESS_COMPILE
}

ReaderWriterLock::ReadHolder::~ReadHolder()
{
#ifndef DACCESS_COMPILE
    if (m_fLockAcquired)
        m_pLock->ReleaseReadLock();
#endif // !DACCESS_COMPILE
}

ReaderWriterLock::WriteHolder::WriteHolder(ReaderWriterLock * pLock, bool fAcquireLock) :
    m_pLock(pLock)
{
#ifndef DACCESS_COMPILE
    m_fLockAcquired = fAcquireLock;
    if (fAcquireLock)
        m_pLock->AcquireWriteLock();
#else
    UNREFERENCED_PARAMETER(fAcquireLock);
#endif // !DACCESS_COMPILE
}

ReaderWriterLock::WriteHolder::~WriteHolder()
{
#ifndef DACCESS_COMPILE
    if (m_fLockAcquired)
        m_pLock->ReleaseWriteLock();
#endif // !DACCESS_COMPILE
}

ReaderWriterLock::ReaderWriterLock() : 
    m_RWLock(0)
#if 0
    , m_WriterWaiting(false)
#endif
{
    m_spinCount = (
#ifndef DACCESS_COMPILE
        (PalGetProcessCpuCount() == 1) ? 0 : 
#endif
        4000);
}


#ifndef DACCESS_COMPILE

// Attempt to take the read lock, but do not wait if a writer has the lock.
// Release the lock if successfully acquired.  Returns true if the lock was
// taken and released.  Returns false if a writer had the lock.  
//
// BEWARE: Because this method returns after releasing the lock, you can't 
// infer the state of the lock based on the return value.  This is currently
// only used to detect if a suspended thread owns the write lock to prevent
// deadlock with the Hijack logic during GC suspension.
//
bool ReaderWriterLock::DangerousTryPulseReadLock()
{
    if (TryAcquireReadLock())
    {
        ReleaseReadLock();
        return true;
    }
    return false;
}

bool ReaderWriterLock::TryAcquireReadLock()
{
    Int32 RWLock;

    do 
    {
        RWLock = m_RWLock;
        if (RWLock == -1)
            return false;
        ASSERT(RWLock >= 0);
    }
    while (RWLock != PalInterlockedCompareExchange(&m_RWLock, RWLock+1, RWLock));

    return true;
}

void ReaderWriterLock::AcquireReadLock()
{
    if (TryAcquireReadLock())
        return;

    AcquireReadLockWorker();
}

void ReaderWriterLock::AcquireReadLockWorker()
{
    UInt32 uSwitchCount = 0;

    for (;;)
    {
#if 0
        // @TODO: Validate that we never re-enter the reader lock from a thread that
        // already holds it.  This scenario will deadlock if there are outstanding
        // writers.

        // prevent writers from being starved. This assumes that writers are rare and 
        // dont hold the lock for a long time. 
        while (m_WriterWaiting)
        {
            Int32 spinCount = m_spinCount;
            while (spinCount > 0) {
                spinCount--;
                PalYieldProcessor();
            }
            __SwitchToThread(0, ++uSwitchCount);
        }
#endif

        if (TryAcquireReadLock())
            return;

        UInt32 uDelay = g_SpinConstants.uInitialDuration;
        do
        {
            if (TryAcquireReadLock())
                return;

            if (g_SystemInfo.dwNumberOfProcessors <= 1)
                break;

            // Delay by approximately 2*i clock cycles (Pentium III).
            // This is brittle code - future processors may of course execute this
            // faster or slower, and future code generators may eliminate the loop altogether.
            // The precise value of the delay is not critical, however, and I can't think
            // of a better way that isn't machine-dependent - petersol.
            int sum = 0;
            for (int delayCount = uDelay; --delayCount; ) 
            {
                sum += delayCount;
                PalYieldProcessor();           // indicate to the processor that we are spining 
            }
            if (sum == 0)
            {
                // never executed, just to fool the compiler into thinking sum is live here,
                // so that it won't optimize away the loop.
                static char dummy;
                dummy++;
            }
            // exponential backoff: wait a factor longer in the next iteration
            uDelay *= g_SpinConstants.uBackoffFactor;
        }
        while (uDelay < g_SpinConstants.uMaximumDuration);

        __SwitchToThread(0, ++uSwitchCount);
    }
}

void ReaderWriterLock::ReleaseReadLock()
{
    Int32 RWLock;
    RWLock = PalInterlockedDecrement(&m_RWLock);
    ASSERT(RWLock >= 0);
}


bool ReaderWriterLock::TryAcquireWriteLock()
{
    Int32 RWLock = PalInterlockedCompareExchange(&m_RWLock, -1, 0);

    ASSERT(RWLock >= 0 || RWLock == -1);
    
    if (RWLock)
        return false;

#if 0
    m_WriterWaiting = false;
#endif

    return true;
}

void ReaderWriterLock::AcquireWriteLock()
{
    UInt32 uSwitchCount = 0;

    for (;;)
    {
        if (TryAcquireWriteLock())
            return;

#if 0
        // Set the writer waiting word, if not already set, to notify potential readers to wait.
        m_WriterWaiting = true;
#endif

        UInt32 uDelay = g_SpinConstants.uInitialDuration;
        do
        {
            if (TryAcquireWriteLock())
                return;

            if (g_SystemInfo.dwNumberOfProcessors <= 1)
            {
                break;
            }
            // Delay by approximately 2*i clock cycles (Pentium III).
            // This is brittle code - future processors may of course execute this
            // faster or slower, and future code generators may eliminate the loop altogether.
            // The precise value of the delay is not critical, however, and I can't think
            // of a better way that isn't machine-dependent - petersol.
            int sum = 0;
            for (int delayCount = uDelay; --delayCount; ) 
            {
                sum += delayCount;
                PalYieldProcessor();           // indicate to the processor that we are spining 
            }
            if (sum == 0)
            {
                // never executed, just to fool the compiler into thinking sum is live here,
                // so that it won't optimize away the loop.
                static char dummy;
                dummy++;
            }
            // exponential backoff: wait a factor longer in the next iteration
            uDelay *= g_SpinConstants.uBackoffFactor;
        }
        while (uDelay < g_SpinConstants.uMaximumDuration);

        __SwitchToThread(0, ++uSwitchCount);
    }
}

void ReaderWriterLock::ReleaseWriteLock()
{
    Int32 RWLock;
    RWLock = PalInterlockedExchange(&m_RWLock, 0);
    ASSERT(RWLock == -1);
}
#endif // DACCESS_COMPILE