AkLock.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. /*******************************************************************************
  2. The content of this file includes portions of the AUDIOKINETIC Wwise Technology
  3. released in source code form as part of the SDK installer package.
  4. Commercial License Usage
  5. Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
  6. may use this file in accordance with the end user license agreement provided
  7. with the software or, alternatively, in accordance with the terms contained in a
  8. written agreement between you and Audiokinetic Inc.
  9. Apache License Usage
  10. Alternatively, this file may be used under the Apache License, Version 2.0 (the
  11. "Apache License"); you may not use this file except in compliance with the
  12. Apache License. You may obtain a copy of the Apache License at
  13. http://www.apache.org/licenses/LICENSE-2.0.
  14. Unless required by applicable law or agreed to in writing, software distributed
  15. under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
  16. OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
  17. the specific language governing permissions and limitations under the License.
  18. Copyright (c) 2023 Audiokinetic Inc.
  19. *******************************************************************************/
  20. #pragma once
  21. #include <AK/SoundEngine/Common/AkTypes.h>
  22. #include <AK/Tools/Common/AkAssert.h>
  23. //-----------------------------------------------------------------------------
  24. // CAkLock class
  25. //-----------------------------------------------------------------------------
  26. class CAkLock
  27. {
  28. public:
  29. /// Constructor
  30. CAkLock()
  31. {
  32. #if defined(AK_SUPPORT_THREADS)
  33. pthread_mutexattr_t mutex_attr;
  34. AKVERIFY(!pthread_mutexattr_init( &mutex_attr ));
  35. AKVERIFY(!pthread_mutexattr_settype( &mutex_attr, PTHREAD_MUTEX_RECURSIVE ));
  36. AKVERIFY(!pthread_mutex_init( &m_mutex, &mutex_attr));
  37. AKVERIFY(!pthread_mutexattr_destroy( &mutex_attr ));
  38. #endif
  39. }
  40. /// Destructor
  41. ~CAkLock()
  42. {
  43. #if defined(AK_SUPPORT_THREADS)
  44. AKVERIFY(!pthread_mutex_destroy( &m_mutex ));
  45. #endif
  46. }
  47. /// Lock
  48. inline AKRESULT Lock( void )
  49. {
  50. #if defined(AK_SUPPORT_THREADS)
  51. if( !pthread_mutex_lock(&m_mutex) )
  52. {
  53. return AK_Success;
  54. }
  55. return AK_Fail;
  56. #else
  57. return AK_Success;
  58. #endif
  59. }
  60. /// Unlock
  61. inline AKRESULT Unlock( void )
  62. {
  63. #if defined(AK_SUPPORT_THREADS)
  64. if( !pthread_mutex_unlock(&m_mutex) )
  65. {
  66. return AK_Success;
  67. }
  68. return AK_Fail;
  69. #else
  70. return AK_Success;
  71. #endif
  72. }
  73. inline bool TryLock()
  74. {
  75. #if defined(AK_SUPPORT_THREADS)
  76. return pthread_mutex_trylock(&m_mutex);
  77. #else
  78. return true;
  79. #endif
  80. }
  81. private:
  82. #if defined(AK_SUPPORT_THREADS)
  83. pthread_mutex_t m_mutex;
  84. #endif
  85. };