Version

menu_open
Target Platform(s):
Wwise SDK 2024.1.4
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) 2025 Audiokinetic Inc.
25 *******************************************************************************/
26 
27 #pragma once
28 
31 #include <android/log.h>
32 #include <stdlib.h>
33 
34 #include <time.h>
35 
36 #define AK_THREAD_INIT_CODE(_threadProperties) syscall(__NR_sched_setaffinity, 0, sizeof(_threadProperties.dwAffinityMask), &_threadProperties.dwAffinityMask)
37 #define AK_SEC_TO_NANOSEC 1000000000ULL
38 
39 namespace AKPLATFORM
40 {
41  /// Platform Independent Helper
42  inline void PerformanceCounter( AkInt64 * out_piLastTime )
43  {
44  struct timespec clockNow;
45  clock_gettime(CLOCK_MONOTONIC, &clockNow);
46  //This give the wallclock time in NS
47  *out_piLastTime = clockNow.tv_sec*AK_SEC_TO_NANOSEC + clockNow.tv_nsec;
48  }
49 
50  /// Platform Independent Helper
51  inline void PerformanceFrequency( AkInt64 * out_piFreq )
52  {
53  //Since Wall Clock is used, 1 NS is the frequency independent of the clock resolution
54  *out_piFreq = AK_SEC_TO_NANOSEC;
55  }
56 }
57 
58 #include <AK/Tools/POSIX/AkPlatformFuncs.h>
59 
60 /// Stack allocations.
61 #define AkAlloca( _size_ ) __builtin_alloca( _size_ )
62 
63 namespace AKPLATFORM
64 {
65  /// Output a debug message on the console (Ansi string)
66 
67 #if defined(AK_OPTIMIZED)
68  inline void OutputDebugMsg( const char* ){}
69 
70  template <int MaxSize = 0> // Unused
71  inline void OutputDebugMsgV( const char* in_pszFmt, ... ) {}
72 #else // AK_ANDROID
73  inline void OutputDebugMsg( const char* in_pszMsg )
74  {
75  // To see output of this
76  // adb logcat ActivityManager:I YourApplication:D AKDEBUG:D *:S
77  __android_log_print(ANDROID_LOG_INFO, "AKDEBUG", "%s", in_pszMsg);
78  }
79 
80  /// Output a debug message on the console (variadic function).
81  template <int MaxSize = 0> // Unused
82  inline void OutputDebugMsgV( const char* in_pszFmt, ...)
83  {
84  va_list args;
85  va_start(args, in_pszFmt);
86  __android_log_vprint(ANDROID_LOG_INFO, "AKDEBUG", in_pszFmt, args);
87  va_end(args);
88  }
89 #endif
90 
91  template<class destType, class srcType>
92  inline size_t AkSimpleConvertString( destType* in_pdDest, const srcType* in_pSrc, size_t in_MaxSize, size_t destStrLen(const destType *), size_t srcStrLen(const srcType *) )
93  {
94  size_t i;
95  size_t lenToCopy = srcStrLen(in_pSrc);
96 
97  lenToCopy = (lenToCopy > in_MaxSize-1) ? in_MaxSize-1 : lenToCopy;
98  for(i = 0; i < lenToCopy; i++)
99  {
100  in_pdDest[i] = (destType) in_pSrc[i];
101  }
102  in_pdDest[lenToCopy] = (destType)0;
103 
104  return lenToCopy;
105  }
106 
107  #define CONVERT_UTF16_TO_CHAR( _astring_, _charstring_ ) \
108  _charstring_ = (char*)AkAlloca( (1 + AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)) * sizeof(char) ); \
109  AK_UTF16_TO_CHAR( _charstring_, (const AkUtf16*)_astring_, AKPLATFORM::AkUtf16StrLen((const AkUtf16*)_astring_)+1 )
110 
111  #define AK_UTF8_TO_OSCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, strlen, strlen )
112  #define AK_UTF16_TO_OSCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
113  #define AK_UTF16_TO_CHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, strlen, AKPLATFORM::AkUtf16StrLen )
114  #define AK_CHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen)
115  #define AK_OSCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkSimpleConvertString( in_pdDest, in_pSrc, in_MaxSize, AKPLATFORM::AkUtf16StrLen, strlen)
116 
117  #if __BIGGEST_ALIGNMENT__ < AK_SIMD_ALIGNMENT
118  #define AkAllocaSIMD( _size_ ) __builtin_alloca_with_align( _size_, AK_SIMD_ALIGNMENT*8 )
119  #endif
120 
121  /// Platform Independent Helper
122  inline void AkCreateThread(
123  AkThreadRoutine pStartRoutine, // Thread routine.
124  void * pParams, // Routine params.
125  const AkThreadProperties & in_threadProperties, // Properties. NULL for default.
126  AkThread * out_pThread, // Returned thread handle.
127  const char * /*in_szThreadName*/ ) // Opt thread name.
128  {
129  AKASSERT( out_pThread != NULL );
130 
131  pthread_attr_t attr;
132 
133  // Create the attr
134  AKVERIFY(!pthread_attr_init(&attr));
135  // Set the stack size
136  AKVERIFY(!pthread_attr_setstacksize(&attr,in_threadProperties.uStackSize));
137 
138  AKVERIFY(!pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE));
139 
140  // Create the tread
141  int threadError = pthread_create( out_pThread, &attr, pStartRoutine, pParams);
142  AKASSERT( threadError == 0 );
143  AKVERIFY(!pthread_attr_destroy(&attr));
144 
145  if( threadError != 0 )
146  {
147  AkClearThread( out_pThread );
148  return;
149  }
150 
151  // ::CreateThread() return NULL if it fails.
152  if ( !*out_pThread )
153  {
154  AkClearThread( out_pThread );
155  return;
156  }
157 
158  // Try to set the thread policy
159  int sched_policy = in_threadProperties.uSchedPolicy;
160 
161  // Get the priority for the policy
162  int minPriority, maxPriority;
163  minPriority = sched_get_priority_min(sched_policy);
164  maxPriority = sched_get_priority_max(sched_policy);
165 
166  // Set the thread priority if valid
167  sched_param schedParam;
168  schedParam.sched_priority = in_threadProperties.nPriority;
169  AKASSERT( in_threadProperties.nPriority >= minPriority && in_threadProperties.nPriority <= maxPriority );
170 
171  //pthread_setschedparam WILL fail on Android Lollipop when used with SCHED_FIFO (the default). Not allowed anymore. (ignore the error code).
172  int err = pthread_setschedparam(*out_pThread, sched_policy, &schedParam);
173  if (err != 0)
174  {
175  //Make sure the priority is well set, even if the policy could not.
176  sched_policy = SCHED_NORMAL;
177  minPriority = sched_get_priority_min(sched_policy);
178  maxPriority = sched_get_priority_max(sched_policy);
179  if (in_threadProperties.nPriority == AK_THREAD_PRIORITY_ABOVE_NORMAL)
180  schedParam.sched_priority = maxPriority;
181  else if (in_threadProperties.nPriority == AK_THREAD_PRIORITY_BELOW_NORMAL)
182  schedParam.sched_priority = minPriority;
183  else
184  schedParam.sched_priority = (maxPriority + minPriority) / 2;
185  err = pthread_setschedparam(*out_pThread, sched_policy, &schedParam);
186  AKASSERT(err == 0);
187  }
188  }
189 
190  #define AK_FILEHANDLE_TO_UINTPTR(_h) ((AkUIntPtr)_h)
191  #define AK_SET_FILEHANDLE_TO_UINTPTR(_h,_u) _h = (AkFileHandle)_u
192 
193 }
int nPriority
Thread priority.
Platform-dependent helpers.
void OutputDebugMsg(const char *in_pszMsg)
Output a debug message on the console (Ansi string)
#define NULL
Definition: AkTypes.h:46
#define AK_THREAD_PRIORITY_ABOVE_NORMAL
void AkCreateThread(AkThreadRoutine pStartRoutine, void *pParams, const AkThreadProperties &in_threadProperties, AkThread *out_pThread, const char *)
Platform Independent Helper.
void OutputDebugMsgV(const char *in_pszFmt,...)
Output a debug message on the console (variadic function).
void PerformanceCounter(AkInt64 *out_piLastTime)
Platform Independent Helper.
nn::os::ThreadFunction AkThreadRoutine
Thread routine.
Definition: AkTypes.h:90
#define AKASSERT(Condition)
Definition: AkAssert.h:67
#define AKVERIFY(x)
Definition: AkAssert.h:69
#define AK_SEC_TO_NANOSEC
AkForceInline void AkClearThread(AkThread *in_pThread)
Platform Independent Helper.
int64_t AkInt64
Signed 64-bit integer.
size_t uStackSize
Thread stack size.
void PerformanceFrequency(AkInt64 *out_piFreq)
Platform Independent Helper.
int uSchedPolicy
Thread scheduling policy.
#define AK_THREAD_PRIORITY_BELOW_NORMAL
size_t AkSimpleConvertString(destType *in_pdDest, const srcType *in_pSrc, size_t in_MaxSize, size_t destStrLen(const destType *), size_t srcStrLen(const srcType *))

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