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 (defun make-lock (&optional name
)
34 (mp:make-lock
:name
(or name
"Anonymous lock")))
36 (defun acquire-lock (lock &optional
(wait-p t
))
37 (mp:get-lock lock wait-p
))
39 (defun release-lock (lock)
40 (mp:giveup-lock lock
))
42 (defmacro with-lock-held
((place) &body body
)
43 `(mp:with-lock
(,place
) ,@body
))
45 (defun make-recursive-lock (&optional name
)
46 (mp:make-lock
:name
(or name
"Anonymous recursive lock") :recursive t
))
48 (defun acquire-recursive-lock (lock &optional
(wait-p t
))
49 (mp:get-lock lock wait-p
))
51 (defun release-recursive-lock (lock)
52 (mp:giveup-lock lock
))
54 (defmacro with-recursive-lock-held
((place) &body body
)
55 `(mp:with-lock
(,place
) ,@body
))
57 ;;; Resource contention: condition variables
59 (defun make-condition-variable (&key name
)
60 (declare (ignore name
))
61 (mp:make-condition-variable
))
63 (defun condition-wait (condition-variable lock
&key timeout
)
65 (mp:condition-variable-timedwait condition-variable lock timeout
)
66 (mp:condition-variable-wait condition-variable lock
))
69 (defun condition-notify (condition-variable)
70 (mp:condition-variable-signal condition-variable
))
72 (defun thread-yield ()
75 ;;; Introspection/debugging
80 (defun interrupt-thread (thread function
&rest args
)
81 (flet ((apply-function ()
83 (lambda () (apply function args
))
85 (declare (dynamic-extent #'apply-function
))
86 (mp:interrupt-process thread
(apply-function))))
88 (defun destroy-thread (thread)
89 (signal-error-if-current-thread thread
)
90 (mp:process-kill thread
))
92 (defun thread-alive-p (thread)
93 (mp:process-active-p thread
))
95 (defun join-thread (thread)
96 (mp:process-join thread
))