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.

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