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.

131 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2016 - ROLI Ltd.
  5. Permission is granted to use this software under the terms of the ISC license
  6. http://www.isc.org/downloads/software-support-policy/isc-license/
  7. Permission to use, copy, modify, and/or distribute this software for any
  8. purpose with or without fee is hereby granted, provided that the above
  9. copyright notice and this permission notice appear in all copies.
  10. THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH REGARD
  11. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  12. FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT,
  13. OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF
  14. USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
  15. TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
  16. OF THIS SOFTWARE.
  17. -----------------------------------------------------------------------------
  18. To release a closed-source product which uses other parts of JUCE not
  19. licensed under the ISC terms, commercial licenses are available: visit
  20. www.juce.com for more information.
  21. ==============================================================================
  22. */
  23. #ifndef JUCE_INTERPROCESSLOCK_H_INCLUDED
  24. #define JUCE_INTERPROCESSLOCK_H_INCLUDED
  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 struct ContainerDeletePolicy<Pimpl>;
  93. ScopedPointer<Pimpl> pimpl;
  94. CriticalSection lock;
  95. String name;
  96. JUCE_DECLARE_NON_COPYABLE (InterProcessLock)
  97. };
  98. #endif // JUCE_INTERPROCESSLOCK_H_INCLUDED