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 SBCL Threads interface can be found at
12 ;;; http://www.sbcl.org/manual/Threading.html
19 (defun %make-thread
(function name
)
20 (sb-thread:make-thread function
:name name
))
22 (defun current-thread ()
23 sb-thread
:*current-thread
*)
25 (defun threadp (object)
26 (typep object
'sb-thread
:thread
))
28 (defun thread-name (thread)
29 (sb-thread:thread-name thread
))
31 ;;; Resource contention: locks and recursive locks
33 (defun make-lock (&optional name
)
34 (sb-thread:make-mutex
:name
(or name
"Anonymous lock")))
36 (defun acquire-lock (lock &optional
(wait-p t
))
37 #+#.
(cl:if
(cl:find-symbol
(cl:string
'#:grab-mutex
) :sb-thread
) '(and) '(or))
38 (sb-thread:grab-mutex lock
:waitp wait-p
)
39 #-
#.
(cl:if
(cl:find-symbol
(cl:string
'#:grab-mutex
) :sb-thread
) '(and) '(or))
40 (sb-thread:get-mutex lock nil wait-p
))
42 (defun release-lock (lock)
43 (sb-thread:release-mutex lock
))
45 (defmacro with-lock-held
((place) &body body
)
46 `(sb-thread:with-mutex
(,place
) ,@body
))
48 (defun make-recursive-lock (&optional name
)
49 (sb-thread:make-mutex
:name
(or name
"Anonymous recursive lock")))
51 ;;; XXX acquire-recursive-lock and release-recursive-lock are actually
52 ;;; complicated because we can't use control stack tricks. We need to
53 ;;; actually count something to check that the acquire/releases are
56 (defmacro with-recursive-lock-held
((place) &body body
)
57 `(sb-thread:with-recursive-lock
(,place
)
60 ;;; Resource contention: condition variables
62 (defun make-condition-variable (&key name
)
63 (sb-thread:make-waitqueue
:name
(or name
"Anonymous condition variable")))
65 (defun condition-wait (condition-variable lock
)
66 (sb-thread:condition-wait condition-variable lock
))
68 (defun condition-notify (condition-variable)
69 (sb-thread:condition-notify condition-variable
))
71 (defun thread-yield ()
72 (sb-thread:release-foreground
))
79 (defmacro with-timeout
((timeout) &body body
)
80 `(sb-ext:with-timeout
,timeout
83 ;;; Introspection/debugging
86 (sb-thread:list-all-threads
))
88 (defun interrupt-thread (thread function
&rest args
)
89 (flet ((apply-function ()
91 (lambda () (apply function args
))
93 (declare (dynamic-extent #'apply-function
))
94 (sb-thread:interrupt-thread thread
(apply-function))))
96 (defun destroy-thread (thread)
97 (signal-error-if-current-thread thread
)
98 (sb-thread:terminate-thread thread
))
100 (defun thread-alive-p (thread)
101 (sb-thread:thread-alive-p thread
))
103 (defun join-thread (thread)
104 (sb-thread:join-thread thread
))