From owner-freebsd-hackers@FreeBSD.ORG Mon Dec 8 20:52:19 2008 Return-Path: Delivered-To: freebsd-hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 034D4106564A for ; Mon, 8 Dec 2008 20:52:19 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from server.baldwin.cx (bigknife-pt.tunnel.tserv9.chi1.ipv6.he.net [IPv6:2001:470:1f10:75::2]) by mx1.freebsd.org (Postfix) with ESMTP id 76BC88FC16 for ; Mon, 8 Dec 2008 20:52:18 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from localhost.corp.yahoo.com (john@localhost [IPv6:::1]) (authenticated bits=0) by server.baldwin.cx (8.14.3/8.14.3) with ESMTP id mB8KqBnH003949; Mon, 8 Dec 2008 15:52:11 -0500 (EST) (envelope-from jhb@freebsd.org) From: John Baldwin To: freebsd-hackers@freebsd.org Date: Mon, 8 Dec 2008 15:17:39 -0500 User-Agent: KMail/1.9.7 References: In-Reply-To: MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200812081517.39375.jhb@freebsd.org> X-Greylist: Sender succeeded SMTP AUTH authentication, not delayed by milter-greylist-2.0.2 (server.baldwin.cx [IPv6:::1]); Mon, 08 Dec 2008 15:52:12 -0500 (EST) X-Virus-Scanned: ClamAV version 0.94.2, clamav-milter version 0.94.2 on server.baldwin.cx X-Virus-Status: Clean X-Spam-Status: No, score=-2.6 required=4.2 tests=AWL,BAYES_00,NO_RELAYS autolearn=ham version=3.1.3 X-Spam-Checker-Version: SpamAssassin 3.1.3 (2006-06-01) on server.baldwin.cx Cc: Marius =?iso-8859-1?q?N=FCnnerich?= Subject: Re: Why safe using msleep with timeout=0 but not tsleep? X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 08 Dec 2008 20:52:19 -0000 On Sunday 07 December 2008 02:00:30 pm Marius N=FCnnerich wrote: > See subject. > Interesting commit: > http://svn.freebsd.org/viewvc/base?view=3Drevision&revision=3D77059 Lost wakeups. If you have code like so that doesn't use any locks: int flag; void foo(void) { flag =3D 1; wakeup(&flag); } void bar(void) { if (flag =3D=3D 0) tsleep(&foo, ..., 0); } Then one CPU may run the 'foo' routine to completion after another CPU has= =20 seen 'flag =3D=3D 0' but before it has put the thread to sleep in tsleep().= Even=20 on UP systems with preemption you can still get this race if you get=20 preempted by an interrupt (which runs foo()) in between the 'flag =3D=3D 0'= test=20 and calling tsleep(). Using an interlock avoid this: struct mtx lock; int flag; void foo(void) { mtx_lock(&lock); flag =3D 1; mtx_unlock(&lock); wakeup(&flag); } void bar(void) { mtx_lock(&lock); if (flag =3D=3D 0) mtx_sleep(&foo, &lock, ..., 0); mtx_unlock(&lock); } In this case 'lock' closes the SMP/preemption races. =2D-=20 John Baldwin