diff options
| author | Tom Tromey | 2012-08-20 07:56:02 -0600 |
|---|---|---|
| committer | Tom Tromey | 2012-08-20 07:56:02 -0600 |
| commit | fb77afbe75308507885113a56017f095da8ba1cc (patch) | |
| tree | 5e87b5bcff336c0db59e180cc2eefde8f184cfa8 | |
| parent | 49bc1a9dfc6e81a370bf12157c3c573743ee200a (diff) | |
| download | emacs-fb77afbe75308507885113a56017f095da8ba1cc.tar.gz 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." | |||
| 4304 | (version-list-= (version-to-list v1) (version-to-list v2))) | 4304 | (version-list-= (version-to-list v1) (version-to-list v2))) |
| 4305 | 4305 | ||
| 4306 | 4306 | ||
| 4307 | ;;; Thread support. | ||
| 4308 | |||
| 4309 | (defmacro with-mutex (mutex &rest body) | ||
| 4310 | "Invoke BODY with MUTEX held, releasing MUTEX when done. | ||
| 4311 | This is the simplest safe way to acquire and release a mutex." | ||
| 4312 | (declare (indent 1) (debug t)) | ||
| 4313 | (let ((sym (make-symbol "mutex"))) | ||
| 4314 | `(let ((,sym ,mutex)) | ||
| 4315 | (mutex-lock ,sym) | ||
| 4316 | (unwind-protect | ||
| 4317 | (progn ,@body) | ||
| 4318 | (mutex-unlock ,sym))))) | ||
| 4319 | |||
| 4320 | (defmacro until-condition (test condition) | ||
| 4321 | "Wait for the condition variable CONDITION, checking TEST. | ||
| 4322 | Acquire CONDITION's mutex, then check TEST. | ||
| 4323 | If TEST evaluates to nil, repeatedly invoke `condition-wait' on CONDITION. | ||
| 4324 | When CONDITION is signalled, check TEST again. | ||
| 4325 | |||
| 4326 | This is the simplest safe way to invoke `condition-wait'." | ||
| 4327 | (let ((cond-sym (make-symbol "condition"))) | ||
| 4328 | `(let ((,cond-sym ,condition)) | ||
| 4329 | (with-mutex (condition-mutex ,cond-sym) | ||
| 4330 | (while (not ,test) | ||
| 4331 | (condition-wait ,cond-sym)))))) | ||
| 4332 | |||
| 4333 | |||
| 4334 | |||
| 4307 | ;;; Misc. | 4335 | ;;; Misc. |
| 4308 | (defconst menu-bar-separator '("--") | 4336 | (defconst menu-bar-separator '("--") |
| 4309 | "Separator for menus.") | 4337 | "Separator for menus.") |