LMMS
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) 2021, 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 ();
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:
127};
128
129//------------------------------------------------------------------------
132//------------------------------------------------------------------------
134{
135public:
136//------------------------------------------------------------------------
137
141 FGuard (ILock& _lock) : lock (_lock) { lock.lock (); }
142
144 ~FGuard () { lock.unlock (); }
145
146//------------------------------------------------------------------------
147private:
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:
179};
180
181} // Thread
182} // Base
183} // Steinberg
pthread_mutex_t mutex
Definition Controller.C:6
FGuard(ILock &_lock)
Definition flock.h:141
FConditionalGuard(FLock *_lock)
Definition flock.h:163
FLock * lock
guarded lock
Definition flock.h:178
~FConditionalGuard()
Definition flock.h:170
ILock & lock
guarded lock
Definition flock.h:148
~FGuard()
Definition flock.h:144
FGuard(ILock &_lock)
Definition flock.h:141
Definition flock.h:92
~FLock()
Definition flock.cpp:86
FLock(const char8 *name="FLock")
Definition flock.cpp:68
void unlock() SMTG_OVERRIDE
Definition flock.cpp:114
bool trylock() SMTG_OVERRIDE
Definition flock.cpp:130
void lock() SMTG_OVERRIDE
Definition flock.cpp:98
FObject()
default constructor...
Definition fobject.h:85
#define OBJ_METHODS(className, baseClass)
Definition fobject.h:339
#define SMTG_OVERRIDE
Definition fplatform.h:241
static const char * name
Definition pugl.h:1582
Definition flock.h:65
Definition flock.h:64
Definition baseiids.cpp:43
int int32
Definition ftypes.h:50
char char8
Definition ftypes.h:93
Definition flock.h:72
virtual ~ILock()
Definition flock.h:74