all repos — mgba @ aefb4b62a16943f090c72262fe8a41edf88a0397

mGBA Game Boy Advance Emulator

Util: Add MutexTryLock
Jeffrey Pfau jeffrey@endrift.com
Thu, 12 Nov 2015 22:50:09 -0800
commit

aefb4b62a16943f090c72262fe8a41edf88a0397

parent

d52a7f3689c53298dc02bd2ebbd66b4213d12dc2

M CHANGESCHANGES

@@ -44,6 +44,7 @@ - Util: Use VFile for configuration

- GBA Memory: Implement several unimplemented memory access types - GBA: Implement bad I/O register loading - GBA Memory: Add GBAView* functions for viewing memory directly without bus issues + - Util: Add MutexTryLock 0.3.1: (2015-10-24) Bugfixes:
M src/gba/supervisor/thread.csrc/gba/supervisor/thread.c

@@ -71,13 +71,15 @@

while (threadContext->state == oldState) { MutexUnlock(&threadContext->stateMutex); - MutexLock(&threadContext->sync.videoFrameMutex); - ConditionWake(&threadContext->sync.videoFrameRequiredCond); - MutexUnlock(&threadContext->sync.videoFrameMutex); + if (!MutexTryLock(&threadContext->sync.videoFrameMutex)) { + ConditionWake(&threadContext->sync.videoFrameRequiredCond); + MutexUnlock(&threadContext->sync.videoFrameMutex); + } - MutexLock(&threadContext->sync.audioBufferMutex); - ConditionWake(&threadContext->sync.audioRequiredCond); - MutexUnlock(&threadContext->sync.audioBufferMutex); + if (!MutexLock(&threadContext->sync.audioBufferMutex)) { + ConditionWake(&threadContext->sync.audioRequiredCond); + MutexUnlock(&threadContext->sync.audioBufferMutex); + } MutexLock(&threadContext->stateMutex); ConditionWake(&threadContext->stateCond);
M src/platform/3ds/threading.hsrc/platform/3ds/threading.h

@@ -37,6 +37,10 @@ static inline int MutexLock(Mutex* mutex) {

return svcWaitSynchronization(*mutex, U64_MAX); } +static inline int MutexTryLock(Mutex* mutex) { + return svcWaitSynchronization(*mutex, 10); +} + static inline int MutexUnlock(Mutex* mutex) { return svcReleaseMutex(*mutex); }
M src/platform/posix/threading.hsrc/platform/posix/threading.h

@@ -33,6 +33,10 @@ static inline int MutexLock(Mutex* mutex) {

return pthread_mutex_lock(mutex); } +static inline int MutexTryLock(Mutex* mutex) { + return pthread_mutex_trylock(mutex); +} + static inline int MutexUnlock(Mutex* mutex) { return pthread_mutex_unlock(mutex); }
M src/platform/psp2/threading.hsrc/platform/psp2/threading.h

@@ -35,6 +35,10 @@ static inline int MutexLock(Mutex* mutex) {

return sceKernelLockMutex(*mutex, 1, 0); } +static inline int MutexTryLock(Mutex* mutex) { + return sceKernelTryLockMutex(*mutex, 1); +} + static inline int MutexUnlock(Mutex* mutex) { return sceKernelUnlockMutex(*mutex, 1); }
M src/platform/windows/threading.hsrc/platform/windows/threading.h

@@ -32,6 +32,13 @@ EnterCriticalSection(mutex);

return GetLastError(); } +static inline int MutexTryLock(Mutex* mutex) { + if (TryEnterCriticalSection(mutex)) { + return GetLastError(); + } + return 1; +} + static inline int MutexUnlock(Mutex* mutex) { LeaveCriticalSection(mutex); return GetLastError();
M src/util/threading.hsrc/util/threading.h

@@ -41,6 +41,11 @@ UNUSED(mutex);

return 0; } +static inline int MutexTryLock(Mutex* mutex) { + UNUSED(mutex); + return 0; +} + static inline int MutexUnlock(Mutex* mutex) { UNUSED(mutex); return 0;