Version
menu_open
link
Wwise SDK 2023.1.3
AkPlatformFuncs.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 #pragma once
28 
30 
31 namespace AKPLATFORM
32 {
33 
34  // --------------------------------------------------------------------
35  // Custom definition for AkLimitedSpin functions
36 #if defined(AK_XBOXSERIESX) // monitorx/waitx is not available on XB1 hardware, only XSX
37 
38  // Waits for a limited amount of time for in_pVal to hit zero (without yielding the thread)
39  inline void AkLimitedSpinForZero(AkAtomic32* in_pVal)
40  {
41  // monitorx and waitx are available on certain AMD CPUs, so we can have a custom impl of AkLimitedSpinForZero
42  AkInt64 endSpinTime = 0;
43  AkInt64 currentTime = 0;
44  PerformanceCounter(&endSpinTime);
45  endSpinTime += AkInt64(AK::g_fFreqRatio * 0.01); // only spin for about 10us
46  while (true)
47  {
48  // set up monitorx on pVal
49  _mm_monitorx((void*)in_pVal, 0U, 0U);
50  // if pval is zero, skip out
51  if (AkAtomicLoad32(in_pVal) == 0)
52  {
53  break;
54  }
55  // wait until a store to pVal occurs (or ~1us passes)
56  _mm_mwaitx(2U, 0U, 1000U);
57 
58  // Check if we've hit the deadline for the timeout
59  PerformanceCounter(&currentTime);
60  if (currentTime > endSpinTime)
61  {
62  break;
63  }
64  }
65  }
66 
67  // Waits for a limited amount of time for in_pVal to get atomically shift from the expected value to the proposed one
68  // returns true if acquisition succeeded
69  inline bool AkLimitedSpinToAcquire(AkAtomic32* in_pVal, AkInt32 in_proposed, AkInt32 in_expected)
70  {
71  if (AkAtomicCas32(in_pVal, in_proposed, in_expected))
72  {
73  return true;
74  }
75 
76  // Cas failed, start the slower evaluation
77 
78  // monitorx and waitx are available on certain AMD CPUs, so we can use monitorx/waitx
79  AkInt64 endSpinTime = 0;
80  AkInt64 currentTime = 0;
81  PerformanceCounter(&endSpinTime);
82  endSpinTime += AkInt64(AK::g_fFreqRatio * 0.01); // only spin for about 10us
83  while (true)
84  {
85  // set up monitorx on pVal
86  _mm_monitorx((void*)in_pVal, 0U, 0U);
87  // attempt cas to acquire and if successful, skip out
88  if (AkAtomicCas32(in_pVal, in_proposed, in_expected))
89  {
90  return true;
91  }
92  // wait until a store to pVal occurs (or ~1us passes)
93  _mm_mwaitx(2U, 0U, 1000U);
94 
95  // Check if we've hit the deadline for the timeout
96  PerformanceCounter(&currentTime);
97  if (currentTime > endSpinTime)
98  {
99  return false;
100  }
101  }
102  }
103 
104 #define AK_LIMITEDSPINFORZERO // mark AkLimitedSpinForZero as defined to avoid duplicate definitions
105 #endif // defined(AK_XBOXSERIESX)
106 }
Platform-dependent helpers.
Definition: AkBitFuncs.h:43
__forceinline int AkAtomicCas32(AkAtomic32 *pDest, long proposed, long expected)
Definition: AkAtomic.h:68
int32_t AkInt32
Signed 32-bit integer.
void PerformanceCounter(AkInt64 *out_piLastTime)
Platform Independent Helper.
volatile uint32_t AkAtomic32
Definition: AkAtomic.h:45
void AkLimitedSpinForZero(AkAtomic32 *in_pVal)
bool AkLimitedSpinToAcquire(AkAtomic32 *in_pVal, AkInt32 in_proposed, AkInt32 in_expected)
AkReal32 g_fFreqRatio
int64_t AkInt64
Signed 64-bit integer.
__forceinline long AkAtomicLoad32(AkAtomic32 *pSrc)
Definition: AkAtomic.h:58

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