From owner-freebsd-threads@FreeBSD.ORG Wed Dec 21 15:23:57 2011 Return-Path: Delivered-To: freebsd-threads@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id B9AE21065676 for ; Wed, 21 Dec 2011 15:23:57 +0000 (UTC) (envelope-from des@des.no) Received: from smtp.des.no (smtp.des.no [194.63.250.102]) by mx1.freebsd.org (Postfix) with ESMTP id 7841B8FC12 for ; Wed, 21 Dec 2011 15:23:57 +0000 (UTC) Received: from ds4.des.no (des.no [84.49.246.2]) by smtp.des.no (Postfix) with ESMTP id 649D86A00; Wed, 21 Dec 2011 14:59:00 +0000 (UTC) Received: by ds4.des.no (Postfix, from userid 1001) id A104E80C1; Wed, 21 Dec 2011 15:58:57 +0100 (CET) From: =?utf-8?Q?Dag-Erling_Sm=C3=B8rgrav?= To: "Poul-Henning Kamp" References: <73233.1324389741@critter.freebsd.dk> Date: Wed, 21 Dec 2011 15:58:57 +0100 In-Reply-To: <73233.1324389741@critter.freebsd.dk> (Poul-Henning Kamp's message of "Tue, 20 Dec 2011 14:02:21 +0000") Message-ID: <86hb0ut1hq.fsf@ds4.des.no> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.3 (berkeley-unix) MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Cc: freebsd-arch@freebsd.org, freebsd-threads@freebsd.org Subject: Re: [Patch] C1X threading support X-BeenThere: freebsd-threads@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Threading on FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Dec 2011 15:23:57 -0000 "Poul-Henning Kamp" 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