Date: Tue, 8 Apr 2014 01:04:55 -0700 From: Adrian Chadd <adrian@freebsd.org> To: =?ISO-8859-2?Q?Edward_Tomasz_Napiera=B3a?= <trasz@freebsd.org> Cc: "freebsd-hackers@freebsd.org" <hackers@freebsd.org> Subject: Re: Multiple locks and missing wakeup. Message-ID: <CAJ-Vmom5fRLP8uFV9cSDR9MR-g3XCrGdg9MMwq1FBzjOJKDumg@mail.gmail.com> 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
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Ća <trasz@freebsd.org> wrote:
> 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 = 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 = LIST_FIRST(&list1);
> do_stuff(elt);
> LIST_REMOVE(&list1, elt);
> }
>
> mtx_lock(&mtx2);
> while (!LIST_EMPTY(&list2)) {
> elt = 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"
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?CAJ-Vmom5fRLP8uFV9cSDR9MR-g3XCrGdg9MMwq1FBzjOJKDumg>
