juce_InterProcessLock.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2015 - ROLI 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_H_INCLUDED
  22. #define JUCE_INTERPROCESSLOCK_H_INCLUDED
  23. //==============================================================================
  24. /**
  25. Acts as a critical section which processes can use to block each other.
  26. @see CriticalSection
  27. */
  28. class JUCE_API InterProcessLock
  29. {
  30. public:
  31. //==============================================================================
  32. /** Creates a lock object.
  33. @param name a name that processes will use to identify this lock object
  34. */
  35. explicit InterProcessLock (const String& name);
  36. /** Destructor.
  37. This will also release the lock if it's currently held by this process.
  38. */
  39. ~InterProcessLock();
  40. //==============================================================================
  41. /** Attempts to lock the critical section.
  42. @param timeOutMillisecs how many milliseconds to wait if the lock is already
  43. held by another process - a value of 0 will return
  44. immediately, negative values will wait 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. void exit();
  51. //==============================================================================
  52. /**
  53. Automatically locks and unlocks an InterProcessLock object.
  54. This works like a ScopedLock, but using an InterprocessLock rather than
  55. a CriticalSection.
  56. @see ScopedLock
  57. */
  58. class ScopedLockType
  59. {
  60. public:
  61. //==============================================================================
  62. /** Creates a scoped lock.
  63. As soon as it is created, this will lock the InterProcessLock, and
  64. when the ScopedLockType object is deleted, the InterProcessLock will
  65. be unlocked.
  66. Note that since an InterprocessLock can fail due to errors, you should check
  67. isLocked() to make sure that the lock was successful before using it.
  68. Make sure this object is created and deleted by the same thread,
  69. otherwise there are no guarantees what will happen! Best just to use it
  70. as a local stack object, rather than creating one with the new() operator.
  71. */
  72. explicit ScopedLockType (InterProcessLock& l) : ipLock (l) { lockWasSuccessful = l.enter(); }
  73. /** Destructor.
  74. The InterProcessLock will be unlocked when the destructor is called.
  75. Make sure this object is created and deleted by the same thread,
  76. otherwise there are no guarantees what will happen!
  77. */
  78. inline ~ScopedLockType() { ipLock.exit(); }
  79. /** Returns true if the InterProcessLock was successfully locked. */
  80. bool isLocked() const noexcept { return lockWasSuccessful; }
  81. private:
  82. //==============================================================================
  83. InterProcessLock& ipLock;
  84. bool lockWasSuccessful;
  85. JUCE_DECLARE_NON_COPYABLE (ScopedLockType)
  86. };
  87. private:
  88. //==============================================================================
  89. class Pimpl;
  90. friend struct ContainerDeletePolicy<Pimpl>;
  91. ScopedPointer<Pimpl> pimpl;
  92. CriticalSection lock;
  93. String name;
  94. JUCE_DECLARE_NON_COPYABLE (InterProcessLock)
  95. };
  96. #endif // JUCE_INTERPROCESSLOCK_H_INCLUDED