emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Emacs-diffs] scratch/benchmarks ef44d4b 2/6: Add per-symbol mutexes


From: Gemini Lasswell
Subject: [Emacs-diffs] scratch/benchmarks ef44d4b 2/6: Add per-symbol mutexes
Date: Sun, 25 Nov 2018 20:11:15 -0500 (EST)

branch: scratch/benchmarks
commit ef44d4b2f62d32bf032a26d10fe92fd743bbd89c
Author: Gemini Lasswell <address@hidden>
Commit: Gemini Lasswell <address@hidden>

    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 6582510..838b8c2 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



reply via email to

[Prev in Thread] Current Thread [Next in Thread]