From ef44d4b2f62d32bf032a26d10fe92fd743bbd89c Mon Sep 17 00:00:00 2001 From: Gemini Lasswell Date: Mon, 15 Oct 2018 08:28:43 -0700 Subject: [PATCH] Add per-symbol mutexes * lisp/thread.el (make-symbol-mutex): New function. (with-symbol-mutex): New macro. --- lisp/thread.el | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/lisp/thread.el b/lisp/thread.el index 65825102922..838b8c243a3 100644 --- a/lisp/thread.el +++ b/lisp/thread.el @@ -310,6 +310,25 @@ If there are no items in QUEUE, block until one is added." (condition-notify (thread--queue-not-full queue)) item))) +;;; Mutexes for variables + +(defun make-symbol-mutex (symbol) + "Create a mutex associated with SYMBOL." + (unless (get symbol 'thread--mutex) + (put symbol 'thread--mutex (make-mutex (symbol-name symbol))))) + +(defmacro with-symbol-mutex (symbol &rest body) + "Run BODY while holding the mutex for SYMBOL. +If another thread holds the mutex, block until it is released." + (declare (indent 1) + (debug (symbolp body))) + (let ((g-mutex (gensym))) + `(let ((,g-mutex (get ',symbol 'thread--mutex))) + (if ,g-mutex + (with-mutex ,g-mutex + ,@body) + (error "`%s' doesn't have a mutex" ',symbol))))) + (provide 'thread) ;;; thread.el ends here -- 2.39.5