From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Ihor Radchenko Newsgroups: gmane.emacs.bugs Subject: bug#57196: 28.1.90; An idea to allow background (low-priority) threads Date: Sun, 14 Aug 2022 15:57:37 +0800 Message-ID: <87mtc7fea6.fsf@localhost> References: <878rnr1n17.fsf@localhost> <83bksnl331.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="3333"; mail-complaints-to="usenet@ciao.gmane.io" Cc: 57196@debbugs.gnu.org To: Eli Zaretskii Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Aug 14 09:57:55 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oN8VP-0000jK-Hb for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 14 Aug 2022 09:57:55 +0200 Original-Received: from localhost ([::1]:55200 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1oN8VN-000611-HL for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 14 Aug 2022 03:57:54 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:55466) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1oN8UZ-00060W-RR for bug-gnu-emacs@gnu.org; Sun, 14 Aug 2022 03:57:04 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:45993) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1oN8UY-0005Sj-FW for bug-gnu-emacs@gnu.org; Sun, 14 Aug 2022 03:57:03 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1oN8UY-0001ko-C1 for bug-gnu-emacs@gnu.org; Sun, 14 Aug 2022 03:57:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Ihor Radchenko Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 14 Aug 2022 07:57:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 57196 X-GNU-PR-Package: emacs Original-Received: via spool by 57196-submit@debbugs.gnu.org id=B57196.16604638086722 (code B ref 57196); Sun, 14 Aug 2022 07:57:02 +0000 Original-Received: (at 57196) by debbugs.gnu.org; 14 Aug 2022 07:56:48 +0000 Original-Received: from localhost ([127.0.0.1]:35742 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oN8UK-0001kL-9M for submit@debbugs.gnu.org; Sun, 14 Aug 2022 03:56:48 -0400 Original-Received: from mail-pj1-f46.google.com ([209.85.216.46]:53829) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1oN8UI-0001k8-5c for 57196@debbugs.gnu.org; Sun, 14 Aug 2022 03:56:46 -0400 Original-Received: by mail-pj1-f46.google.com with SMTP id pm17so4540312pjb.3 for <57196@debbugs.gnu.org>; Sun, 14 Aug 2022 00:56:46 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:from:to:cc; bh=pqXUYzMzwMoTEms5e6QrAOt0TegO906snoE7/xWtObw=; b=lPWm3Slk1LcK0GAH4LZ1h0vT6+RGF09RjSEN4Fk3V5q8wuznWM0DZ2r1DicLNVBzdA HLebLF932akEJZ+S28aj7UeLR5Dsvd0qP8JZDNMZK2Wx3IHz5IIOx/cxXTVVUQsc1rY+ DHC5q5cHIr7ZKEG7+/YNZha7lmjnwObCMxbDsH9GaHveWbX9G0G7mVkWs4cysgKHq//U QR+1UFKIbQCwY00bWt53Vo10VmZJLD5Ok3KnxPWt4Nhbc//pwnzfP7qrMfkfZ56ymS3C jEEAMSZaV2ZjGAelhxIXR/8iXyKYG0MCzX395qFLUOJ9JoHPH0XMpoefXsA2LWEMekzb bqUA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=mime-version:message-id:date:references:in-reply-to:subject:cc:to :from:x-gm-message-state:from:to:cc; bh=pqXUYzMzwMoTEms5e6QrAOt0TegO906snoE7/xWtObw=; b=dBv4bTJgzSQ/RpJ5QwCf3TWCSc2JK7MxfdxOJRVWTBzKgeCE02KmuYtRZzHyCqPzB6 w/SxLQDA7ho9uVDtgHRDN9k/On+MpkO9/HeaNnaL6bozEBfdUMP2LAylgECV8emSAVM/ orZ4YlHwihHWDg2iS7cPeBvOYgUXbNnIBF90Dzj+6Fsm+2Us+nzEh9NaDYclj79ZzOVr 8twOWqDjd5AbQQvApo+/pd7/3uOUd+diEx2i8F8Z3AP2r4SsxPu3MB68VbNDuAbgzIqY xJxvTbvQuaIsfkiWS4j7ZlCL8XhxfG0Kq5cTmophZd2o4kr2W1CbqMin7f2Lxot16FTD eQhA== X-Gm-Message-State: ACgBeo0nx+INKEsJlyiEcNyZ2TjnTxxwFl859u+hmFvcogucFzlhQKL/ oveUXkiksfYlb/SgMD4NWrY= X-Google-Smtp-Source: AA6agR4D/gFMUB7JDHKgQDO++bs5HUrDxMxOg7FFfMSv67xbXUrsEIaX6JOlwhvcL80Z5gXD5m9HuQ== X-Received: by 2002:a17:902:e353:b0:16d:c10a:651a with SMTP id p19-20020a170902e35300b0016dc10a651amr11754786plc.146.1660463796062; Sun, 14 Aug 2022 00:56:36 -0700 (PDT) Original-Received: from localhost ([2409:8a70:2bf:80b0:8ec6:81ff:fe70:339d]) by smtp.gmail.com with ESMTPSA id x124-20020a626382000000b0053291ddd8e5sm3050273pfb.40.2022.08.14.00.56.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 14 Aug 2022 00:56:35 -0700 (PDT) In-Reply-To: <83bksnl331.fsf@gnu.org> X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:239630 Archived-At: Eli Zaretskii 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? 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. > TBH, I'm not really sure your analysis, which basically says this is a > problem with thread "equality", is correct. Did you try to see what > causes the sluggish operation in the cases where you saw it? I once tried to write some code for async magit status buffer and for async agenda generation. I am not sure how I can properly determine the cause of sluggish operation, but AFAIU the cause was the following: 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. >> I imagine that something similar could be done for threads. >> `make-thread' could allow some kind of priority setting that will limit >> its execution time to comfortable levels, so that the user in main >> thread can actually interact with Emacs without noticing anything. > > What mechanism will stop the running thread that has exceeded its > allotted time? In the current implementation, the thread stops itself > when it calls a small set of primitives, but with your proposal we'd > need to make the "scheduler" run in a separate thread, which would > mean a complete redesign of how threads are scheduled. 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. "idle-time" may also be used, pausing thread until that much idle-time passed. -- Ihor Radchenko, Org mode contributor, Learn more about Org mode at https://orgmode.org/. Support Org development at https://liberapay.com/org-mode, or support my work at https://liberapay.com/yantar92