[Patch] C1X threading support

Dag-Erling Smørgrav des at des.no
Wed Dec 21 14:59:07 UTC 2011


"Poul-Henning Kamp" <phk at phk.freebsd.dk> writes:
> There is no way this can be impossible on a platform which can
> implement a mutex in the first place:
>
>
> 	mtx_lock(l)
> 	{
> 		atomic_magic_lock(l->lock_field)
> 		l->id = thread_id;
> 	}

OK

> 	mtx_unlock(l)
> 	{
> 		assert(l->id == thread_id);
> 		l->id = NULL;
> 		atomic_magic_unlock(l->lock_field)
> 	}

susceptible to race conditions

> 	mtx_assert_held(l)
> 	{
> 		assert(l->lock-field != 0);
> 		assert(l->id == thread_id);
> 	}

susceptible to race conditions

The canonical solution is to use some low-level lock primitive (worst
case, a critical section) to protect the mutex structure, but then you
need some way for mtx_lock() to sleep if the mutex is held and some way
for mtx_unlock() to wake sleepers.  You also need to lock the mutex
structure in mtx_assert_held(), unless l->id is atomic.

DES
-- 
Dag-Erling Smørgrav - des at des.no


More information about the freebsd-arch mailing list