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.cpp
Go to the documentation of this file.
1 //------------------------------------------------------------------------
2// Project : SDK Base
3// Version : 1.0
4//
5// Category : Helpers
6// Filename : base/thread/source/flock.cpp
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#define DEBUG_LOCK 0
39
41#include "base/source/fdebug.h"
42
43//------------------------------------------------------------------------
44#if SMTG_OS_WINDOWS
45//------------------------------------------------------------------------
46#ifndef WINVER
47#define WINVER 0x0500
48#endif
49#ifndef _WIN32_WINNT
50#define _WIN32_WINNT WINVER
51#endif
52
53#include <windows.h>
54#include <objbase.h>
55#define INIT_CS(cs) \
56 InitializeCriticalSection ((LPCRITICAL_SECTION)&cs);
57
58#endif // SMTG_OS_WINDOWS
59
60namespace Steinberg {
61namespace Base {
62namespace Thread {
63
64
65//------------------------------------------------------------------------
66// FLock implementation
67//------------------------------------------------------------------------
68FLock::FLock (const char8* /*name*/)
69{
70#if SMTG_PTHREADS
71 pthread_mutexattr_t mutexAttr;
72 pthread_mutexattr_init (&mutexAttr);
73 pthread_mutexattr_settype (&mutexAttr, PTHREAD_MUTEX_RECURSIVE);
74 if (pthread_mutex_init (&mutex, &mutexAttr) != 0)
75 {SMTG_WARNING("mutex_init failed")}
76 pthread_mutexattr_destroy (&mutexAttr);
77
78#elif SMTG_OS_WINDOWS
79 INIT_CS (section)
80#else
81#warning implement FLock!
82#endif
83}
84
85//------------------------------------------------------------------------
87{
88#if SMTG_PTHREADS
89 pthread_mutex_destroy (&mutex);
90
91#elif SMTG_OS_WINDOWS
92 DeleteCriticalSection ((LPCRITICAL_SECTION)&section);
93
94#endif
95}
96
97//------------------------------------------------------------------------
99{
100#if DEBUG_LOCK
101 FDebugPrint ("FLock::lock () %x\n", this);
102#endif
103
104#if SMTG_PTHREADS
105 pthread_mutex_lock (&mutex);
106
107#elif SMTG_OS_WINDOWS
108 EnterCriticalSection ((LPCRITICAL_SECTION)&section);
109
110#endif
111}
112
113//------------------------------------------------------------------------
115{
116#if DEBUG_LOCK
117 FDebugPrint ("FLock::unlock () %x\n", this);
118#endif
119
120#if SMTG_PTHREADS
121 pthread_mutex_unlock (&mutex);
122
123#elif SMTG_OS_WINDOWS
124 LeaveCriticalSection ((LPCRITICAL_SECTION)&section);
125
126#endif
127}
128
129//------------------------------------------------------------------------
131{
132#if SMTG_PTHREADS
133 return pthread_mutex_trylock (&mutex) == 0;
134
135#elif SMTG_OS_WINDOWS
136 return TryEnterCriticalSection ((LPCRITICAL_SECTION)&section) != 0 ? true : false;
137
138#else
139 return false;
140#endif
141}
142
143} // Thread
144} // Base
145} // Steinberg
146
FLock(const char8 *name="FLock")
Lock constructor.
Definition flock.cpp:68
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
Debugging tools.
Locks.
pthread_mutex_init
pthread_mutex_lock
pthread_mutexattr_init
pthread_mutexattr_settype
typedef pthread_mutexattr_t