unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Manuel Giraud via "Emacs development discussions." <emacs-devel@gnu.org>
To: emacs-devel@gnu.org
Subject: Async message-send
Date: Wed, 07 Feb 2024 11:42:21 +0100	[thread overview]
Message-ID: <874jekv9ma.fsf@ledu-giraud.fr> (raw)

Hi,

I'm (again) trying to make an async version of 'message-send'.  Reading
the elisp manual, I found that maybe I could use a "condition variable"
to make the communication between a sender thread and a house-keeping
thread.  Here is a working example of what I had in mind:

--8<---------------cut here---------------start------------->8---
;;; -*- lexical-binding:t -*-
;;; Toy example with thread and condition variable

(defvar done-workers nil)

(defun worker (mutex cv token)
  `(lambda ()
     (if (zerop (mod (random 100) 2))
	 ;; Error case: notify without adding me to the done-workers
	 ;; list.
	 (with-mutex ,mutex
	   (condition-notify ,cv))
       (with-current-buffer (get-buffer-create "foobar")
	 (insert "I'm done… ")
	 (with-mutex ,mutex
	   (push ,token done-workers)
	   (condition-notify ,cv))))))

(defun house-keeper (mutex cv token)
  `(lambda ()
     (with-mutex ,mutex
       (condition-wait ,cv)
       (cond ((member ,token done-workers)
	      (with-current-buffer (get-buffer-create "foobar")
		(insert "okay\n"))
	      (setq done-workers (remove ,token done-workers)))
	     (t (message "There was an error"))))))

(defun +my-command ()
  (interactive)
  (let* ((mutex (make-mutex))
	 (cv (make-condition-variable mutex))
	 (token (random 100)))
    (make-thread (worker mutex cv token))
    (make-thread (house-keeper mutex cv token))))
--8<---------------cut here---------------end--------------->8---

Before trying to apply this pattern to 'message-send', do you think this
approach could have any flaws?  Thanks.
-- 
Manuel Giraud



                 reply	other threads:[~2024-02-07 10:42 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=874jekv9ma.fsf@ledu-giraud.fr \
    --to=emacs-devel@gnu.org \
    --cc=manuel@ledu-giraud.fr \
    /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 public inbox

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

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