The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

132 lines
5.1KB

  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef __JUCE_INTERPROCESSLOCK_JUCEHEADER__
  22. #define __JUCE_INTERPROCESSLOCK_JUCEHEADER__
  23. #include "../text/juce_String.h"
  24. #include "../memory/juce_ScopedPointer.h"
  25. //==============================================================================
  26. /**
  27. Acts as a critical section which processes can use to block each other.
  28. @see CriticalSection
  29. */
  30. class JUCE_API InterProcessLock
  31. {
  32. public:
  33. //==============================================================================
  34. /** Creates a lock object.
  35. @param name a name that processes will use to identify this lock object
  36. */
  37. explicit InterProcessLock (const String& name);
  38. /** Destructor.
  39. This will also release the lock if it's currently held by this process.
  40. */
  41. ~InterProcessLock();
  42. //==============================================================================
  43. /** Attempts to lock the critical section.
  44. @param timeOutMillisecs how many milliseconds to wait if the lock is already
  45. held by another process - a value of 0 will return
  46. immediately, negative values will wait forever
  47. @returns true if the lock could be gained within the timeout period, or
  48. false if the timeout expired.
  49. */
  50. bool enter (int timeOutMillisecs = -1);
  51. /** Releases the lock if it's currently held by this process. */
  52. void exit();
  53. //==============================================================================
  54. /**
  55. Automatically locks and unlocks an InterProcessLock object.
  56. This works like a ScopedLock, but using an InterprocessLock rather than
  57. a CriticalSection.
  58. @see ScopedLock
  59. */
  60. class ScopedLockType
  61. {
  62. public:
  63. //==============================================================================
  64. /** Creates a scoped lock.
  65. As soon as it is created, this will lock the InterProcessLock, and
  66. when the ScopedLockType object is deleted, the InterProcessLock will
  67. be unlocked.
  68. Note that since an InterprocessLock can fail due to errors, you should check
  69. isLocked() to make sure that the lock was successful before using it.
  70. Make sure this object is created and deleted by the same thread,
  71. otherwise there are no guarantees what will happen! Best just to use it
  72. as a local stack object, rather than creating one with the new() operator.
  73. */
  74. explicit ScopedLockType (InterProcessLock& l) : ipLock (l) { lockWasSuccessful = l.enter(); }
  75. /** Destructor.
  76. The InterProcessLock will be unlocked when the destructor is called.
  77. Make sure this object is created and deleted by the same thread,
  78. otherwise there are no guarantees what will happen!
  79. */
  80. inline ~ScopedLockType() { ipLock.exit(); }
  81. /** Returns true if the InterProcessLock was successfully locked. */
  82. bool isLocked() const noexcept { return lockWasSuccessful; }
  83. private:
  84. //==============================================================================
  85. InterProcessLock& ipLock;
  86. bool lockWasSuccessful;
  87. JUCE_DECLARE_NON_COPYABLE (ScopedLockType)
  88. };
  89. private:
  90. //==============================================================================
  91. class Pimpl;
  92. friend class ScopedPointer <Pimpl>;
  93. ScopedPointer <Pimpl> pimpl;
  94. CriticalSection lock;
  95. String name;
  96. JUCE_DECLARE_NON_COPYABLE (InterProcessLock)
  97. };
  98. #endif // __JUCE_INTERPROCESSLOCK_JUCEHEADER__