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.

122 lines
4.4KB

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