diff --git a/src/thread/switch/SDL_syscond.c b/src/thread/switch/SDL_syscond.c index 435c99b8f..1f60567c1 100644 --- a/src/thread/switch/SDL_syscond.c +++ b/src/thread/switch/SDL_syscond.c @@ -33,10 +33,14 @@ struct SDL_cond { - Mutex mutex; CondVar var; }; +struct SDL_mutex +{ + RMutex mtx; +}; + /* Create a condition variable */ SDL_cond * SDL_CreateCond(void) @@ -45,8 +49,7 @@ SDL_CreateCond(void) cond = (SDL_cond *) SDL_malloc(sizeof(SDL_cond)); if (cond) { - mutexInit(&cond->mutex); - condvarInit(&cond->var, &cond->mutex); + condvarInit(&cond->var); } else { SDL_OutOfMemory(); @@ -113,17 +116,22 @@ Thread B: int SDL_CondWaitTimeout(SDL_cond *cond, SDL_mutex *mutex, Uint32 ms) { + uint32_t mutex_state[2]; + if (!cond) { return SDL_SetError("Passed a NULL condition variable"); } - /* Unlock the mutex, as is required by condition variable semantics */ - SDL_UnlockMutex(mutex); + // backup mutex state + mutex_state[0] = mutex->mtx.thread_tag; + mutex_state[1] = mutex->mtx.counter; + mutex->mtx.thread_tag = 0; + mutex->mtx.counter = 0; - condvarWaitTimeout(&cond->var, ms * 1000000); + condvarWaitTimeout(&cond->var, &mutex->mtx.lock, ms * 1000000); - /* Lock the mutex, as is required by condition variable semantics */ - SDL_LockMutex(mutex); + mutex->mtx.thread_tag = mutex_state[0]; + mutex->mtx.counter = mutex_state[1]; return 0; }