OpenShot Library | libopenshot-audio  0.1.9
juce_SpinLock.h
1 
2 /** @weakgroup juce_core-threads
3  * @{
4  */
5 /*
6  ==============================================================================
7 
8  This file is part of the JUCE library.
9  Copyright (c) 2017 - ROLI Ltd.
10 
11  JUCE is an open source library subject to commercial or open-source
12  licensing.
13 
14  The code included in this file is provided under the terms of the ISC license
15  http://www.isc.org/downloads/software-support-policy/isc-license. Permission
16  To use, copy, modify, and/or distribute this software for any purpose with or
17  without fee is hereby granted provided that the above copyright notice and
18  this permission notice appear in all copies.
19 
20  JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
21  EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
22  DISCLAIMED.
23 
24  ==============================================================================
25 */
26 
27 namespace juce
28 {
29 
30 //==============================================================================
31 /**
32  A simple spin-lock class that can be used as a simple, low-overhead mutex for
33  uncontended situations.
34 
35  Note that unlike a CriticalSection, this type of lock is not re-entrant, and may
36  be less efficient when used in a highly contended situation, but it's very small and
37  requires almost no initialisation.
38  It's most appropriate for simple situations where you're only going to hold the
39  lock for a very brief time.
40 
41  @see CriticalSection
42 
43  @tags{Core}
44 */
46 {
47 public:
48  inline SpinLock() = default;
49  inline ~SpinLock() = default;
50 
51  /** Acquires the lock.
52  This will block until the lock has been successfully acquired by this thread.
53  Note that a SpinLock is NOT re-entrant, and is not smart enough to know whether the
54  caller thread already has the lock - so if a thread tries to acquire a lock that it
55  already holds, this method will never return!
56 
57  It's strongly recommended that you never call this method directly - instead use the
58  ScopedLockType class to manage the locking using an RAII pattern instead.
59  */
60  void enter() const noexcept;
61 
62  /** Attempts to acquire the lock, returning true if this was successful. */
63  inline bool tryEnter() const noexcept
64  {
65  return lock.compareAndSetBool (1, 0);
66  }
67 
68  /** Releases the lock. */
69  inline void exit() const noexcept
70  {
71  jassert (lock.get() == 1); // Agh! Releasing a lock that isn't currently held!
72  lock = 0;
73  }
74 
75  //==============================================================================
76  /** Provides the type of scoped lock to use for locking a SpinLock. */
78 
79  /** Provides the type of scoped unlocker to use with a SpinLock. */
81 
82 private:
83  //==============================================================================
84  mutable Atomic<int> lock;
85 
86  JUCE_DECLARE_NON_COPYABLE (SpinLock)
87 };
88 
89 } // namespace juce
90 
91 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
A simple spin-lock class that can be used as a simple, low-overhead mutex for uncontended situations...
Definition: juce_SpinLock.h:45
bool tryEnter() const noexcept
Attempts to acquire the lock, returning true if this was successful.
Definition: juce_SpinLock.h:63
void exit() const noexcept
Releases the lock.
Definition: juce_SpinLock.h:69
Automatically locks and unlocks a mutex object.
Automatically unlocks and re-locks a mutex object.