Tinylisp and Multi-threaded Emacs
TAME-ing the beast!!
Here's a fork of tinylisp and a glimpse of a possible multi-threaded future. Below is the content of the file a.el in the demo. It creates one read thread which waits for non-nil value of global-var and a set thread which sleeps for 5s before modifying global-var and notifying the waiting threads. Since these are running in separate threads, main loop is free for user input.
(define list (lambda args args))
(define defun (macro (f v . x) (list 'define f (list 'lambda v (cons 'progn x)))))
(setq make-condition-variable make-cond)
(setq condition-wait cond-wait)
(setq condition-notify cond-signal)
(setq message p)
(setq sleep-for sleep)
(setq global-var nil)
;; Create a mutex and an associated condition variable
(setq mutex (make-mutex "mutex"))
(setq cond-var (make-condition-variable mutex "cond-var"))
;; Read and set functions used by read and set threads
(defun set-global-var ()
(mutex-lock mutex)
(sleep 5)
(setq global-var "1")
(condition-notify cond-var)
(mutex-unlock mutex)
)
(defun read-global-var ()
(mutex-lock mutex)
(while (not global-var)
(condition-wait cond-var mutex))
global-var
(mutex-unlock mutex)
)
;; Create and start threads
(make-thread read-global-var)
(make-thread set-global-var)
global-var
(define defun (macro (f v . x) (list 'define f (list 'lambda v (cons 'progn x)))))
(setq make-condition-variable make-cond)
(setq condition-wait cond-wait)
(setq condition-notify cond-signal)
(setq message p)
(setq sleep-for sleep)
(setq global-var nil)
;; Create a mutex and an associated condition variable
(setq mutex (make-mutex "mutex"))
(setq cond-var (make-condition-variable mutex "cond-var"))
;; Read and set functions used by read and set threads
(defun set-global-var ()
(mutex-lock mutex)
(sleep 5)
(setq global-var "1")
(condition-notify cond-var)
(mutex-unlock mutex)
)
(defun read-global-var ()
(mutex-lock mutex)
(while (not global-var)
(condition-wait cond-var mutex))
global-var
(mutex-unlock mutex)
)
;; Create and start threads
(make-thread read-global-var)
(make-thread set-global-var)
global-var
Code: https://gitlab.com/atamariya/tinylisp/ (uint64 branch)
Demo: https://youtu.be/TZfmpWu0mec
Comments
Post a Comment