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.

119 lines
4.4KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2017 - ROLI Ltd.
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. The code included in this file is provided under the terms of the ISC license
  8. http://www.isc.org/downloads/software-support-policy/isc-license. Permission
  9. To use, copy, modify, and/or distribute this software for any purpose with or
  10. without fee is hereby granted provided that the above copyright notice and
  11. this permission notice appear in all copies.
  12. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  13. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  14. DISCLAIMED.
  15. ==============================================================================
  16. */
  17. #pragma once
  18. //==============================================================================
  19. /**
  20. Acts as a critical section which processes can use to block each other.
  21. @see CriticalSection
  22. */
  23. class JUCE_API InterProcessLock
  24. {
  25. public:
  26. //==============================================================================
  27. /** Creates a lock object.
  28. @param name a name that processes will use to identify this lock object
  29. */
  30. explicit InterProcessLock (const String& name);
  31. /** Destructor.
  32. This will also release the lock if it's currently held by this process.
  33. */
  34. ~InterProcessLock();
  35. //==============================================================================
  36. /** Attempts to lock the critical section.
  37. @param timeOutMillisecs how many milliseconds to wait if the lock is already
  38. held by another process - a value of 0 will return
  39. immediately, negative values will wait forever
  40. @returns true if the lock could be gained within the timeout period, or
  41. false if the timeout expired.
  42. */
  43. bool enter (int timeOutMillisecs = -1);
  44. /** Releases the lock if it's currently held by this process. */
  45. void exit();
  46. //==============================================================================
  47. /**
  48. Automatically locks and unlocks an InterProcessLock object.
  49. This works like a ScopedLock, but using an InterprocessLock rather than
  50. a CriticalSection.
  51. @see ScopedLock
  52. */
  53. class ScopedLockType
  54. {
  55. public:
  56. //==============================================================================
  57. /** Creates a scoped lock.
  58. As soon as it is created, this will lock the InterProcessLock, and
  59. when the ScopedLockType object is deleted, the InterProcessLock will
  60. be unlocked.
  61. Note that since an InterprocessLock can fail due to errors, you should check
  62. isLocked() to make sure that the lock was successful before using it.
  63. Make sure this object is created and deleted by the same thread,
  64. otherwise there are no guarantees what will happen! Best just to use it
  65. as a local stack object, rather than creating one with the new() operator.
  66. */
  67. explicit ScopedLockType (InterProcessLock& l) : ipLock (l) { lockWasSuccessful = l.enter(); }
  68. /** Destructor.
  69. The InterProcessLock will be unlocked when the destructor is called.
  70. Make sure this object is created and deleted by the same thread,
  71. otherwise there are no guarantees what will happen!
  72. */
  73. inline ~ScopedLockType() { ipLock.exit(); }
  74. /** Returns true if the InterProcessLock was successfully locked. */
  75. bool isLocked() const noexcept { return lockWasSuccessful; }
  76. private:
  77. //==============================================================================
  78. InterProcessLock& ipLock;
  79. bool lockWasSuccessful;
  80. JUCE_DECLARE_NON_COPYABLE (ScopedLockType)
  81. };
  82. private:
  83. //==============================================================================
  84. class Pimpl;
  85. friend struct ContainerDeletePolicy<Pimpl>;
  86. ScopedPointer<Pimpl> pimpl;
  87. CriticalSection lock;
  88. String name;
  89. JUCE_DECLARE_NON_COPYABLE (InterProcessLock)
  90. };