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.

133 lines
5.0KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library - "Jules' Utility Class Extensions"
  4. Copyright 2004-11 by Raw Material Software Ltd.
  5. ------------------------------------------------------------------------------
  6. JUCE can be redistributed and/or modified under the terms of the GNU General
  7. Public License (Version 2), as published by the Free Software Foundation.
  8. A copy of the license is included in the JUCE distribution, or can be found
  9. online at www.gnu.org/licenses.
  10. JUCE is distributed in the hope that it will be useful, but WITHOUT ANY
  11. WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  12. A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. ------------------------------------------------------------------------------
  14. To release a closed-source product which uses JUCE, commercial licenses are
  15. available: visit www.rawmaterialsoftware.com/juce for more information.
  16. ==============================================================================
  17. */
  18. #ifndef __JUCE_INTERPROCESSLOCK_JUCEHEADER__
  19. #define __JUCE_INTERPROCESSLOCK_JUCEHEADER__
  20. #include "../text/juce_String.h"
  21. #include "../memory/juce_ScopedPointer.h"
  22. //==============================================================================
  23. /**
  24. Acts as a critical section which processes can use to block each other.
  25. @see CriticalSection
  26. */
  27. class JUCE_API InterProcessLock
  28. {
  29. public:
  30. //==============================================================================
  31. /** Creates a lock object.
  32. @param name a name that processes will use to identify this lock object
  33. */
  34. explicit InterProcessLock (const String& name);
  35. /** Destructor.
  36. This will also release the lock if it's currently held by this process.
  37. */
  38. ~InterProcessLock();
  39. //==============================================================================
  40. /** Attempts to lock the critical section.
  41. @param timeOutMillisecs how many milliseconds to wait if the lock
  42. is already held by another process - a value of
  43. 0 will return immediately, negative values will wait
  44. forever
  45. @returns true if the lock could be gained within the timeout period, or
  46. false if the timeout expired.
  47. */
  48. bool enter (int timeOutMillisecs = -1);
  49. /** Releases the lock if it's currently held by this process.
  50. */
  51. void exit();
  52. //==============================================================================
  53. /**
  54. Automatically locks and unlocks an InterProcessLock object.
  55. This works like a ScopedLock, but using an InterprocessLock rather than
  56. a CriticalSection.
  57. @see ScopedLock
  58. */
  59. class ScopedLockType
  60. {
  61. public:
  62. //==============================================================================
  63. /** Creates a scoped lock.
  64. As soon as it is created, this will lock the InterProcessLock, and
  65. when the ScopedLockType object is deleted, the InterProcessLock will
  66. be unlocked.
  67. Note that since an InterprocessLock can fail due to errors, you should check
  68. isLocked() to make sure that the lock was successful before using it.
  69. Make sure this object is created and deleted by the same thread,
  70. otherwise there are no guarantees what will happen! Best just to use it
  71. as a local stack object, rather than creating one with the new() operator.
  72. */
  73. explicit ScopedLockType (InterProcessLock& lock) : lock_ (lock) { lockWasSuccessful = lock.enter(); }
  74. /** Destructor.
  75. The InterProcessLock will be unlocked when the destructor is called.
  76. Make sure this object is created and deleted by the same thread,
  77. otherwise there are no guarantees what will happen!
  78. */
  79. inline ~ScopedLockType() { lock_.exit(); }
  80. /** Returns true if the InterProcessLock was successfully locked. */
  81. bool isLocked() const noexcept { return lockWasSuccessful; }
  82. private:
  83. //==============================================================================
  84. InterProcessLock& lock_;
  85. bool lockWasSuccessful;
  86. JUCE_DECLARE_NON_COPYABLE (ScopedLockType);
  87. };
  88. private:
  89. //==============================================================================
  90. class Pimpl;
  91. friend class ScopedPointer <Pimpl>;
  92. ScopedPointer <Pimpl> pimpl;
  93. CriticalSection lock;
  94. String name;
  95. JUCE_DECLARE_NON_COPYABLE (InterProcessLock);
  96. };
  97. #endif // __JUCE_INTERPROCESSLOCK_JUCEHEADER__