blob: 6a3939cae70a9f70be5e47e2ec8a8deaafbc6abf (
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
|
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
class ReaderWriterLock
{
volatile Int32 m_RWLock; // lock used for R/W synchronization
Int32 m_spinCount; // spin count for a reader waiting for a writer to release the lock
bool m_fBlockOnGc; // True if the spinning writers should block when GC is in progress
#if 0
// used to prevent writers from being starved by readers
// we currently do not prevent writers from starving readers since writers
// are supposed to be rare.
bool m_WriterWaiting;
#endif
bool TryAcquireReadLock();
bool TryAcquireWriteLock();
public:
class ReadHolder
{
ReaderWriterLock * m_pLock;
bool m_fLockAcquired;
public:
ReadHolder(ReaderWriterLock * pLock, bool fAcquireLock = true);
~ReadHolder();
};
class WriteHolder
{
ReaderWriterLock * m_pLock;
bool m_fLockAcquired;
public:
WriteHolder(ReaderWriterLock * pLock, bool fAcquireLock = true);
~WriteHolder();
};
ReaderWriterLock(bool fBlockOnGc = false);
void AcquireReadLock();
void ReleaseReadLock();
bool DangerousTryPulseReadLock();
protected:
void AcquireWriteLock();
void ReleaseWriteLock();
void AcquireReadLockWorker();
};
|