AkAtomic.h 6.0 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. // AkAtomic.h
  21. #pragma once
  22. #include <stddef.h>
  23. #include <stdint.h>
  24. #include <time.h>
  25. #define AkThreadYield() { \
  26. struct timespec _akthreadyieldts; \
  27. _akthreadyieldts.tv_sec = 0; \
  28. _akthreadyieldts.tv_nsec = 1; \
  29. nanosleep(&_akthreadyieldts, NULL); \
  30. } \
  31. inline void AkSpinHint(void)
  32. {
  33. #if defined(__x86_64__) || defined(__i386__)
  34. __asm__ volatile("pause" ::: "memory");
  35. #elif defined(__aarch64__) || (defined(__arm__) && __ARM_ARCH >= 7)
  36. __asm__ volatile("yield" ::: "memory");
  37. #else
  38. #error Unsupported platform for AkSpinHint
  39. #endif
  40. //No spin hint supported on this CPU
  41. }
  42. #ifdef __cplusplus
  43. extern "C" {
  44. #endif
  45. typedef volatile int32_t AkAtomic32;
  46. typedef volatile int64_t AkAtomic64;
  47. typedef volatile void* AkAtomicPtr;
  48. #define AK_ATOMIC_FENCE_FULL_BARRIER() __sync_synchronize();
  49. static inline int32_t AkAtomicLoad32( AkAtomic32* pSrc ) { int32_t ret; __atomic_load( ( int32_t* )pSrc, &ret, __ATOMIC_ACQUIRE ); return ret; }
  50. static inline void AkAtomicStore32( AkAtomic32* pDest, int32_t value ) { __atomic_store( ( int32_t* )pDest, &value, __ATOMIC_SEQ_CST); }
  51. static inline int32_t AkAtomicInc32( AkAtomic32* pValue ) { return __sync_add_and_fetch( ( int32_t* )pValue, 1 ); }
  52. static inline int32_t AkAtomicDec32( AkAtomic32* pValue ) { return __sync_sub_and_fetch( ( int32_t* )pValue, 1 ); }
  53. static inline int32_t AkAtomicExchange32( AkAtomic32* pDest, int32_t value ) { return __sync_lock_test_and_set( ( int32_t* )pDest, value ); }
  54. static inline int32_t AkAtomicAdd32( AkAtomic32* pDest, int32_t value ) { return __sync_add_and_fetch( ( int32_t* )pDest, value ); }
  55. static inline int32_t AkAtomicSub32( AkAtomic32* pDest, int32_t value ) { return __sync_sub_and_fetch( ( int32_t* )pDest, value ); }
  56. static inline int32_t AkAtomicAnd32( AkAtomic32* pDest, int32_t value ) { return __sync_and_and_fetch( ( int32_t* )pDest, value ); }
  57. static inline int32_t AkAtomicOr32( AkAtomic32* pDest, int32_t value ) { return __sync_or_and_fetch( ( int32_t* )pDest, value ); }
  58. static inline int AkAtomicCas32( AkAtomic32* pDest, int32_t proposed, int32_t expected ) { return __sync_bool_compare_and_swap( ( int32_t* )pDest, expected, proposed ); }
  59. #if defined( __i386 ) || ( defined( __ARM_ARCH ) && ( __ARM_ARCH <= 7 ) )
  60. static inline int64_t AkAtomicLoad64( AkAtomic64* pSrc ) { return __sync_val_compare_and_swap( pSrc, 0, 0 ); }
  61. static inline void AkAtomicStore64( AkAtomic64* pDest, int64_t value ) { int64_t tmp; do { tmp = *pDest; } while( __sync_val_compare_and_swap( pDest, tmp, value ) != tmp ); }
  62. #else
  63. static inline int64_t AkAtomicLoad64( AkAtomic64* pSrc ) { int64_t ret; __atomic_load( ( int64_t* )pSrc, &ret, __ATOMIC_ACQUIRE ); return ret; }
  64. static inline void AkAtomicStore64( AkAtomic64* pDest, int64_t value ) { __atomic_store( ( int64_t* )pDest, &value, __ATOMIC_SEQ_CST ); }
  65. #endif
  66. static inline int64_t AkAtomicInc64( AkAtomic64* pValue ) { return __sync_add_and_fetch( ( int64_t* )pValue, 1 ); }
  67. static inline int64_t AkAtomicDec64( AkAtomic64* pValue ) { return __sync_sub_and_fetch( ( int64_t* )pValue, 1 ); }
  68. static inline int64_t AkAtomicExchange64( AkAtomic64* pDest, int64_t value ) { return __sync_lock_test_and_set( ( int64_t* )pDest, value ); }
  69. static inline int64_t AkAtomicAdd64( AkAtomic64* pDest, int64_t value ) { return __sync_add_and_fetch( ( int64_t* )pDest, value ); }
  70. static inline int64_t AkAtomicSub64( AkAtomic64* pDest, int64_t value ) { return __sync_sub_and_fetch( ( int64_t* )pDest, value ); }
  71. static inline int64_t AkAtomicAnd64( AkAtomic64* pDest, int64_t value ) { return __sync_and_and_fetch( ( int64_t* )pDest, value ); }
  72. static inline int64_t AkAtomicOr64( AkAtomic64* pDest, int64_t value ) { return __sync_or_and_fetch( ( int64_t* )pDest, value ); }
  73. static inline int AkAtomicCas64( AkAtomic64* pDest, int64_t proposed, int64_t expected ) { return __sync_bool_compare_and_swap( ( int64_t* )pDest, expected, proposed ); }
  74. static inline void* AkAtomicLoadPtr( AkAtomicPtr* pSrc ) { size_t ret; __atomic_load( ( size_t* )pSrc, &ret, __ATOMIC_ACQUIRE ); return ( void* )ret; }
  75. static inline void AkAtomicStorePtr( AkAtomicPtr* pDest, void* value ) { __atomic_store( ( size_t* )pDest, ( size_t* )&value, __ATOMIC_SEQ_CST); }
  76. static inline void* AkAtomicExchangePtr( AkAtomicPtr* pDest, void* value ) { return ( void* )__sync_lock_test_and_set( ( void** )pDest, value ); }
  77. static inline int AkAtomicCasPtr( AkAtomicPtr* pDest, void* proposed, void* expected ) { return __sync_bool_compare_and_swap( ( void** )pDest, expected, proposed ); }
  78. #ifdef __cplusplus
  79. }
  80. #endif