JUCE-7.0.12-0-g4f43011b96 JUCE-7.0.12-0-g4f43011b96
JUCE — C++ application framework with suport for VST, VST3, LV2 audio plug-ins

« « « Anklang Documentation
Loading...
Searching...
No Matches
flock.h
Go to the documentation of this file.
1 //------------------------------------------------------------------------
2// Project : SDK Base
3// Version : 1.0
4//
5// Category : Helpers
6// Filename : base/thread/include/flock.h
7// Created by : Steinberg, 1995
8// Description : locks
9//
10//-----------------------------------------------------------------------------
11// LICENSE
12// (c) 2023, Steinberg Media Technologies GmbH, All Rights Reserved
13//-----------------------------------------------------------------------------
14// Redistribution and use in source and binary forms, with or without modification,
15// are permitted provided that the following conditions are met:
16//
17// * Redistributions of source code must retain the above copyright notice,
18// this list of conditions and the following disclaimer.
19// * Redistributions in binary form must reproduce the above copyright notice,
20// this list of conditions and the following disclaimer in the documentation
21// and/or other materials provided with the distribution.
22// * Neither the name of the Steinberg Media Technologies nor the names of its
23// contributors may be used to endorse or promote products derived from this
24// software without specific prior written permission.
25//
26// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
27// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
29// IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
30// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
31// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
34// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
35// OF THE POSSIBILITY OF SUCH DAMAGE.
36//-----------------------------------------------------------------------------
37
38//----------------------------------------------------------------------------------
42//----------------------------------------------------------------------------------
43#pragma once
44
45#include "base/source/fobject.h"
47
48#if SMTG_PTHREADS
49#include <pthread.h>
50
51#elif SMTG_OS_WINDOWS
52struct CRITSECT // CRITICAL_SECTION
53{
54 void* DebugInfo; // PRTL_CRITICAL_SECTION_DEBUG DebugInfo;
55 Steinberg::int32 LockCount; // LONG LockCount;
56 Steinberg::int32 RecursionCount; // LONG RecursionCount;
57 void* OwningThread; // HANDLE OwningThread
58 void* LockSemaphore; // HANDLE LockSemaphore
59 Steinberg::int32 SpinCount; // ULONG_PTR SpinCount
60};
61#endif
62
63namespace Steinberg {
64namespace Base {
65namespace Thread {
66
67//------------------------------------------------------------------------
70//------------------------------------------------------------------------
71struct ILock
72{
73//------------------------------------------------------------------------
74 virtual ~ILock () {}
75
77 virtual void lock () = 0;
78
80 virtual void unlock () = 0;
81
83 virtual bool trylock () = 0;
84//------------------------------------------------------------------------
85};
86
87//------------------------------------------------------------------------
90//------------------------------------------------------------------------
91class FLock : public ILock
92{
93public:
94//------------------------------------------------------------------------
95
99 FLock (const char8* name = "FLock");
100
102 ~FLock () SMTG_OVERRIDE;
103
104 //-- ILock -----------------------------------------------------------
105 void lock () SMTG_OVERRIDE;
106 void unlock () SMTG_OVERRIDE;
107 bool trylock () SMTG_OVERRIDE;
108
109//------------------------------------------------------------------------
110protected:
111#if SMTG_PTHREADS
112 pthread_mutex_t mutex;
113
114#elif SMTG_OS_WINDOWS
115 CRITSECT section;
116#endif
117};
118
119//------------------------------------------------------------------------
122//------------------------------------------------------------------------
123class FLockObject : public FObject, public FLock
124{
125public:
126 OBJ_METHODS (FLockObject, FObject)
127};
128
129//------------------------------------------------------------------------
132//------------------------------------------------------------------------
134{
135public:
136//------------------------------------------------------------------------
137
141 FGuard (ILock& _lock) : lock (_lock) { lock.lock (); }
142
144 ~FGuard () { lock.unlock (); }
145
146//------------------------------------------------------------------------
147private:
148 ILock& lock;
149};
150
151//------------------------------------------------------------------------
154//------------------------------------------------------------------------
156{
157public:
158//------------------------------------------------------------------------
159
163 FConditionalGuard (FLock* _lock) : lock (_lock)
164 {
165 if (lock)
166 lock->lock ();
167 }
168
171 {
172 if (lock)
173 lock->unlock ();
174 }
175
176//------------------------------------------------------------------------
177private:
178 FLock* lock;
179};
180
181} // Thread
182} // Base
183} // Steinberg
Conditional Guard - Locks only if valid lock is passed.
Definition flock.h:156
FConditionalGuard(FLock *_lock)
FConditionGuard constructor.
Definition flock.h:163
~FConditionalGuard()
FConditionGuard destructor.
Definition flock.h:170
FGuard - automatic object for locks.
Definition flock.h:134
~FGuard()
FGuard destructor.
Definition flock.h:144
FGuard(ILock &_lock)
FGuard constructor.
Definition flock.h:141
FLockObj declaration.
Definition flock.h:124
FLock declaration.
Definition flock.h:92
void unlock() SMTG_OVERRIDE
Disables lock.
Definition flock.cpp:114
bool trylock() SMTG_OVERRIDE
Tries to disable lock.
Definition flock.cpp:130
~FLock() SMTG_OVERRIDE
Lock destructor.
Definition flock.cpp:86
void lock() SMTG_OVERRIDE
Enables lock.
Definition flock.cpp:98
Implements FUnknown and IDependent.
Definition fobject.h:84
Basic Object implementing FUnknown.
Lock interface declaration.
Definition flock.h:72
virtual void lock()=0
Enables lock.
virtual void unlock()=0
Disables lock.
virtual bool trylock()=0
Tries to disable lock.
typedef pthread_mutex_t