From owner-svn-src-head@freebsd.org Wed Nov 9 15:09:25 2016 Return-Path: Delivered-To: svn-src-head@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 008DAC387AC; Wed, 9 Nov 2016 15:09:25 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from mail.baldwin.cx (bigwig.baldwin.cx [96.47.65.170]) (using TLSv1 with cipher DHE-RSA-CAMELLIA256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id D0B98769; Wed, 9 Nov 2016 15:09:24 +0000 (UTC) (envelope-from jhb@freebsd.org) Received: from ralph.baldwin.cx (c-73-231-226-104.hsd1.ca.comcast.net [73.231.226.104]) by mail.baldwin.cx (Postfix) with ESMTPSA id 6C11910A722; Wed, 9 Nov 2016 10:09:22 -0500 (EST) From: John Baldwin To: Hans Petter Selasky Cc: Oleksandr Tymoshenko , src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: Re: svn commit: r308424 - head/sys/arm/broadcom/bcm2835 Date: Wed, 09 Nov 2016 07:02:36 -0800 Message-ID: <3214848.geWV8qu7rM@ralph.baldwin.cx> User-Agent: KMail/4.14.10 (FreeBSD/11.0-PRERELEASE; KDE/4.14.10; amd64; ; ) In-Reply-To: References: <201611071738.uA7HceYu045944@repo.freebsd.org> <680D84F2-65BF-48DD-8D11-311B1F65A634@freebsd.org> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" X-Greylist: Sender succeeded SMTP AUTH, not delayed by milter-greylist-4.4.3 (mail.baldwin.cx); Wed, 09 Nov 2016 10:09:22 -0500 (EST) X-Virus-Scanned: clamav-milter 0.99.2 at mail.baldwin.cx X-Virus-Status: Clean X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 09 Nov 2016 15:09:25 -0000 On Monday, November 07, 2016 08:32:17 PM Hans Petter Selasky wrote: > On 11/07/16 20:23, Oleksandr Tymoshenko wrote: > > > >> On Nov 7, 2016, at 10:27 AM, Hans Petter Selasky = wrote: > >> > >> On 11/07/16 18:38, Oleksandr Tymoshenko wrote: > >>> +=09=09bcm2835_audio_unlock(sc); > >>> +=09=09cv_signal(&sc->worker_cv); > >> > >> > >> Shouldn't cv_signal() be done locked, so that you don't loose any = transactions? CV's only wakeup the treads that are sleeping right there= and then. > > > > Hi Hans, > > > > In this case it doesn=E2=80=99t matter. bcm2835_audio_xxx lock func= tions are used to keep channel state consistent. The actual audio hw re= programming happens in worker thread which only picks up latest state o= f the virtual channel, there is no need to run every transaction in seq= uence. > > >=20 > Hi, >=20 > It is not about running in sequence, but that if the worker thread is= =20 > not sleeping, but on the way to sleep, it will never get woken up unl= ess=20 > you use proper locks here! You do not have to hold locks across cv_signal/broadcast or wakeup. Yo= u do have to hold them across the check determining whether you should sleep= . Take this simple example: =09lock(&m); =09while (should_sleep) =09=09cv_wait(&cv, &m); =09unlock(&m); =09... =09lock(&m); =09should_sleep =3D true; =09unlock(&m); =09cv_signal(&cv); A thread that locks 'm' after the 'unlock' but before the cv_signal wil= l see 'should_sleep' as false and will not call cv_wait(). The cv_signal of = course might then wakeup a second thread prematurely, but that's why you shoul= d always use a while loop with cv wait operations (same is true with pthr= ead condvars btw). On the other hand, doing the wakeup outside of the lock= avoids preempting during the wakeup only to immediately block on the lo= ck and switch back to the thread that did the wakeup. --=20 John Baldwin