Add TAL-Reverb-II plugin to test
[juce-lv2.git] / juce / source / src / threads / juce_ReadWriteLock.h
blob7b729a742e2fd80d4d6ed28e6d6e1d77d9e4bdb6
1 /*
2 ==============================================================================
4 This file is part of the JUCE library - "Jules' Utility Class Extensions"
5 Copyright 2004-11 by Raw Material Software Ltd.
7 ------------------------------------------------------------------------------
9 JUCE can be redistributed and/or modified under the terms of the GNU General
10 Public License (Version 2), as published by the Free Software Foundation.
11 A copy of the license is included in the JUCE distribution, or can be found
12 online at www.gnu.org/licenses.
14 JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
15 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
18 ------------------------------------------------------------------------------
20 To release a closed-source product which uses JUCE, commercial licenses are
21 available: visit www.rawmaterialsoftware.com/juce for more information.
23 ==============================================================================
26 #ifndef __JUCE_READWRITELOCK_JUCEHEADER__
27 #define __JUCE_READWRITELOCK_JUCEHEADER__
29 #include "juce_CriticalSection.h"
30 #include "juce_SpinLock.h"
31 #include "juce_WaitableEvent.h"
32 #include "juce_Thread.h"
33 #include "../containers/juce_Array.h"
36 //==============================================================================
37 /**
38 A critical section that allows multiple simultaneous readers.
40 Features of this type of lock are:
42 - Multiple readers can hold the lock at the same time, but only one writer
43 can hold it at once.
44 - Writers trying to gain the lock will be blocked until all readers and writers
45 have released it
46 - Readers trying to gain the lock while a writer is waiting to acquire it will be
47 blocked until the writer has obtained and released it
48 - If a thread already has a read lock and tries to obtain a write lock, it will succeed if
49 there are no other readers
50 - If a thread already has the write lock and tries to obtain a read lock, this will succeed.
51 - Recursive locking is supported.
53 @see ScopedReadLock, ScopedWriteLock, CriticalSection
55 class JUCE_API ReadWriteLock
57 public:
58 //==============================================================================
59 /**
60 Creates a ReadWriteLock object.
62 ReadWriteLock() noexcept;
64 /** Destructor.
66 If the object is deleted whilst locked, any subsequent behaviour
67 is unpredictable.
69 ~ReadWriteLock() noexcept;
71 //==============================================================================
72 /** Locks this object for reading.
74 Multiple threads can simulaneously lock the object for reading, but if another
75 thread has it locked for writing, then this will block until it releases the
76 lock.
78 @see exitRead, ScopedReadLock
80 void enterRead() const noexcept;
82 /** Releases the read-lock.
84 If the caller thread hasn't got the lock, this can have unpredictable results.
86 If the enterRead() method has been called multiple times by the thread, each
87 call must be matched by a call to exitRead() before other threads will be allowed
88 to take over the lock.
90 @see enterRead, ScopedReadLock
92 void exitRead() const noexcept;
94 //==============================================================================
95 /** Locks this object for writing.
97 This will block until any other threads that have it locked for reading or
98 writing have released their lock.
100 @see exitWrite, ScopedWriteLock
102 void enterWrite() const noexcept;
104 /** Tries to lock this object for writing.
106 This is like enterWrite(), but doesn't block - it returns true if it manages
107 to obtain the lock.
109 @see enterWrite
111 bool tryEnterWrite() const noexcept;
113 /** Releases the write-lock.
115 If the caller thread hasn't got the lock, this can have unpredictable results.
117 If the enterWrite() method has been called multiple times by the thread, each
118 call must be matched by a call to exit() before other threads will be allowed
119 to take over the lock.
121 @see enterWrite, ScopedWriteLock
123 void exitWrite() const noexcept;
126 private:
127 //==============================================================================
128 SpinLock accessLock;
129 WaitableEvent waitEvent;
130 mutable int numWaitingWriters, numWriters;
131 mutable Thread::ThreadID writerThreadId;
132 mutable Array <Thread::ThreadID> readerThreads;
134 JUCE_DECLARE_NON_COPYABLE (ReadWriteLock);
138 #endif // __JUCE_READWRITELOCK_JUCEHEADER__