Audio plugin host https://kx.studio/carla
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.

flock.h 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. //------------------------------------------------------------------------
  2. // Project : SDK Base
  3. // Version : 1.0
  4. //
  5. // Category : Helpers
  6. // Filename : base/thread/include/flock.h
  7. // Created by : Steinberg, 1995
  8. // Description : locks
  9. //
  10. //-----------------------------------------------------------------------------
  11. // LICENSE
  12. // (c) 2019, Steinberg Media Technologies GmbH, All Rights Reserved
  13. //-----------------------------------------------------------------------------
  14. // Redistribution and use in source and binary forms, with or without modification,
  15. // are permitted provided that the following conditions are met:
  16. //
  17. // * Redistributions of source code must retain the above copyright notice,
  18. // this list of conditions and the following disclaimer.
  19. // * Redistributions in binary form must reproduce the above copyright notice,
  20. // this list of conditions and the following disclaimer in the documentation
  21. // and/or other materials provided with the distribution.
  22. // * Neither the name of the Steinberg Media Technologies nor the names of its
  23. // contributors may be used to endorse or promote products derived from this
  24. // software without specific prior written permission.
  25. //
  26. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  27. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  28. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  29. // IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  30. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  32. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  33. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  34. // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  35. // OF THE POSSIBILITY OF SUCH DAMAGE.
  36. //-----------------------------------------------------------------------------
  37. //----------------------------------------------------------------------------------
  38. /** @file base/thread/include/flock.h
  39. Locks. */
  40. /** @defgroup baseLocks Locks */
  41. //----------------------------------------------------------------------------------
  42. #pragma once
  43. #include "base/source/fobject.h"
  44. #include "pluginterfaces/base/ftypes.h"
  45. #if SMTG_PTHREADS
  46. #include <pthread.h>
  47. #elif SMTG_OS_WINDOWS
  48. struct CRITSECT // CRITICAL_SECTION
  49. {
  50. void* DebugInfo; // PRTL_CRITICAL_SECTION_DEBUG DebugInfo;
  51. Steinberg::int32 LockCount; // LONG LockCount;
  52. Steinberg::int32 RecursionCount; // LONG RecursionCount;
  53. void* OwningThread; // HANDLE OwningThread
  54. void* LockSemaphore; // HANDLE LockSemaphore
  55. Steinberg::int32 SpinCount; // ULONG_PTR SpinCount
  56. };
  57. #endif
  58. namespace Steinberg {
  59. namespace Base {
  60. namespace Thread {
  61. //------------------------------------------------------------------------
  62. /** Lock interface declaration.
  63. @ingroup baseLocks */
  64. //------------------------------------------------------------------------
  65. struct ILock
  66. {
  67. //------------------------------------------------------------------------
  68. virtual ~ILock () {}
  69. /** Enables lock. */
  70. virtual void lock () = 0;
  71. /** Disables lock. */
  72. virtual void unlock () = 0;
  73. /** Tries to disable lock. */
  74. virtual bool trylock () = 0;
  75. //------------------------------------------------------------------------
  76. };
  77. //------------------------------------------------------------------------
  78. /** FLock declaration.
  79. @ingroup baseLocks */
  80. //------------------------------------------------------------------------
  81. class FLock : public ILock
  82. {
  83. public:
  84. //------------------------------------------------------------------------
  85. /** Lock constructor.
  86. * @param name lock name
  87. */
  88. FLock (const char8* name = "FLock");
  89. /** Lock destructor. */
  90. ~FLock ();
  91. //-- ILock -----------------------------------------------------------
  92. virtual void lock () SMTG_OVERRIDE;
  93. virtual void unlock () SMTG_OVERRIDE;
  94. virtual bool trylock () SMTG_OVERRIDE;
  95. //------------------------------------------------------------------------
  96. protected:
  97. #if SMTG_PTHREADS
  98. pthread_mutex_t mutex; ///< Mutex object
  99. #elif SMTG_OS_WINDOWS
  100. CRITSECT section; ///< Critical section object
  101. #endif
  102. };
  103. //------------------------------------------------------------------------
  104. /** FLockObj declaration. Reference counted lock
  105. @ingroup baseLocks */
  106. //------------------------------------------------------------------------
  107. class FLockObject : public FObject, public FLock
  108. {
  109. public:
  110. OBJ_METHODS (FLockObject, FObject)
  111. };
  112. //------------------------------------------------------------------------
  113. /** FGuard - automatic object for locks.
  114. @ingroup baseLocks */
  115. //------------------------------------------------------------------------
  116. class FGuard
  117. {
  118. public:
  119. //------------------------------------------------------------------------
  120. /** FGuard constructor.
  121. * @param _lock guard this lock
  122. */
  123. FGuard (ILock& _lock) : lock (_lock) { lock.lock (); }
  124. /** FGuard destructor. */
  125. ~FGuard () { lock.unlock (); }
  126. //------------------------------------------------------------------------
  127. private:
  128. ILock& lock; ///< guarded lock
  129. };
  130. //------------------------------------------------------------------------
  131. /** Conditional Guard - Locks only if valid lock is passed.
  132. @ingroup baseLocks */
  133. //------------------------------------------------------------------------
  134. class FConditionalGuard
  135. {
  136. public:
  137. //------------------------------------------------------------------------
  138. /** FConditionGuard constructor.
  139. * @param _lock guard this lock
  140. */
  141. FConditionalGuard (FLock* _lock) : lock (_lock)
  142. {
  143. if (lock)
  144. lock->lock ();
  145. }
  146. /** FConditionGuard destructor. */
  147. ~FConditionalGuard ()
  148. {
  149. if (lock)
  150. lock->unlock ();
  151. }
  152. //------------------------------------------------------------------------
  153. private:
  154. FLock* lock; ///< guarded lock
  155. };
  156. } // Thread
  157. } // Base
  158. } // Steinberg