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.

147 lines
4.3KB

  1. //------------------------------------------------------------------------
  2. // Project : SDK Base
  3. // Version : 1.0
  4. //
  5. // Category : Helpers
  6. // Filename : base/thread/source/flock.cpp
  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. #define DEBUG_LOCK 0
  38. #include "base/thread/include/flock.h"
  39. #include "base/source/fdebug.h"
  40. //------------------------------------------------------------------------
  41. #if SMTG_OS_WINDOWS
  42. //------------------------------------------------------------------------
  43. #ifndef WINVER
  44. #define WINVER 0x0500
  45. #endif
  46. #ifndef _WIN32_WINNT
  47. #define _WIN32_WINNT WINVER
  48. #endif
  49. #include <windows.h>
  50. #include <objbase.h>
  51. #define INIT_CS(cs) \
  52. InitializeCriticalSection ((LPCRITICAL_SECTION)&cs);
  53. #endif
  54. namespace Steinberg {
  55. namespace Base {
  56. namespace Thread {
  57. //------------------------------------------------------------------------
  58. // FLock implementation
  59. //------------------------------------------------------------------------
  60. FLock::FLock (const char8* /*name*/)
  61. {
  62. #if SMTG_PTHREADS
  63. pthread_mutexattr_t mutexAttr;
  64. pthread_mutexattr_init (&mutexAttr);
  65. pthread_mutexattr_settype (&mutexAttr, PTHREAD_MUTEX_RECURSIVE);
  66. if (pthread_mutex_init (&mutex, &mutexAttr) != 0)
  67. {SMTG_WARNING("mutex_init failed")}
  68. pthread_mutexattr_destroy (&mutexAttr);
  69. #elif SMTG_OS_WINDOWS
  70. INIT_CS (section)
  71. #else
  72. #warning implement FLock!
  73. #endif
  74. }
  75. //------------------------------------------------------------------------
  76. FLock::~FLock ()
  77. {
  78. #if SMTG_PTHREADS
  79. pthread_mutex_destroy (&mutex);
  80. #elif SMTG_OS_WINDOWS
  81. DeleteCriticalSection ((LPCRITICAL_SECTION)&section);
  82. #endif
  83. }
  84. //------------------------------------------------------------------------
  85. void FLock::lock ()
  86. {
  87. #if DEBUG_LOCK
  88. FDebugPrint ("FLock::lock () %x\n", this);
  89. #endif
  90. #if SMTG_PTHREADS
  91. pthread_mutex_lock (&mutex);
  92. #elif SMTG_OS_WINDOWS
  93. EnterCriticalSection ((LPCRITICAL_SECTION)&section);
  94. #endif
  95. }
  96. //------------------------------------------------------------------------
  97. void FLock::unlock ()
  98. {
  99. #if DEBUG_LOCK
  100. FDebugPrint ("FLock::unlock () %x\n", this);
  101. #endif
  102. #if SMTG_PTHREADS
  103. pthread_mutex_unlock (&mutex);
  104. #elif SMTG_OS_WINDOWS
  105. LeaveCriticalSection ((LPCRITICAL_SECTION)&section);
  106. #endif
  107. }
  108. //------------------------------------------------------------------------
  109. bool FLock::trylock ()
  110. {
  111. #if SMTG_PTHREADS
  112. return pthread_mutex_trylock (&mutex) == 0;
  113. #elif SMTG_OS_WINDOWS
  114. return TryEnterCriticalSection ((LPCRITICAL_SECTION)&section) != 0 ? true : false;
  115. #else
  116. return false;
  117. #endif
  118. }
  119. } // Thread
  120. } // Base
  121. } // Steinberg