From: Gemini Lasswell Date: Mon, 15 Oct 2018 15:28:43 +0000 (-0700) Subject: Add per-symbol mutexes X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=ef44d4b2f62d32bf032a26d10fe92fd743bbd89c;p=emacs.git Add per-symbol mutexes * lisp/thread.el (make-symbol-mutex): New function. (with-symbol-mutex): New macro. --- 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