Version
menu_open
link
Target Platform(s):
Wwise SDK 2022.1.11
AkAtomic.h
Go to the documentation of this file.
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 
5 Commercial License Usage
6 
7 Licensees holding valid commercial licenses to the AUDIOKINETIC Wwise Technology
8 may use this file in accordance with the end user license agreement provided
9 with the software or, alternatively, in accordance with the terms contained in a
10 written agreement between you and Audiokinetic Inc.
11 
12 Apache License Usage
13 
14 Alternatively, this file may be used under the Apache License, Version 2.0 (the
15 "Apache License"); you may not use this file except in compliance with the
16 Apache License. You may obtain a copy of the Apache License at
17 http://www.apache.org/licenses/LICENSE-2.0.
18 
19 Unless required by applicable law or agreed to in writing, software distributed
20 under the Apache License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
21 OR CONDITIONS OF ANY KIND, either express or implied. See the Apache License for
22 the specific language governing permissions and limitations under the License.
23 
24  Copyright (c) 2024 Audiokinetic Inc.
25 *******************************************************************************/
26 
27 // AkAtomic.h
28 
29 #pragma once
30 
31 #include <stddef.h>
32 #include <stdint.h>
33 #include <time.h>
34 
35 #define AkThreadYield() { \
36  struct timespec _akthreadyieldts; \
37  _akthreadyieldts.tv_sec = 0; \
38  _akthreadyieldts.tv_nsec = 1; \
39  nanosleep(&_akthreadyieldts, NULL); \
40  } \
41 
42 inline void AkSpinHint(void)
43 {
44 #if defined(__x86_64__) || defined(__i386__)
45  __asm__ volatile("pause" ::: "memory");
46 #elif defined(__aarch64__) || (defined(__arm__) && __ARM_ARCH >= 7)
47  __asm__ volatile("yield" ::: "memory");
48 #else
49  #error Unsupported platform for AkSpinHint
50 #endif
51  //No spin hint supported on this CPU
52 }
53 
54 #ifdef __cplusplus
55 extern "C" {
56 #endif
57 
58 typedef volatile int32_t AkAtomic32;
59 typedef volatile int64_t AkAtomic64;
60 typedef volatile void* AkAtomicPtr;
61 
62 #define AK_ATOMIC_FENCE_FULL_BARRIER() __sync_synchronize();
63 
64 static inline int32_t AkAtomicLoad32( AkAtomic32* pSrc ) { int32_t ret; __atomic_load( ( int32_t* )pSrc, &ret, __ATOMIC_SEQ_CST); return ret; }
65 static inline void AkAtomicStore32( AkAtomic32* pDest, int32_t value ) { __atomic_store( ( int32_t* )pDest, &value, __ATOMIC_SEQ_CST); }
66 static inline int32_t AkAtomicInc32( AkAtomic32* pValue ) { return __sync_add_and_fetch( ( int32_t* )pValue, 1 ); }
67 static inline int32_t AkAtomicDec32( AkAtomic32* pValue ) { return __sync_sub_and_fetch( ( int32_t* )pValue, 1 ); }
68 static inline int32_t AkAtomicExchange32( AkAtomic32* pDest, int32_t value ) { return __sync_lock_test_and_set( ( int32_t* )pDest, value ); }
69 static inline int32_t AkAtomicAdd32( AkAtomic32* pDest, int32_t value ) { return __sync_add_and_fetch( ( int32_t* )pDest, value ); }
70 static inline int32_t AkAtomicSub32( AkAtomic32* pDest, int32_t value ) { return __sync_sub_and_fetch( ( int32_t* )pDest, value ); }
71 static inline int32_t AkAtomicAnd32( AkAtomic32* pDest, int32_t value ) { return __sync_and_and_fetch( ( int32_t* )pDest, value ); }
72 static inline int32_t AkAtomicOr32( AkAtomic32* pDest, int32_t value ) { return __sync_or_and_fetch( ( int32_t* )pDest, value ); }
73 static inline int AkAtomicCas32( AkAtomic32* pDest, int32_t proposed, int32_t expected ) { return __sync_bool_compare_and_swap( ( int32_t* )pDest, expected, proposed ); }
74 
75 #if defined( __i386 ) || ( defined( __ARM_ARCH ) && ( __ARM_ARCH <= 7 ) )
76 static inline int64_t AkAtomicLoad64( AkAtomic64* pSrc ) { return __sync_val_compare_and_swap( pSrc, 0, 0 ); }
77 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 ); }
78 #else
79 static inline int64_t AkAtomicLoad64( AkAtomic64* pSrc ) { int64_t ret; __atomic_load( ( int64_t* )pSrc, &ret, __ATOMIC_SEQ_CST); return ret; }
80 static inline void AkAtomicStore64( AkAtomic64* pDest, int64_t value ) { __atomic_store( ( int64_t* )pDest, &value, __ATOMIC_SEQ_CST ); }
81 #endif
82 
83 static inline int64_t AkAtomicInc64( AkAtomic64* pValue ) { return __sync_add_and_fetch( ( int64_t* )pValue, 1 ); }
84 static inline int64_t AkAtomicDec64( AkAtomic64* pValue ) { return __sync_sub_and_fetch( ( int64_t* )pValue, 1 ); }
85 static inline int64_t AkAtomicExchange64( AkAtomic64* pDest, int64_t value ) { return __sync_lock_test_and_set( ( int64_t* )pDest, value ); }
86 static inline int64_t AkAtomicAdd64( AkAtomic64* pDest, int64_t value ) { return __sync_add_and_fetch( ( int64_t* )pDest, value ); }
87 static inline int64_t AkAtomicSub64( AkAtomic64* pDest, int64_t value ) { return __sync_sub_and_fetch( ( int64_t* )pDest, value ); }
88 static inline int64_t AkAtomicAnd64( AkAtomic64* pDest, int64_t value ) { return __sync_and_and_fetch( ( int64_t* )pDest, value ); }
89 static inline int64_t AkAtomicOr64( AkAtomic64* pDest, int64_t value ) { return __sync_or_and_fetch( ( int64_t* )pDest, value ); }
90 static inline int AkAtomicCas64( AkAtomic64* pDest, int64_t proposed, int64_t expected ) { return __sync_bool_compare_and_swap( ( int64_t* )pDest, expected, proposed ); }
91 
92 static inline void* AkAtomicLoadPtr( AkAtomicPtr* pSrc ) { size_t ret; __atomic_load( ( size_t* )pSrc, &ret, __ATOMIC_SEQ_CST); return ( void* )ret; }
93 static inline void AkAtomicStorePtr( AkAtomicPtr* pDest, void* value ) { __atomic_store( ( size_t* )pDest, ( size_t* )&value, __ATOMIC_SEQ_CST); }
94 static inline void* AkAtomicExchangePtr( AkAtomicPtr* pDest, void* value ) { return ( void* )__sync_lock_test_and_set( ( void** )pDest, value ); }
95 static inline int AkAtomicCasPtr( AkAtomicPtr* pDest, void* proposed, void* expected ) { return __sync_bool_compare_and_swap( ( void** )pDest, expected, proposed ); }
96 
97 #ifdef __cplusplus
98 }
99 #endif
__forceinline long AkAtomicAdd32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:63
volatile void * AkAtomicPtr
Definition: AkAtomic.h:44
__forceinline int AkAtomicCas64(AkAtomic64 *pDest, long long proposed, long long expected)
Definition: AkAtomic.h:87
__forceinline void * AkAtomicExchangePtr(AkAtomicPtr *pDest, void *value)
Definition: AkAtomic.h:101
__forceinline void AkAtomicStore64(AkAtomic64 *pDest, long long value)
Definition: AkAtomic.h:79
__forceinline long long AkAtomicOr64(AkAtomic64 *pDest, long long value)
Definition: AkAtomic.h:86
__forceinline int AkAtomicCas32(AkAtomic32 *pDest, long proposed, long expected)
Definition: AkAtomic.h:67
__forceinline long long AkAtomicDec64(AkAtomic64 *pValue)
Definition: AkAtomic.h:81
__forceinline void AkAtomicStorePtr(AkAtomicPtr *pDest, void *value)
Definition: AkAtomic.h:100
void AkSpinHint(void)
Definition: AkAtomic.h:42
__forceinline long AkAtomicDec32(AkAtomic32 *pValue)
Definition: AkAtomic.h:61
__forceinline long long AkAtomicAnd64(AkAtomic64 *pDest, long long value)
Definition: AkAtomic.h:85
__forceinline void AkAtomicStore32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:59
__forceinline long long AkAtomicSub64(AkAtomic64 *pDest, long long value)
Definition: AkAtomic.h:84
__forceinline long long AkAtomicExchange64(AkAtomic64 *pDest, long long value)
Definition: AkAtomic.h:82
volatile long long AkAtomic64
Definition: AkAtomic.h:43
__forceinline long AkAtomicInc32(AkAtomic32 *pValue)
Definition: AkAtomic.h:60
__forceinline long long AkAtomicLoad64(AkAtomic64 *pSrc)
Definition: AkAtomic.h:76
__forceinline long long AkAtomicAdd64(AkAtomic64 *pDest, long long value)
Definition: AkAtomic.h:83
__forceinline long long AkAtomicInc64(AkAtomic64 *pValue)
Definition: AkAtomic.h:80
volatile long AkAtomic32
Definition: AkAtomic.h:42
__forceinline long AkAtomicAnd32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:65
__forceinline long AkAtomicSub32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:64
__forceinline long AkAtomicOr32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:66
__forceinline void * AkAtomicLoadPtr(AkAtomicPtr *pSrc)
Definition: AkAtomic.h:92
__forceinline long AkAtomicExchange32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:62
__forceinline long AkAtomicLoad32(AkAtomic32 *pSrc)
Definition: AkAtomic.h:57
__forceinline int AkAtomicCasPtr(AkAtomicPtr *pDest, void *proposed, void *expected)
Definition: AkAtomic.h:102

Was this page helpful?

Need Support?

Questions? Problems? Need more info? Contact us, and we can help!

Visit our Support page

Tell us about your project. We're here to help.

Register your project and we'll help you get started with no strings attached!

Get started with Wwise