OpenShot Library | OpenShotAudio  0.2.2
juce_InterProcessLock.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  Acts as a critical section which processes can use to block each other.
33 
34  @see CriticalSection
35 
36  @tags{Core}
37 */
39 {
40 public:
41  //==============================================================================
42  /** Creates a lock object.
43  @param name a name that processes will use to identify this lock object
44  */
45  explicit InterProcessLock (const String& name);
46 
47  /** Destructor.
48  This will also release the lock if it's currently held by this process.
49  */
51 
52  //==============================================================================
53  /** Attempts to lock the critical section.
54 
55  @param timeOutMillisecs how many milliseconds to wait if the lock is already
56  held by another process - a value of 0 will return
57  immediately, negative values will wait forever
58  @returns true if the lock could be gained within the timeout period, or
59  false if the timeout expired.
60  */
61  bool enter (int timeOutMillisecs = -1);
62 
63  /** Releases the lock if it's currently held by this process. */
64  void exit();
65 
66  //==============================================================================
67  /**
68  Automatically locks and unlocks an InterProcessLock object.
69 
70  This works like a ScopedLock, but using an InterprocessLock rather than
71  a CriticalSection.
72 
73  @see ScopedLock
74  */
76  {
77  public:
78  //==============================================================================
79  /** Creates a scoped lock.
80 
81  As soon as it is created, this will lock the InterProcessLock, and
82  when the ScopedLockType object is deleted, the InterProcessLock will
83  be unlocked.
84 
85  Note that since an InterprocessLock can fail due to errors, you should check
86  isLocked() to make sure that the lock was successful before using it.
87 
88  Make sure this object is created and deleted by the same thread,
89  otherwise there are no guarantees what will happen! Best just to use it
90  as a local stack object, rather than creating one with the new() operator.
91  */
92  explicit ScopedLockType (InterProcessLock& l) : ipLock (l) { lockWasSuccessful = l.enter(); }
93 
94  /** Destructor.
95 
96  The InterProcessLock will be unlocked when the destructor is called.
97 
98  Make sure this object is created and deleted by the same thread,
99  otherwise there are no guarantees what will happen!
100  */
101  inline ~ScopedLockType() { ipLock.exit(); }
102 
103  /** Returns true if the InterProcessLock was successfully locked. */
104  bool isLocked() const noexcept { return lockWasSuccessful; }
105 
106  private:
107  //==============================================================================
108  InterProcessLock& ipLock;
109  bool lockWasSuccessful;
110 
111  JUCE_DECLARE_NON_COPYABLE (ScopedLockType)
112  };
113 
114 private:
115  //==============================================================================
116  class Pimpl;
117  std::unique_ptr<Pimpl> pimpl;
118 
119  CriticalSection lock;
120  String name;
121 
122  JUCE_DECLARE_NON_COPYABLE (InterProcessLock)
123 };
124 
125 } // namespace juce
126 
127 /** @}*/
#define JUCE_API
This macro is added to all JUCE public class declarations.
Acts as a critical section which processes can use to block each other.
bool enter(int timeOutMillisecs=-1)
Attempts to lock the critical section.
The JUCE String class!
Definition: juce_String.h:42
Automatically locks and unlocks an InterProcessLock object.
bool isLocked() const noexcept
Returns true if the InterProcessLock was successfully locked.
ScopedLockType(InterProcessLock &l)
Creates a scoped lock.
A re-entrant mutex.