Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 8 Apr 2014 11:51:56 +0300
From:      Konstantin Belousov <kostikbel@gmail.com>
To:        Edward Tomasz Napiera?a <trasz@FreeBSD.org>
Cc:        hackers@freebsd.org
Subject:   Re: Multiple locks and missing wakeup.
Message-ID:  <20140408085156.GR21331@kib.kiev.ua>
In-Reply-To: <0D69A6A8-43D1-41FB-8C2D-00F5CAD9C86E@FreeBSD.org>
References:  <0D69A6A8-43D1-41FB-8C2D-00F5CAD9C86E@FreeBSD.org>

next in thread | previous in thread | raw e-mail | index | archive | help

--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--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20140408085156.GR21331>