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.

143 lines
4.3KB

  1. //------------------------------------------------------------------------
  2. // Project : SDK Base
  3. // Version : 1.0
  4. //
  5. // Category : Helpers
  6. // Filename : base/source/flock.cpp
  7. // Created by : Steinberg, 1995
  8. // Description : Locks
  9. //
  10. //-----------------------------------------------------------------------------
  11. // LICENSE
  12. // (c) 2017, 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/source/flock.h"
  39. #include "base/source/fdebug.h"
  40. //------------------------------------------------------------------------
  41. #if 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. //------------------------------------------------------------------------
  55. namespace Steinberg {
  56. //------------------------------------------------------------------------
  57. // FLock implementation
  58. //------------------------------------------------------------------------
  59. FLock::FLock (const char8* name)
  60. {
  61. #if PTHREADS
  62. pthread_mutexattr_t mutexAttr;
  63. pthread_mutexattr_init (&mutexAttr);
  64. pthread_mutexattr_settype (&mutexAttr, PTHREAD_MUTEX_RECURSIVE);
  65. if (pthread_mutex_init (&mutex, &mutexAttr) != 0)
  66. {WARNING("mutex_init failed")}
  67. pthread_mutexattr_destroy (&mutexAttr);
  68. #elif WINDOWS
  69. INIT_CS (section)
  70. #else
  71. #warning implement FLock!
  72. #endif
  73. }
  74. //------------------------------------------------------------------------
  75. FLock::~FLock ()
  76. {
  77. #if PTHREADS
  78. pthread_mutex_destroy (&mutex);
  79. #elif WINDOWS
  80. DeleteCriticalSection ((LPCRITICAL_SECTION)&section);
  81. #endif
  82. }
  83. //------------------------------------------------------------------------
  84. void FLock::lock ()
  85. {
  86. #if DEBUG_LOCK
  87. FDebugPrint ("FLock::lock () %x\n", this);
  88. #endif
  89. #if PTHREADS
  90. pthread_mutex_lock (&mutex);
  91. #elif WINDOWS
  92. EnterCriticalSection ((LPCRITICAL_SECTION)&section);
  93. #endif
  94. }
  95. //------------------------------------------------------------------------
  96. void FLock::unlock ()
  97. {
  98. #if DEBUG_LOCK
  99. FDebugPrint ("FLock::unlock () %x\n", this);
  100. #endif
  101. #if PTHREADS
  102. pthread_mutex_unlock (&mutex);
  103. #elif WINDOWS
  104. LeaveCriticalSection ((LPCRITICAL_SECTION)&section);
  105. #endif
  106. }
  107. //------------------------------------------------------------------------
  108. bool FLock::trylock ()
  109. {
  110. #if PTHREADS
  111. return pthread_mutex_trylock (&mutex) == 0;
  112. #elif WINDOWS
  113. return TryEnterCriticalSection ((LPCRITICAL_SECTION)&section) != 0 ? true : false;
  114. #else
  115. return false;
  116. #endif
  117. }
  118. //------------------------------------------------------------------------
  119. } // namespace Steinberg