[SDL] semaphores made from mutexes?

Sam Lantinga slouken at devolution.com
Wed Apr 19 12:42:43 PDT 2000

> > > If initial_value > 1, SDL_SemPost() will (potentially) be called by a
> > > thread that has not originally locked the mutex.
> > 
> > That's okay.  In fact that is the way it is supposed to work.  The first
> > block on the mutex when count == 0 should block, and then any subsequent
> > post should unblock the waiting thread.
> > 
> > Am I misunderstanding you?

> I assumed that this was intentional.

> The problem is that this is not supported by POSIX and that it does not work
> with pthreads on Linux. Only the locking thread is allowed to unlock the
> mutex.

Ahh, yes, this is a serious flaw.
>From looking at the source to linuxthreads, it appears that fast mutexes
do not check ownership of the lock, but a comment elsewhere in the code
gives me pause:

   This is safe because there are no concurrent __pthread_unlock
   operations -- only the thread that locked the mutex can unlock it.


Okay, so we need a condition variable implementation
...  which is often implemented by semaphores ...

A good discussion of condition variables and implementation is available
at: http://www-classic.be.com/aboutbe/benewsletter/volume_III/Issue40.html

Stephane, I don't have a good solution for you right now.

See ya,
	-Sam Lantinga, Lead Programmer, Loki Entertainment Software

More information about the SDL mailing list