1 //------------------------------------------------------------------------
6 // Filename : base/thread/include/flock.h
7 // Created by : Steinberg, 1995
10 //-----------------------------------------------------------------------------
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:
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.
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 //-----------------------------------------------------------------------------
38 //----------------------------------------------------------------------------------
39 /** @file base/thread/include/flock.h
41 /** @defgroup baseLocks Locks */
42 //----------------------------------------------------------------------------------
45 #include "base/source/fobject.h"
46 #include "pluginterfaces/base/ftypes.h"
52 struct CRITSECT
// CRITICAL_SECTION
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
67 //------------------------------------------------------------------------
68 /** Lock interface declaration.
70 //------------------------------------------------------------------------
73 //------------------------------------------------------------------------
77 virtual void lock () = 0;
80 virtual void unlock () = 0;
82 /** Tries to disable lock. */
83 virtual bool trylock () = 0;
84 //------------------------------------------------------------------------
87 //------------------------------------------------------------------------
88 /** FLock declaration.
90 //------------------------------------------------------------------------
91 class FLock
: public ILock
94 //------------------------------------------------------------------------
97 * @param name lock name
99 FLock (const char8
* name
= "FLock");
101 /** Lock destructor. */
104 //-- ILock -----------------------------------------------------------
105 void lock () SMTG_OVERRIDE
;
106 void unlock () SMTG_OVERRIDE
;
107 bool trylock () SMTG_OVERRIDE
;
109 //------------------------------------------------------------------------
112 pthread_mutex_t mutex
; ///< Mutex object
114 #elif SMTG_OS_WINDOWS
115 CRITSECT section
; ///< Critical section object
119 //------------------------------------------------------------------------
120 /** FLockObj declaration. Reference counted lock
121 @ingroup baseLocks */
122 //------------------------------------------------------------------------
123 class FLockObject
: public FObject
, public FLock
126 OBJ_METHODS (FLockObject
, FObject
)
129 //------------------------------------------------------------------------
130 /** FGuard - automatic object for locks.
131 @ingroup baseLocks */
132 //------------------------------------------------------------------------
136 //------------------------------------------------------------------------
138 /** FGuard constructor.
139 * @param _lock guard this lock
141 FGuard (ILock
& _lock
) : lock (_lock
) { lock
.lock (); }
143 /** FGuard destructor. */
144 ~FGuard () { lock
.unlock (); }
146 //------------------------------------------------------------------------
148 ILock
& lock
; ///< guarded lock
151 //------------------------------------------------------------------------
152 /** Conditional Guard - Locks only if valid lock is passed.
153 @ingroup baseLocks */
154 //------------------------------------------------------------------------
155 class FConditionalGuard
158 //------------------------------------------------------------------------
160 /** FConditionGuard constructor.
161 * @param _lock guard this lock
163 FConditionalGuard (FLock
* _lock
) : lock (_lock
)
169 /** FConditionGuard destructor. */
170 ~FConditionalGuard ()
176 //------------------------------------------------------------------------
178 FLock
* lock
; ///< guarded lock