1 ;;;; -*- indent-tabs-mode: nil -*-
4 Copyright
2006, 2007 Greg Pfeil
6 Distributed under the MIT license
(see LICENSE file
)
9 (in-package #:bordeaux-threads
)
11 ;;; documentation on the ECL Multiprocessing interface can be found at
12 ;;; http://ecls.sourceforge.net/cgi-bin/view/Main/MultiProcessing
19 (defun %make-thread
(function name
)
20 (mp:process-run-function name function
))
22 (defun current-thread ()
23 mp
::*current-process
*)
25 (defun threadp (object)
26 (typep object
'mp
:process
))
28 (defun thread-name (thread)
29 (mp:process-name thread
))
31 ;;; Resource contention: locks and recursive locks
33 (deftype lock
() 'mp
:lock
)
35 (deftype recursive-lock
()
36 '(and mp
:lock
(satisfies mp
:recursive-lock-p
)))
38 (defun lock-p (object)
39 (typep object
'mp
:lock
))
41 (defun recursive-lock-p (object)
42 (and (typep object
'mp
:lock
)
43 (mp:recursive-lock-p object
)))
45 (defun make-lock (&optional name
)
46 (mp:make-lock
:name
(or name
"Anonymous lock")))
48 (defun acquire-lock (lock &optional
(wait-p t
))
49 (mp:get-lock lock wait-p
))
51 (defun release-lock (lock)
52 (mp:giveup-lock lock
))
54 (defmacro with-lock-held
((place) &body body
)
55 `(mp:with-lock
(,place
) ,@body
))
57 (defun make-recursive-lock (&optional name
)
58 (mp:make-lock
:name
(or name
"Anonymous recursive lock") :recursive t
))
60 (defun acquire-recursive-lock (lock &optional
(wait-p t
))
61 (mp:get-lock lock wait-p
))
63 (defun release-recursive-lock (lock)
64 (mp:giveup-lock lock
))
66 (defmacro with-recursive-lock-held
((place) &body body
)
67 `(mp:with-lock
(,place
) ,@body
))
69 ;;; Resource contention: condition variables
71 (defun make-condition-variable (&key name
)
72 (declare (ignore name
))
73 (mp:make-condition-variable
))
75 (defun condition-wait (condition-variable lock
&key timeout
)
77 (handler-case (with-timeout (timeout)
78 (mp:condition-variable-wait condition-variable lock
))
82 (mp:condition-variable-wait condition-variable lock
)))
84 (defun condition-notify (condition-variable)
85 (mp:condition-variable-signal condition-variable
))
87 (defun thread-yield ()
90 ;;; Introspection/debugging
95 (defun interrupt-thread (thread function
&rest args
)
96 (flet ((apply-function ()
98 (named-lambda %interrupt-thread-wrapper
()
99 (apply function args
))
101 (declare (dynamic-extent #'apply-function
))
102 (mp:interrupt-process thread
(apply-function))))
104 (defun destroy-thread (thread)
105 (signal-error-if-current-thread thread
)
106 (mp:process-kill thread
))
108 (defun thread-alive-p (thread)
109 (mp:process-active-p thread
))
111 (defun join-thread (thread)
112 (mp:process-join thread
))