Skip to content
This repository has been archived by the owner on Feb 17, 2022. It is now read-only.

Fix mutex ownership in pthread SDL_CondWait() #131

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

baklazan
Copy link

This commit fixes a following bug in pthread versions of SDL_CondWait() and SDL_CondWaitTimeout():

When call to pthread_cond_wait() (or pthread_cond_timedwait()) succeeds and the current thread reacquires the pthread_mutex, it doesn't properly update its wrapper (the SDL_mutex object). If FAKE_RECURSIVE_MUTEX is set, this leads to problems in the following scenario:

SDL_mutex *mutex;
SDL_cond *cond;

Thread A:    
  SDL_LockMutex(mutex);
  SDL_CondWait(cond, mutex);
  SDL_UnlockMutex(mutex);
Thread B:
  SDL_LockMutex(mutex);
  SDL_UnlockMutex(mutex);
  SDL_CondSignal(cond);

Execution:
  Thread A: SDL_LockMutex(mutex);
  Thread A: SDL_CondWait(cond, mutex); // releases mutex and blocks
  Thread B: SDL_LockMutex(mutex);      // updates mutex->owner to Thread B
  Thread B: SDL_UnlockMutex(mutex);    // updates mutex->owner to 0
  Thread B: SDL_CondSignal(conde);     // unblocks Thread A
  Thread A: SDL_UnlockMutex(mutex);    // error: mutex not owned by this thread

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant