28 #ifndef _AK_PLATFORM_FUNCS_H_
29 #define _AK_PLATFORM_FUNCS_H_
31 #include <AK/SoundEngine/Common/AkTypes.h>
32 #include <AK/Tools/Common/AkAssert.h>
33 #include <sce_atomic.h>
39 #include <kernel\eventflag.h>
69 #define AK_DECLARE_THREAD_ROUTINE( FuncName ) void* FuncName(void* lpParameter)
70 #define AK_THREAD_RETURN( _param_ ) return (_param_);
71 #define AK_THREAD_ROUTINE_PARAMETER lpParameter
72 #define AK_GET_THREAD_ROUTINE_PARAMETER_PTR(type) reinterpret_cast<type*>( AK_THREAD_ROUTINE_PARAMETER )
74 #define AK_RETURN_THREAD_OK 0x00000000
75 #define AK_RETURN_THREAD_ERROR 0x00000001
76 #define AK_DEFAULT_STACK_SIZE (128*1024)
77 #define AK_THREAD_DEFAULT_SCHED_POLICY SCE_KERNEL_SCHED_FIFO
78 #define AK_THREAD_PRIORITY_NORMAL SCE_KERNEL_PRIO_FIFO_DEFAULT
79 #define AK_THREAD_PRIORITY_ABOVE_NORMAL SCE_KERNEL_PRIO_FIFO_HIGHEST
80 #define AK_THREAD_PRIORITY_BELOW_NORMAL SCE_KERNEL_PRIO_FIFO_LOWEST
82 #define AK_THREAD_AFFINITY_ALL 63 // from binary 111111 setting the 6 available core to true. (ex: 4 << 1)
83 #define AK_THREAD_AFFINITY_DEFAULT AK_THREAD_AFFINITY_ALL
86 #define AK_RELEASE_GPU_OFFLINE_FRAME sce::Gnm::submitDone();
89 #define AK_NULL_THREAD NULL
91 #define AK_INFINITE (AK_UINT_MAX)
93 #define AkMax(x1, x2) (((x1) > (x2))? (x1): (x2))
94 #define AkMin(x1, x2) (((x1) < (x2))? (x1): (x2))
95 #define AkClamp(x, min, max) ((x) < (min)) ? (min) : (((x) > (max) ? (max) : (x)))
103 fputs( in_pszMsg, stderr );
108 fputws( in_pszMsg, stderr );
127 return (in_event != NULL);
134 int ret = sceKernelCreateEventFlag(
137 SCE_KERNEL_EVF_ATTR_MULTI,
157 sceKernelDeleteEventFlag(io_event);
164 AKVERIFY( sceKernelWaitEventFlag(
167 SCE_KERNEL_EVF_WAITMODE_OR | SCE_KERNEL_EVF_WAITMODE_CLEAR_ALL,
175 AKVERIFY( sceKernelSetEventFlag( in_event, 1 ) == 0 );
184 return sceAtomicIncrement32( (
volatile SceInt32 *) pValue ) + 1;
190 return sceAtomicDecrement32( (
volatile SceInt32 *) pValue ) - 1;
195 return sceAtomicCompareAndSwap64(io_pDest, in_expectedOldVal, in_newValue) == in_expectedOldVal;
200 return sceAtomicCompareAndSwap32(io_pDest, in_expectedOldVal, in_newValue) == in_expectedOldVal;
214 return ( *in_pThread != AK_NULL_THREAD );
220 *in_pThread = AK_NULL_THREAD;
226 AKASSERT( in_pThread );
227 AKASSERT( *in_pThread );
234 #define AkExitThread( _result ) return _result; // ?????
239 out_threadProperties.
uStackSize = AK_DEFAULT_STACK_SIZE;
240 out_threadProperties.
uSchedPolicy = AK_THREAD_DEFAULT_SCHED_POLICY;
241 out_threadProperties.
nPriority = AK_THREAD_PRIORITY_NORMAL;
247 AkThreadRoutine pStartRoutine,
250 AkThread * out_pThread,
251 const char * in_szThreadName )
253 AKASSERT( out_pThread != NULL );
258 AKVERIFY(!scePthreadAttrInit(&attr));
260 AKVERIFY(!scePthreadAttrSetstacksize(&attr,in_threadProperties.
uStackSize));
261 AKVERIFY(!scePthreadAttrSetdetachstate(&attr, SCE_PTHREAD_CREATE_JOINABLE));
262 AKVERIFY(!scePthreadAttrSetinheritsched(&attr, SCE_PTHREAD_EXPLICIT_SCHED));
263 AKVERIFY(!scePthreadAttrSetaffinity(&attr,in_threadProperties.
dwAffinityMask));
267 if( scePthreadAttrSetschedpolicy( &attr, sched_policy ) )
269 AKASSERT( !
"AKCreateThread invalid sched policy, will automatically set it to FIFO scheduling" );
270 sched_policy = AK_THREAD_DEFAULT_SCHED_POLICY;
271 AKVERIFY( !scePthreadAttrSetschedpolicy( &attr, sched_policy ));
274 int minPriority, maxPriority;
275 minPriority = SCE_KERNEL_PRIO_FIFO_HIGHEST;
276 maxPriority = SCE_KERNEL_PRIO_FIFO_LOWEST;
279 AKASSERT( in_threadProperties.
nPriority >= minPriority && in_threadProperties.
nPriority <= maxPriority );
280 if( in_threadProperties.
nPriority >= minPriority && in_threadProperties.
nPriority <= maxPriority )
282 SceKernelSchedParam schedParam;
283 AKVERIFY( scePthreadAttrGetschedparam(&attr, &schedParam) == 0 );
284 schedParam.sched_priority = in_threadProperties.
nPriority;
285 AKVERIFY( scePthreadAttrSetschedparam(&attr, &schedParam) == 0 );
289 int threadError = scePthreadCreate(out_pThread, &attr, pStartRoutine, pParams, in_szThreadName);
290 AKASSERT( threadError == 0 );
291 AKVERIFY(!scePthreadAttrDestroy(&attr));
293 if( threadError != 0 )
310 AKASSERT( in_pThread );
311 AKASSERT( *in_pThread );
312 AKVERIFY(!scePthreadJoin( *in_pThread, NULL ));
317 return scePthreadSelf();
321 AkForceInline
void AkSleep( AkUInt32 in_ulMilliseconds )
323 usleep( in_ulMilliseconds * 1000 );
330 AkForceInline
void AkMemCpy(
void * pDest,
const void * pSrc, AkUInt32 uSize )
332 memcpy( pDest, pSrc, uSize );
336 AkForceInline
void AkMemSet(
void * pDest, AkInt32 iVal, AkUInt32 uSize )
338 memset( pDest, iVal, uSize );
347 uint64_t uTime = sceKernelGetProcessTimeCounter();
348 *out_piLastTime = (AkInt64)uTime;
354 *out_piFreq = (AkInt64)sceKernelGetProcessTimeCounterFrequency();
366 AkForceInline AkReal32
Elapsed(
const AkInt64 & in_iNow,
const AkInt64 & in_iStart )
373 AkUInt32 in_uiOutBufferSize,
374 char* io_pszAnsiString )
376 AKASSERT( io_pszAnsiString != NULL );
379 memset (&state,
'\0',
sizeof (state));
381 return (AkInt32)wcsrtombs(io_pszAnsiString,
382 &in_pszUnicodeString,
390 AkUInt32 in_uiOutBufferSize,
391 void* io_pvUnicodeStringBuffer )
393 AKASSERT( io_pvUnicodeStringBuffer != NULL );
396 memset (&state,
'\0',
sizeof (state));
398 return (AkInt32)mbsrtowcs((
wchar_t*)io_pvUnicodeStringBuffer,
405 AkUInt32 in_uiOutBufferSize,
406 void* io_pvUnicodeStringBuffer )
408 return AkCharToWideChar( in_pszUtf8String, in_uiOutBufferSize, (
wchar_t*)io_pvUnicodeStringBuffer );
412 AkForceInline
void SafeStrCpy(
wchar_t * in_pDest,
const wchar_t* in_pSrc,
size_t in_uDestMaxNumChars )
414 size_t uSizeCopy = AkMin( in_uDestMaxNumChars - 1, wcslen( in_pSrc ) + 1 );
415 wcsncpy( in_pDest, in_pSrc, uSizeCopy );
416 in_pDest[uSizeCopy] =
'\0';
420 AkForceInline
void SafeStrCpy(
char * in_pDest,
const char* in_pSrc,
size_t in_uDestMaxNumChars )
422 size_t uSizeCopy = AkMin( in_uDestMaxNumChars - 1, strlen( in_pSrc ) + 1 );
423 strncpy( in_pDest, in_pSrc, uSizeCopy );
424 in_pDest[uSizeCopy] =
'\0';
428 AkForceInline
void SafeStrCat(
wchar_t * in_pDest,
const wchar_t* in_pSrc,
size_t in_uDestMaxNumChars )
430 size_t uAvailableSize = ( in_uDestMaxNumChars - wcslen( in_pDest ) - 1 );
431 wcsncat( in_pDest, in_pSrc, AkMin( uAvailableSize, wcslen( in_pSrc ) ) );
435 AkForceInline
void SafeStrCat(
char * in_pDest,
const char* in_pSrc,
size_t in_uDestMaxNumChars )
437 size_t uAvailableSize = ( in_uDestMaxNumChars - strlen( in_pDest ) - 1 );
438 strncat( in_pDest, in_pSrc, AkMin( uAvailableSize, strlen( in_pSrc ) ) );
442 #define AkAlloca( _size_ ) alloca( _size_ )
454 #define CONVERT_WIDE_TO_OSCHAR( _wstring_, _oscharstring_ ) \
455 _oscharstring_ = (AkOSChar*)AkAlloca( (1 + wcslen( _wstring_ )) * sizeof(AkOSChar) ); \
456 AKPLATFORM::AkWideCharToChar( _wstring_ , (AkUInt32)(1 + wcslen( _wstring_ )), (AkOSChar*)( _oscharstring_ ) )
467 #define CONVERT_CHAR_TO_OSCHAR( _astring_, _oscharstring_ ) ( _oscharstring_ ) = (AkOSChar*)( _astring_ )
477 #define CONVERT_OSCHAR_TO_WIDE( _osstring_, _wstring_ ) \
478 _wstring_ = (wchar_t*)AkAlloca((1+strlen(_osstring_)) * sizeof(wchar_t)); \
479 AKPLATFORM::AkCharToWideChar( _osstring_, (AkUInt32)(1 + strlen(_osstring_ )), _wstring_ )
489 #define CONVERT_OSCHAR_TO_CHAR( _osstring_, _astring_ ) _astring_ = (char*)_osstring_
495 return ( wcslen( in_pStr ) );
500 AkForceInline
size_t OsStrLen(
const AkOSChar* in_pszString )
502 return ( strlen( in_pszString ) );
506 #define AK_OSPRINTF snprintf
514 AkForceInline
int OsStrCmp(
const AkOSChar* in_pszString1,
const AkOSChar* in_pszString2 )
516 return ( strcmp( in_pszString1, in_pszString2 ) );
525 inline int OsStrNCmp(
const AkOSChar* in_pszString1,
const AkOSChar* in_pszString2,
size_t in_MaxCountSize )
527 return ( strncmp(in_pszString1, in_pszString2, in_MaxCountSize) );
530 #define AK_UTF16_TO_WCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::SafeStrCpy( in_pdDest, in_pSrc, in_MaxSize )
531 #define AK_WCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::SafeStrCpy( in_pdDest, in_pSrc, in_MaxSize )
532 #define AK_UTF16_TO_OSCHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkWideCharToChar( in_pSrc, in_MaxSize, in_pdDest )
533 #define AK_UTF16_TO_CHAR( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkWideCharToChar( in_pSrc, in_MaxSize, in_pdDest )
534 #define AK_CHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkCharToWideChar( in_pSrc, in_MaxSize, in_pdDest )
535 #define AK_OSCHAR_TO_UTF16( in_pdDest, in_pSrc, in_MaxSize ) AKPLATFORM::AkCharToWideChar( in_pSrc, in_MaxSize, in_pdDest )
538 #define AK_PATH_SEPARATOR ("/")
539 #define AK_LIBRARY_PREFIX ("")
540 #define AK_DYNAMIC_LIBRARY_EXTENSION (".prx")
544 #ifdef AK_ENABLE_INSTRUMENT
547 #include <sdk_version.h>
548 #if SCE_ORBIS_SDK_VERSION >= 0x04500000
549 #include <razorcpu.h>
550 #ifndef SCE_RAZOR_MARKER_DISABLE_HUD
551 #define SCE_RAZOR_MARKER_DISABLE_HUD 0
555 class AkInstrumentScope
558 inline AkInstrumentScope(
const char *in_pszZoneName )
560 sceRazorCpuPushMarkerStatic( in_pszZoneName, 0, SCE_RAZOR_MARKER_DISABLE_HUD );
563 inline ~AkInstrumentScope()
565 sceRazorCpuPopMarker();
569 #define AK_INSTRUMENT_BEGIN( _zone_name_ ) sceRazorCpuPushMarkerStatic( text, 0, SCE_RAZOR_MARKER_DISABLE_HUD )
570 #define AK_INSTRUMENT_BEGIN_C( _color_, _zone_name_ ) sceRazorCpuPushMarkerStatic( text, _color_, SCE_RAZOR_MARKER_DISABLE_HUD )
571 #define AK_INSTRUMENT_END( _zone_name_ ) sceRazorCpuPopMarker()
572 #define AK_INSTRUMENT_SCOPE( _zone_name_ ) AkInstrumentScope akInstrumentScope_##__LINE__(_zone_name_)
574 #define AK_INSTRUMENT_IDLE_BEGIN( _zone_name_ )
575 #define AK_INSTRUMENT_IDLE_END( _zone_name_ )
576 #define AK_INSTRUMENT_IDLE_SCOPE( _zone_name_ )
578 #define AK_INSTRUMENT_STALL_BEGIN( _zone_name_ )
579 #define AK_INSTRUMENT_STALL_END( _zone_name_ )
580 #define AK_INSTRUMENT_STALL_SCOPE( _zone_name_ )
582 #define AK_INSTRUMENT_THREAD_START( _thread_name_ )
584 #endif // AK_ENABLE_INSTRUMENT
586 #endif // _AK_PLATFORM_FUNCS_H_