all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Eli Zaretskii <eliz@gnu.org>
To: Ihor Radchenko <yantar92@gmail.com>
Cc: 57196@debbugs.gnu.org
Subject: bug#57196: 28.1.90; An idea to allow background (low-priority) threads
Date: Sun, 14 Aug 2022 16:37:12 +0300	[thread overview]
Message-ID: <83tu6fj69j.fsf@gnu.org> (raw)
In-Reply-To: <87mtc7fea6.fsf@localhost> (message from Ihor Radchenko on Sun, 14 Aug 2022 15:57:37 +0800)

> From: Ihor Radchenko <yantar92@gmail.com>
> Cc: 57196@debbugs.gnu.org
> Date: Sun, 14 Aug 2022 15:57:37 +0800
> 
> Eli Zaretskii <eliz@gnu.org> writes:
> 
> > Our "scheduler", such as it is, is in thread.c:really_call_select.  It
> > basically releases the global lock and lets the first thread waiting
> > on the lock to acquire the lock.  If someone wants to implement a
> > smarter lock-grabbing logic with some kind of "nice" capability, AFAIU
> > that's the place to look and modify.
> 
> Do I understand correctly that thread.c:really_call_select simply relies
> on pthread itself to select the next thread?

What do you mean by "pthread itself"?  AFAIU, threads are scheduled
by the OS, not by pthreads.  So which thread will be the next to grab
the lock is up to the OS, the way we programmed it.

> Then, a simple pthread_setschedparam inside
> systhread.c:sys_thread_create may do the job given that we pass an extra
> optional parameter to make-thread.

You could try that and see if that helps, although I wouldn't know how
you'd select the policy in this case.

> 1. There is main thread and the magit/agenda thread
> 2. Magit/agenda thread uses a lot of CPU while the main thread is not (I
>    was typing/navigating the buffer)
> 3. Every keypress in the main thread caused thread switch to the
>    CPU-hungry magit/agenda thread
> 4. Frequent switches caused high typing latency because every single key
>    stroke in the main thread had to wait until the magit/agenda yields.

The only way of improving the UX in this case is for the other thread
to yield more frequently.

> I do not suggest to stop the running thread externally.
> 
> Instead, we may accumulate the thread execution time. Let's call this
> "thread 1".
> 
> Then, every time the _other_ running thread yields (stops itself) and
> "thread 1" is about to be called, we do the following:
> 1. If "thread 1" execution time is less than "duration" thread
>    property, run the thread.
> 2. If "thread 1" execution time is more than "duration", skip running
>    the thread remembering the time now (pause time).
> 3. If "thread 1" execution time is more than "duration" and "pause time"
>    was more than "break time" thread property ago, set execution time to
>    0 and allow the thread to be running.

You can do this in your thread function: when it gets to run, check
whether it "earned" its time slot, and if not, yield immediately
without calling the workhorse code.





      reply	other threads:[~2022-08-14 13:37 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-08-14  4:12 bug#57196: 28.1.90; An idea to allow background (low-priority) threads Ihor Radchenko
2022-08-14  7:02 ` Eli Zaretskii
2022-08-14  7:57   ` Ihor Radchenko
2022-08-14 13:37     ` Eli Zaretskii [this message]

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=83tu6fj69j.fsf@gnu.org \
    --to=eliz@gnu.org \
    --cc=57196@debbugs.gnu.org \
    --cc=yantar92@gmail.com \
    /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.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.