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
)
64 (mp:condition-variable-wait condition-variable lock
))
66 (defun condition-notify (condition-variable)
67 (mp:condition-variable-signal condition-variable
))
69 (defun thread-yield ()
72 ;;; Introspection/debugging
77 (defun interrupt-thread (thread function
&rest args
)
78 (flet ((apply-function ()
80 (lambda () (apply function args
))
82 (declare (dynamic-extent #'apply-function
))
83 (mp:interrupt-process thread
(apply-function))))
85 (defun destroy-thread (thread)
86 (signal-error-if-current-thread thread
)
87 (mp:process-kill thread
))
89 (defun thread-alive-p (thread)
90 (mp:process-active-p thread
))
92 (defun join-thread (thread)
93 (mp:process-join thread
))