Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 21 Dec 2011 15:58:57 +0100
From:      =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= <des@des.no>
To:        "Poul-Henning Kamp" <phk@phk.freebsd.dk>
Cc:        freebsd-arch@freebsd.org, freebsd-threads@freebsd.org
Subject:   Re: [Patch] C1X threading support
Message-ID:  <86hb0ut1hq.fsf@ds4.des.no>
In-Reply-To: <73233.1324389741@critter.freebsd.dk> (Poul-Henning Kamp's message of "Tue, 20 Dec 2011 14:02:21 %2B0000")
References:  <73233.1324389741@critter.freebsd.dk>

next in thread | previous in thread | raw e-mail | index | archive | help
"Poul-Henning Kamp" <phk@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 =3D thread_id;
> 	}

OK

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

susceptible to race conditions

> 	mtx_assert_held(l)
> 	{
> 		assert(l->lock-field !=3D 0);
> 		assert(l->id =3D=3D 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
--=20
Dag-Erling Sm=C3=B8rgrav - des@des.no



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?86hb0ut1hq.fsf>