Version
menu_open
link
Target Platform(s):
Wwise SDK 2023.1.4
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 <Windows.h>
32 
33 
34 // Sleep of 1 is as close as we can get on Microsoft platforms
35 // SwitchToThread() is liable to cause the current thread to be unscheduled for 10-30ms
36 #define AkThreadYield() Sleep(1);
37 
38 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 typedef volatile long AkAtomic32;
44 typedef volatile long long AkAtomic64;
45 typedef volatile void* AkAtomicPtr;
46 
47 #if defined( _M_ARM )
48 #define AK_ATOMIC_FENCE_FULL_BARRIER() __dmb( _ARM_BARRIER_ISH )
49 #elif defined( _M_ARM64 )
50 #define AK_ATOMIC_FENCE_FULL_BARRIER() __dmb( _ARM64_BARRIER_ISH )
51 #else
52 #define AK_ATOMIC_FENCE_FULL_BARRIER() MemoryBarrier();
53 #endif
54 
55 #if defined( _M_ARM ) || defined( _M_ARM64 )
56 __forceinline long AkAtomicLoad32( AkAtomic32* pSrc ) { long tmp; tmp = *pSrc; AK_ATOMIC_FENCE_FULL_BARRIER(); return tmp; }
57 #else
58 __forceinline long AkAtomicLoad32( AkAtomic32* pSrc ) { return *pSrc; }
59 #endif
60 __forceinline void AkAtomicStore32( AkAtomic32* pDest, long value ) { InterlockedExchange(pDest, value); }
61 __forceinline long AkAtomicInc32( AkAtomic32* pValue ) { return InterlockedExchangeAdd( pValue, 1 ) + 1; }
62 __forceinline long AkAtomicDec32( AkAtomic32* pValue ) { return InterlockedExchangeAdd( pValue, -1 ) - 1; }
63 __forceinline long AkAtomicExchange32( AkAtomic32* pDest, long value ) { return InterlockedExchange( pDest, value ); }
64 __forceinline long AkAtomicAdd32( AkAtomic32* pDest, long value ) { return InterlockedExchangeAdd( pDest, value ) + value; }
65 __forceinline long AkAtomicSub32( AkAtomic32* pDest, long value ) { return InterlockedExchangeAdd( pDest, -value ) - value; }
66 __forceinline long AkAtomicAnd32( AkAtomic32* pDest, long value ) { return InterlockedAnd(pDest, value) & value; }
67 __forceinline long AkAtomicOr32( AkAtomic32* pDest, long value ) { return InterlockedOr(pDest, value) | value; }
68 __forceinline int AkAtomicCas32( AkAtomic32* pDest, long proposed, long expected ) { return InterlockedCompareExchange( pDest, proposed, expected ) == expected ? 1 : 0; }
69 
70 #ifdef _WIN64
71 #if defined( _M_ARM ) || defined( _M_ARM64 )
72 __forceinline long long AkAtomicLoad64( AkAtomic64* pSrc ) { long long tmp; tmp = *pSrc; AK_ATOMIC_FENCE_FULL_BARRIER(); return tmp; }
73 #else
74 __forceinline long long AkAtomicLoad64( AkAtomic64* pSrc ) { return *pSrc; }
75 #endif
76 #else
77 __forceinline long long AkAtomicLoad64( AkAtomic64* pSrc ) { return InterlockedCompareExchange64( pSrc, 0, 0 ); }
78 #endif
79 
80 __forceinline void AkAtomicStore64( AkAtomic64* pDest, long long value ) { InterlockedExchange64(pDest, value); }
81 __forceinline long long AkAtomicInc64( AkAtomic64* pValue ) { return InterlockedExchangeAdd64( pValue, 1 ) + 1; }
82 __forceinline long long AkAtomicDec64( AkAtomic64* pValue ) { return InterlockedExchangeAdd64( pValue, - 1 ) - 1; }
83 __forceinline long long AkAtomicExchange64( AkAtomic64* pDest, long long value ) { return InterlockedExchange64( pDest, value ); }
84 __forceinline long long AkAtomicAdd64( AkAtomic64* pDest, long long value ) { return InterlockedExchangeAdd64( pDest, value ) + value; }
85 __forceinline long long AkAtomicSub64( AkAtomic64* pDest, long long value ) { return InterlockedExchangeAdd64( pDest, -value ) - value; }
86 __forceinline long long AkAtomicAnd64( AkAtomic64* pDest, long long value ) { return InterlockedAnd64(pDest, value) & value; }
87 __forceinline long long AkAtomicOr64( AkAtomic64* pDest, long long value ) { return InterlockedOr64(pDest, value) | value; }
88 __forceinline int AkAtomicCas64( AkAtomic64* pDest, long long proposed, long long expected ) { return InterlockedCompareExchange64( pDest, proposed, expected ) == expected ? 1 : 0; }
89 
90 #if defined( _M_ARM ) || defined( _M_ARM64 )
91 __forceinline void* AkAtomicLoadPtr( AkAtomicPtr* pSrc ) { void* tmp; tmp = ( void* )*pSrc; AK_ATOMIC_FENCE_FULL_BARRIER(); return tmp; }
92 #else
93 __forceinline void* AkAtomicLoadPtr( AkAtomicPtr* pSrc ) { return ( void* )*pSrc; }
94 #endif
95 
96 #ifdef _WIN64
97 __forceinline void AkAtomicStorePtr( AkAtomicPtr* pDest, void* value ) { InterlockedExchangePointer( ( volatile PVOID* )pDest, value ); }
98 __forceinline void* AkAtomicExchangePtr( AkAtomicPtr* pDest, void* value ) { return InterlockedExchangePointer( ( volatile PVOID* )pDest, value ); }
99 __forceinline int AkAtomicCasPtr( AkAtomicPtr* pDest, void* proposed, void* expected ) { return InterlockedCompareExchangePointer( ( volatile PVOID* )pDest, proposed, expected ) == expected ? 1 : 0; }
100 #else
101 __forceinline void AkAtomicStorePtr( AkAtomicPtr* pDest, void* value ) { InterlockedExchangePointer( ( void** )pDest, value ); }
102 __forceinline void* AkAtomicExchangePtr(AkAtomicPtr* pDest, void* value) { return InterlockedExchangePointer( ( void** )pDest, value ); }
103 __forceinline int AkAtomicCasPtr(AkAtomicPtr* pDest, void* proposed, void* expected) { return InterlockedCompareExchangePointer( ( void** )pDest, proposed, expected ) == expected ? 1 : 0; }
104 #endif
105 
106 #if defined(_MSC_VER)
107 __forceinline void AkSpinHint(void)
108 {
109 #if defined(_M_IX86) || defined(_M_X64)
110  _mm_pause();
111 #elif defined( _M_ARM ) || defined( _M_ARM64 )
112  __yield();
113 #else
114  #error Unsupported platform for AkSpinHint
115 #endif
116 }
117 #endif
118 
119 #ifdef __cplusplus
120 }
121 #endif
void AkSpinHint()
Definition: AkAtomic.h:36
#define AK_ATOMIC_FENCE_FULL_BARRIER()
Definition: AkAtomic.h:52
__forceinline long AkAtomicAdd32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:64
__forceinline int AkAtomicCas64(AkAtomic64 *pDest, long long proposed, long long expected)
Definition: AkAtomic.h:88
__forceinline void * AkAtomicExchangePtr(AkAtomicPtr *pDest, void *value)
Definition: AkAtomic.h:102
__forceinline void AkAtomicStore64(AkAtomic64 *pDest, long long value)
Definition: AkAtomic.h:80
__forceinline long long AkAtomicOr64(AkAtomic64 *pDest, long long value)
Definition: AkAtomic.h:87
__forceinline int AkAtomicCas32(AkAtomic32 *pDest, long proposed, long expected)
Definition: AkAtomic.h:68
volatile uint64_t AkAtomic64
Definition: AkAtomic.h:46
__forceinline long long AkAtomicDec64(AkAtomic64 *pValue)
Definition: AkAtomic.h:82
__forceinline void AkAtomicStorePtr(AkAtomicPtr *pDest, void *value)
Definition: AkAtomic.h:101
volatile uint32_t AkAtomic32
Definition: AkAtomic.h:45
__forceinline long AkAtomicDec32(AkAtomic32 *pValue)
Definition: AkAtomic.h:62
__forceinline long long AkAtomicAnd64(AkAtomic64 *pDest, long long value)
Definition: AkAtomic.h:86
__forceinline void AkAtomicStore32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:60
__forceinline long long AkAtomicSub64(AkAtomic64 *pDest, long long value)
Definition: AkAtomic.h:85
__forceinline long long AkAtomicExchange64(AkAtomic64 *pDest, long long value)
Definition: AkAtomic.h:83
__forceinline long AkAtomicInc32(AkAtomic32 *pValue)
Definition: AkAtomic.h:61
__forceinline long long AkAtomicLoad64(AkAtomic64 *pSrc)
Definition: AkAtomic.h:77
__forceinline long long AkAtomicAdd64(AkAtomic64 *pDest, long long value)
Definition: AkAtomic.h:84
__forceinline long long AkAtomicInc64(AkAtomic64 *pValue)
Definition: AkAtomic.h:81
__forceinline long AkAtomicAnd32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:66
__forceinline long AkAtomicSub32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:65
__forceinline long AkAtomicOr32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:67
__forceinline void * AkAtomicLoadPtr(AkAtomicPtr *pSrc)
Definition: AkAtomic.h:93
__forceinline long AkAtomicExchange32(AkAtomic32 *pDest, long value)
Definition: AkAtomic.h:63
volatile void * AkAtomicPtr
Definition: AkAtomic.h:47
__forceinline long AkAtomicLoad32(AkAtomic32 *pSrc)
Definition: AkAtomic.h:58
__forceinline int AkAtomicCasPtr(AkAtomicPtr *pDest, void *proposed, void *expected)
Definition: AkAtomic.h:103

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