public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: Alexandre Bique <bique.alexandre@gmail.com>,
	Konstantin Kharlamov <hi-angel@yandex.ru>
Cc: Florian Weimer <fweimer@redhat.com>,
	Alexandre Bique via Libc-help <libc-help@sourceware.org>
Subject: Re: Yield to specific thread?
Date: Tue, 25 May 2021 15:21:46 -0300	[thread overview]
Message-ID: <7eb8d574-45c0-32d8-2b9d-c719535fd246@linaro.org> (raw)
In-Reply-To: <CAP737oLvhkHeyKzHG948Bp2xgWx-c9NHqd=mfdc+DfCw0ZJJQA@mail.gmail.com>

I think you will need a conditional variable with a priority support.
Unfortunately POSIX requirements makes hard to provide it on glibc,

There is a project that aims to provide it [1] and I think it would
fit better in the scenarios you described: you setup a conditional
variable on a shared memory between the two processes A and B, you
setup B with higher priority than A, and when A produces a request
the condvar wakeup event will wake the highest priority waiters
(in the case B).

This library uses the FUTEX_WAIT_REQUEUE_PI futex operations with a
different (and I think non-POSIX conformant) conditional variable
implementation.

[1] https://github.com/dvhart/librtpi 

On 20/05/2021 08:54, Alexandre Bique via Libc-help wrote:
> Oh I think I fixed it using 3 mutexes.
> Alexandre Bique
> 
> On Thu, May 20, 2021 at 1:20 PM Konstantin Kharlamov <hi-angel@yandex.ru> wrote:
>>
>> On Thu, 2021-05-20 at 13:09 +0200, Alexandre Bique via Libc-help wrote:
>>> On Thu, May 20, 2021 at 1:03 PM Florian Weimer <fweimer@redhat.com> wrote:
>>>>
>>>> * Alexandre Bique via Libc-help:
>>>>
>>>>> Ideally I'd like to do:
>>>>> A produces a request
>>>>> A sched_yield_to(B)
>>>>> B processes the request
>>>>> B sched_yield_to(A)
>>>>
>>>> This looks like an application for a condition variable or perhaps a
>>>> barrier.  If there is just a single writer, the kernel should wake up
>>>> the desired thread.
>>>
>>> I don't think conditions or barriers would solve the problem. Because
>>> they would just put the waiting threads on the wake up queue like the
>>> read() on the pipe would.
>>
>> I assume it should work. I remember Torvalds ranting about people using sched_yield() for the wrong reasons¹, and he mentioned mutex (which apparently worked for you) as one of possible solutions. Quoting:
>>
>>> Good locking simply needs to be more directed than what "sched_yield()" can ever give you outside of a UP system without caches. It needs to actively tell the system what you're yielding to (and optimally it would also tell the system about whether you care about fairness/latency or not - a lot of loads don't).
>>>
>>> But that's not "sched_yield()" - that's something different. It's generally something like std::mutex, pthread_mutex_lock(), or perhaps a tuned thing that uses an OS-specific facility like "futex", where you do the nonblocking (and non-contended) case in user space using a shared memory location, but when you get contention you tell the OS what you're waiting for (and what you're waking up).
>>
>>
>> 1: https://www.realworldtech.com/forum/?threadid=189711&curpostid=189752
>>

  reply	other threads:[~2021-05-25 18:21 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-05-20 10:42 Alexandre Bique
2021-05-20 11:02 ` Florian Weimer
2021-05-20 11:09   ` Alexandre Bique
2021-05-20 11:20     ` Konstantin Kharlamov
2021-05-20 11:54       ` Alexandre Bique
2021-05-25 18:21         ` Adhemerval Zanella [this message]
2021-05-26  9:36           ` Tadeus Prastowo
2021-05-26 14:09           ` Alexandre Bique
2021-05-20 12:27 ` Godmar Back

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=7eb8d574-45c0-32d8-2b9d-c719535fd246@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=bique.alexandre@gmail.com \
    --cc=fweimer@redhat.com \
    --cc=hi-angel@yandex.ru \
    --cc=libc-help@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).