OpenShot Library | OpenShotAudio 0.2.2
juce_ReadWriteLock.h
1
2/** @weakgroup juce_core-threads
3 * @{
4 */
5/*
6 ==============================================================================
7
8 This file is part of the JUCE library.
9 Copyright (c) 2017 - ROLI Ltd.
10
11 JUCE is an open source library subject to commercial or open-source
12 licensing.
13
14 The code included in this file is provided under the terms of the ISC license
15 http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16 To use, copy, modify, and/or distribute this software for any purpose with or
17 without fee is hereby granted provided that the above copyright notice and
18 this permission notice appear in all copies.
19
20 JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21 EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22 DISCLAIMED.
23
24 ==============================================================================
25*/
26
27namespace juce
28{
29
30//==============================================================================
31/**
32 A critical section that allows multiple simultaneous readers.
33
34 Features of this type of lock are:
35
36 - Multiple readers can hold the lock at the same time, but only one writer
37 can hold it at once.
38 - Writers trying to gain the lock will be blocked until all readers and writers
39 have released it
40 - Readers trying to gain the lock while a writer is waiting to acquire it will be
41 blocked until the writer has obtained and released it
42 - If a thread already has a read lock and tries to obtain a write lock, it will succeed if
43 there are no other readers
44 - If a thread already has the write lock and tries to obtain a read lock, this will succeed.
45 - Recursive locking is supported.
46
47 @see ScopedReadLock, ScopedWriteLock, CriticalSection
48
49 @tags{Core}
50*/
52{
53public:
54 //==============================================================================
55 /**
56 Creates a ReadWriteLock object.
57 */
58 ReadWriteLock() noexcept;
59
60 /** Destructor.
61 If the object is deleted whilst locked, any subsequent behaviour is undefined.
62 */
63 ~ReadWriteLock() noexcept;
64
65 //==============================================================================
66 /** Locks this object for reading.
67
68 Multiple threads can simultaneously lock the object for reading, but if another
69 thread has it locked for writing, then this will block until it releases the lock.
70
71 @see exitRead, ScopedReadLock
72 */
73 void enterRead() const noexcept;
74
75 /** Tries to lock this object for reading.
76
77 Multiple threads can simultaneously lock the object for reading, but if another
78 thread has it locked for writing, then this will fail and return false.
79
80 @returns true if the lock is successfully gained.
81 @see exitRead, ScopedReadLock
82 */
83 bool tryEnterRead() const noexcept;
84
85 /** Releases the read-lock.
86
87 If the caller thread hasn't got the lock, this can have unpredictable results.
88
89 If the enterRead() method has been called multiple times by the thread, each
90 call must be matched by a call to exitRead() before other threads will be allowed
91 to take over the lock.
92
93 @see enterRead, ScopedReadLock
94 */
95 void exitRead() const noexcept;
96
97 //==============================================================================
98 /** Locks this object for writing.
99
100 This will block until any other threads that have it locked for reading or
101 writing have released their lock.
102
103 @see exitWrite, ScopedWriteLock
104 */
105 void enterWrite() const noexcept;
106
107 /** Tries to lock this object for writing.
108
109 This is like enterWrite(), but doesn't block - it returns true if it manages
110 to obtain the lock.
111
112 @returns true if the lock is successfully gained.
113 @see enterWrite
114 */
115 bool tryEnterWrite() const noexcept;
116
117 /** Releases the write-lock.
118
119 If the caller thread hasn't got the lock, this can have unpredictable results.
120
121 If the enterWrite() method has been called multiple times by the thread, each
122 call must be matched by a call to exit() before other threads will be allowed
123 to take over the lock.
124
125 @see enterWrite, ScopedWriteLock
126 */
127 void exitWrite() const noexcept;
128
129
130private:
131 //==============================================================================
132 SpinLock accessLock;
133 WaitableEvent readWaitEvent, writeWaitEvent;
134 mutable int numWaitingWriters = 0, numWriters = 0;
135 mutable Thread::ThreadID writerThreadId = {};
136
137 struct ThreadRecursionCount
138 {
139 Thread::ThreadID threadID;
140 int count;
141 };
142
143 mutable Array <ThreadRecursionCount> readerThreads;
144
145 bool tryEnterWriteInternal (Thread::ThreadID) const noexcept;
146
147 JUCE_DECLARE_NON_COPYABLE (ReadWriteLock)
148};
149
150} // namespace juce
151
152/** @}*/
A critical section that allows multiple simultaneous readers.
A simple spin-lock class that can be used as a simple, low-overhead mutex for uncontended situations.
Definition: juce_SpinLock.h:46
void * ThreadID
A value type used for thread IDs.
Definition: juce_Thread.h:308
Allows threads to wait for events triggered by other threads.
#define JUCE_API
This macro is added to all JUCE public class declarations.