Introduce a new kind of mutex, the suspension safe one.
commitc00c3f14824d50955ba1096fae550657f5f057e5
authorRodrigo Kumpera <kumpera@gmail.com>
Mon, 22 Apr 2013 21:17:52 +0000 (22 17:17 -0400)
committerRodrigo Kumpera <kumpera@gmail.com>
Mon, 22 Apr 2013 21:20:02 +0000 (22 17:20 -0400)
treed44005e9ccbc78f6a3d6458334917d4849265970
parentb30bdb8e5777d31c27456ff945176ce56eb17705
Introduce a new kind of mutex, the suspension safe one.

* mono-mutex.c: Add mono_mutex_init_suspend_safe so special targets
like OSX that have broken implementations of suspend for pthread mutex
will hang less.

A suspension safe mutex means one that can handle this scenario:

mutex M

thread 1:
1)lock M
2)suspend thread 2
3)unlock M
4)lock M

thread 2:
5)lock M

Say (1) happens before (5) and (5) happens before (2).
This means that thread 2 was suspended by the kernel because
it's waiting on mutext M.

Thread 1 then proceed to suspend thread 2 and unlock/lock the
mutex.

If the kernel implements mutexes with FIFO wait lists, this means
that thread 1 will be blocked waiting for thread 2 acquire the lock.
Since thread 2 is suspended, we have a deadlock.

A suspend safe mutex is an unfair lock but will schedule any runable
thread that is waiting for a the lock.

This problem was witnessed on OSX in mono/tests/thread-exit.cs.
mono/utils/mono-mutex.c
mono/utils/mono-mutex.h
mono/utils/mono-threads.c
mono/utils/mono-threads.h