diff options
author | Tom Tromey <tromey@redhat.com> | 2012-08-20 07:56:02 -0600 |
---|---|---|
committer | Tom Tromey <tromey@redhat.com> | 2012-08-20 07:56:02 -0600 |
commit | fb77afbe75308507885113a56017f095da8ba1cc (patch) | |
tree | 5e87b5bcff336c0db59e180cc2eefde8f184cfa8 | |
parent | 49bc1a9dfc6e81a370bf12157c3c573743ee200a (diff) | |
download | emacs-fb77afbe75308507885113a56017f095da8ba1cc.tar.gz emacs-fb77afbe75308507885113a56017f095da8ba1cc.tar.bz2 emacs-fb77afbe75308507885113a56017f095da8ba1cc.zip |
add convenience macros with-mutex and until-condition
with-mutex is a safe way to run some code with a mutex held.
until-condition is a safe way to wait on a condition variable.
-rw-r--r-- | lisp/subr.el | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/lisp/subr.el b/lisp/subr.el index 74afd59f8d5..95783205ca2 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -4304,6 +4304,34 @@ as alpha versions." (version-list-= (version-to-list v1) (version-to-list v2))) +;;; Thread support. + +(defmacro with-mutex (mutex &rest body) + "Invoke BODY with MUTEX held, releasing MUTEX when done. +This is the simplest safe way to acquire and release a mutex." + (declare (indent 1) (debug t)) + (let ((sym (make-symbol "mutex"))) + `(let ((,sym ,mutex)) + (mutex-lock ,sym) + (unwind-protect + (progn ,@body) + (mutex-unlock ,sym))))) + +(defmacro until-condition (test condition) + "Wait for the condition variable CONDITION, checking TEST. +Acquire CONDITION's mutex, then check TEST. +If TEST evaluates to nil, repeatedly invoke `condition-wait' on CONDITION. +When CONDITION is signalled, check TEST again. + +This is the simplest safe way to invoke `condition-wait'." + (let ((cond-sym (make-symbol "condition"))) + `(let ((,cond-sym ,condition)) + (with-mutex (condition-mutex ,cond-sym) + (while (not ,test) + (condition-wait ,cond-sym)))))) + + + ;;; Misc. (defconst menu-bar-separator '("--") "Separator for menus.") |