From owner-freebsd-hackers@FreeBSD.ORG Tue Apr 8 08:52:03 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 951868F1; Tue, 8 Apr 2014 08:52:03 +0000 (UTC) Received: from kib.kiev.ua (kib.kiev.ua [IPv6:2001:470:d5e7:1::1]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (Client did not present a certificate) by mx1.freebsd.org (Postfix) with ESMTPS id 1FF491692; Tue, 8 Apr 2014 08:52:02 +0000 (UTC) Received: from tom.home (kostik@localhost [127.0.0.1]) by kib.kiev.ua (8.14.8/8.14.8) with ESMTP id s388puul011878; Tue, 8 Apr 2014 11:51:56 +0300 (EEST) (envelope-from kostikbel@gmail.com) DKIM-Filter: OpenDKIM Filter v2.8.3 kib.kiev.ua s388puul011878 Received: (from kostik@localhost) by tom.home (8.14.8/8.14.8/Submit) id s388puBl011877; Tue, 8 Apr 2014 11:51:56 +0300 (EEST) (envelope-from kostikbel@gmail.com) X-Authentication-Warning: tom.home: kostik set sender to kostikbel@gmail.com using -f Date: Tue, 8 Apr 2014 11:51:56 +0300 From: Konstantin Belousov To: Edward Tomasz Napiera?a Subject: Re: Multiple locks and missing wakeup. Message-ID: <20140408085156.GR21331@kib.kiev.ua> References: <0D69A6A8-43D1-41FB-8C2D-00F5CAD9C86E@FreeBSD.org> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha1; protocol="application/pgp-signature"; boundary="1trTh7oBgAEAEHeG" Content-Disposition: inline In-Reply-To: <0D69A6A8-43D1-41FB-8C2D-00F5CAD9C86E@FreeBSD.org> User-Agent: Mutt/1.5.23 (2014-03-12) X-Spam-Status: No, score=-2.0 required=5.0 tests=ALL_TRUSTED,BAYES_00, DKIM_ADSP_CUSTOM_MED,FREEMAIL_FROM,NML_ADSP_CUSTOM_MED autolearn=no autolearn_force=no version=3.4.0 X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on tom.home Cc: 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:52:03 -0000 --1trTh7oBgAEAEHeG Content-Type: text/plain; charset=us-ascii Content-Disposition: inline Content-Transfer-Encoding: quoted-printable On Tue, Apr 08, 2014 at 08:34:30AM +0200, Edward Tomasz Napiera?a wrote: > Let's say I have a kernel thread processing elements from a queue, > sleeping until there is work to do; something like this: >=20 > 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); >=20 > 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: >=20 > mtx_lock(&mtx1); > for (;;) { > while (!LIST_EMPTY(&list1)) { > elt =3D LIST_FIRST(&list1); > do_stuff(elt); > LIST_REMOVE(&list1, elt); > } mtx_unlock(&mtx1);, right ? >=20 > mtx_lock(&mtx2); > while (!LIST_EMPTY(&list2)) { > elt =3D LIST_FIRST(&list2); > do_other_stuff(elt); > LIST_REMOVE(&list2, elt); > } > mtx_unlock(&mtx2); >=20 > sleep(&list1, &mtx1); > } > mtx_unlock(&mtx1); You should clarify your intent. Do you need to sleep waiting for the work items to appear for list2 ? What wakeup do you miss in the pseudo-code you posted ? If you do need the notification for the list2, use &list1 as the wakeup channel for it. --1trTh7oBgAEAEHeG Content-Type: application/pgp-signature -----BEGIN PGP SIGNATURE----- Version: GnuPG v2.0.22 (FreeBSD) iQIcBAEBAgAGBQJTQ7irAAoJEJDCuSvBvK1BzGMQAJLa2yN9HyUS2T9buJCNI7Q6 j6Z2H5z/C/9y83XgzDYC+MTwwK5IbaCXsZRELfv+xwHfKXRLIVH2ZNmzN/+dYYWb TlF4mYRR5/wwczKpYZGPAxAkha2esIIEiq9E9wdafnbUfxLfomJMJzxO6ZgGbCae 1X6iohmB3fRYEg+fLhVIxbgQABvLMmxTeQ6VsIqGfjWA8Av/WnYTcRPIOadnVaTz F0wk8TCQPyNkvcaCP/NWmN1PFbuKDB9Sc5P/XuNkDlfsmZS/vwjc2oaCxRUipRrf xdBspepKazH4gtk4MKyLJ6Ndl+4DJvpDjEZjcKpsVhs4IbayfrF5GxrjS/8IfrKp X4ie0MUYsHN5nwJN38Ri4LqXDa3BtZo+6upOeARYRX4kuJ7xXMYdtljtUUqxwMzP R9+TWCxUh/CagvTT1KKZnrZoHTwB8u5BQWo8J5meKbm4ip0/bHsNxheJdEuf8/1P pH0gLK5vnr+7bQeA8LwNM4PBTzfmoURY5mwGfO+QSLSgOv7teJtajR+qGV+IlgdG YeuMQ7E9oEMLpPhSGVfmSQoaLySsrU+yn5CmbAqYzMa6UX/r3usRvXPnZikz4AvS MWspLEGpbHwO8r2n5mu5DkgVkxc8AFpWxN7jyf4G7isW3T4iec7p0qzRYvwWVImL tsdbc57H2091q08IAkl5 =1ryS -----END PGP SIGNATURE----- --1trTh7oBgAEAEHeG--