LMMS
Loading...
Searching...
No Matches
SpinLock.h
Go to the documentation of this file.
1/*
2 ==============================================================================
3
4 This file is part of the Water library.
5 Copyright (c) 2016 ROLI Ltd.
6 Copyright (C) 2017-2022 Filipe Coelho <falktx@falktx.com>
7
8 Permission is granted to use this software under the terms of the ISC license
9 http://www.isc.org/downloads/software-support-policy/isc-license/
10
11 Permission to use, copy, modify, and/or distribute this software for any
12 purpose with or without fee is hereby granted, provided that the above
13 copyright notice and this permission notice appear in all copies.
14
15 THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
16 TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17 FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
18 OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
19 USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
20 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
21 OF THIS SOFTWARE.
22
23 ==============================================================================
24*/
25
26#ifndef WATER_SPINLOCK_H_INCLUDED
27#define WATER_SPINLOCK_H_INCLUDED
28
29#include "ScopedLock.h"
30#include "../memory/Atomic.h"
31
32namespace water {
33
34//==============================================================================
48{
49public:
50 inline SpinLock() noexcept : lock() {}
51 inline ~SpinLock() noexcept {}
52
63 {
64 if (! tryEnter())
65 {
66 for (int i = 20; --i >= 0;)
67 if (tryEnter())
68 return;
69
70 while (! tryEnter())
71 {
72#ifdef CARLA_OS_WIN
73 Sleep (0);
74#else
75 sched_yield();
76#endif
77 }
78 }
79 }
80
82 inline bool tryEnter() const noexcept
83 {
84 return lock.compareAndSetBool (1, 0);
85 }
86
88 inline void exit() const noexcept
89 {
90 CARLA_SAFE_ASSERT_RETURN(lock.get() == 1,);
91 lock = 0;
92 }
93
94 //==============================================================================
96 typedef GenericScopedLock <SpinLock> ScopedLockType;
97
99 typedef GenericScopedUnlock <SpinLock> ScopedUnlockType;
100
101private:
102 //==============================================================================
104
106};
107
108}
109
110#endif // WATER_SPINLOCK_H_INCLUDED
#define CARLA_SAFE_ASSERT_RETURN(cond, ret)
Definition CarlaDefines.h:190
#define CARLA_DECLARE_NON_COPYABLE(ClassName)
Definition CarlaDefines.h:242
#define noexcept
Definition DistrhoDefines.h:72
Definition Atomic.h:95
bool tryEnter() const noexcept
Definition SpinLock.h:82
Atomic< int > lock
Definition SpinLock.h:103
~SpinLock() noexcept
Definition SpinLock.h:51
GenericScopedUnlock< SpinLock > ScopedUnlockType
Definition SpinLock.h:99
void enter() const noexcept
Definition SpinLock.h:62
void exit() const noexcept
Definition SpinLock.h:88
SpinLock() noexcept
Definition SpinLock.h:50
GenericScopedLock< SpinLock > ScopedLockType
Definition SpinLock.h:96
register unsigned i
Definition inflate.c:1575
Definition AudioSampleBuffer.h:33
void Sleep(int ms)
Definition swell.cpp:63
#define const
Definition zconf.h:137