unofficial mirror of 
 help / color / mirror / code / Atom feed
* Async message-send
@ 2024-02-07 10:42 Manuel Giraud via Emacs development discussions.
  0 siblings, 0 replies; only message in thread
From: Manuel Giraud via Emacs development discussions. @ 2024-02-07 10:42 UTC (permalink / raw)
  To: emacs-devel


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

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2024-02-07 10:42 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-02-07 10:42 Async message-send Manuel Giraud via Emacs development discussions.

Code repositories for project(s) associated with this public inbox

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