From owner-freebsd-hackers@FreeBSD.ORG Tue Apr 8 08:04:56 2014 Return-Path: Delivered-To: hackers@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) (using TLSv1 with cipher ADH-AES256-SHA (256/256 bits)) (No client certificate requested) by hub.freebsd.org (Postfix) with ESMTPS id ACAF6E9A; Tue, 8 Apr 2014 08:04:56 +0000 (UTC) Received: from mail-qa0-x22d.google.com (mail-qa0-x22d.google.com [IPv6:2607:f8b0:400d:c00::22d]) (using TLSv1 with cipher ECDHE-RSA-RC4-SHA (128/128 bits)) (Client CN "smtp.gmail.com", Issuer "Google Internet Authority G2" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 5A617122B; Tue, 8 Apr 2014 08:04:56 +0000 (UTC) Received: by mail-qa0-f45.google.com with SMTP id cm18so518146qab.32 for ; Tue, 08 Apr 2014 01:04:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type:content-transfer-encoding; bh=HJBAR1d2g1JjTuJaBepT7RjYzR4Ic52IIGkwt3eZNsU=; b=DalebWcauR5086Yoo3t+v0zIa5JGlBiiDKJKmCUdJm0tQP0rCl3omwAmdGP0TTuFWR uA33Dfy1aA7ynYzpadjA8I0nbso6zUZtwKBj6zqmVnJHCSoykm+pOiUpBgkKSzcs8hTb Ki1h3ttQHEr3LMSMTOQICH0mBKVYLH2pTZPfO8dzYO5gwHdz7ru41zVFj8e7pvP8/6BR gUV69HVhXRHib5RLqWrN+Wi1+LQy8YE4yD2kdWMAIcYqVZof37NmMVwwM5n/QUREFpWj K9pA+j1Z+n//BL7sdtAbQ3rqAx+FC/TkuHasojrYOhc6yR95u0IuzvW6/Wvhrri8sbwN eq0A== MIME-Version: 1.0 X-Received: by 10.140.22.197 with SMTP id 63mr2455914qgn.4.1396944295561; Tue, 08 Apr 2014 01:04:55 -0700 (PDT) Sender: adrian.chadd@gmail.com Received: by 10.224.50.206 with HTTP; Tue, 8 Apr 2014 01:04:55 -0700 (PDT) In-Reply-To: <0D69A6A8-43D1-41FB-8C2D-00F5CAD9C86E@FreeBSD.org> References: <0D69A6A8-43D1-41FB-8C2D-00F5CAD9C86E@FreeBSD.org> Date: Tue, 8 Apr 2014 01:04:55 -0700 X-Google-Sender-Auth: KJUbs4_hBdZOFw2tquVimEpjmIY Message-ID: Subject: Re: Multiple locks and missing wakeup. From: Adrian Chadd To: =?ISO-8859-2?Q?Edward_Tomasz_Napiera=B3a?= Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Cc: "freebsd-hackers@freebsd.org" X-BeenThere: freebsd-hackers@freebsd.org X-Mailman-Version: 2.1.17 Precedence: list List-Id: Technical Discussions relating to FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 08 Apr 2014 08:04:56 -0000 If you don't need the lock over the do_stuff, then: for(;;) { list_t lcl; init(lcl); lock; move list1 to lcl; unlock; while (! list_empty(lcl)) do_crap_on_lcl_head(); } -a On 7 April 2014 23:34, Edward Tomasz Napiera=C5=82a wro= te: > Let's say I have a kernel thread processing elements from a queue, > sleeping until there is work to do; something like this: > > mtx_lock(&mtx1); > for (;;) { > while (!LIST_EMPTY(&list1)) { > elt =3D LIST_FIRST(&list1); > do_stuff(elt); > LIST_REMOVE(&list1, elt); > } > sleep(&list1, &mtx1); > } > mtx_unlock(&mtx1); > > Now, is there some way to make it work with two lists, protected > by different mutexes? The mutex part is crucial here; the whole > point of this is to reduce lock contention on one of the lists. The > following code would result in a missing wakeup: > > mtx_lock(&mtx1); > for (;;) { > while (!LIST_EMPTY(&list1)) { > elt =3D LIST_FIRST(&list1); > do_stuff(elt); > LIST_REMOVE(&list1, elt); > } > > mtx_lock(&mtx2); > while (!LIST_EMPTY(&list2)) { > elt =3D LIST_FIRST(&list2); > do_other_stuff(elt); > LIST_REMOVE(&list2, elt); > } > mtx_unlock(&mtx2); > > sleep(&list1, &mtx1); > } > mtx_unlock(&mtx1); > > _______________________________________________ > freebsd-hackers@freebsd.org mailing list > http://lists.freebsd.org/mailman/listinfo/freebsd-hackers > To unsubscribe, send any mail to "freebsd-hackers-unsubscribe@freebsd.org= "