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
)
15 (defun %make-thread
(function name
)
16 (mt:make-thread function
18 :initial-bindings mt
:*default-special-bindings
*))
20 (defun current-thread ()
23 (defun threadp (object)
26 (defun thread-name (thread)
27 (mt:thread-name thread
))
29 ;;; Resource contention: locks and recursive locks
31 (deftype lock
() 'mt
:mutex
)
33 (deftype recursive-lock
()
34 '(and mt
:mutex
(satisfies mt
:mutex-recursive-p
)))
36 (defun lock-p (object)
37 (typep object
'mt
:mutex
))
39 (defun recursive-lock-p (object)
40 (and (typep object
'mt
:mutex
)
41 (mt:mutex-recursive-p object
)))
43 (defun make-lock (&optional name
)
44 (mt:make-mutex
:name
(or name
"Anonymous lock")))
46 (defun acquire-lock (lock &optional
(wait-p t
))
47 (mt:mutex-lock lock
:timeout
(if wait-p nil
0)))
49 (defun release-lock (lock)
50 (mt:mutex-unlock lock
))
52 (defmacro with-lock-held
((place) &body body
)
53 `(mt:with-mutex-lock
(,place
) ,@body
))
55 (defun make-recursive-lock (&optional name
)
56 (mt:make-mutex
:name
(or name
"Anonymous recursive lock")
59 (defun acquire-recursive-lock (lock &optional
(wait-p t
))
60 (acquire-lock lock wait-p
))
62 (defun release-recursive-lock (lock)
65 (defmacro with-recursive-lock-held
((place) &body body
)
66 `(mt:with-mutex-lock
(,place
) ,@body
))
68 ;;; Resource contention: condition variables
70 (defun make-condition-variable (&key name
)
71 (mt:make-exemption
:name
(or name
"Anonymous condition variable")))
73 (defun condition-wait (condition-variable lock
&key timeout
)
74 (mt:exemption-wait condition-variable lock
:timeout timeout
)
77 (defun condition-notify (condition-variable)
78 (mt:exemption-signal condition-variable
))
80 (defun thread-yield ()
85 (defmacro with-timeout
((timeout) &body body
)
87 `(mt:with-timeout
(,timeout
(error 'timeout
:length
,timeout
))
90 ;;; Introspection/debugging
92 ;;; VTZ: mt:list-threads returns all threads that are not garbage collected.
94 (delete-if-not #'mt
:thread-active-p
(mt:list-threads
)))
96 (defun interrupt-thread (thread function
&rest args
)
97 (mt:thread-interrupt thread
:function function
:arguments args
))
99 (defun destroy-thread (thread)
100 ;;; VTZ: actually we can kill ourselelf.
101 ;;; suicide is part of our contemporary life :)
102 (signal-error-if-current-thread thread
)
103 (mt:thread-interrupt thread
:function t
))
105 (defun thread-alive-p (thread)
106 (mt:thread-active-p thread
))
108 (defun join-thread (thread)
109 (values-list (mt:thread-join thread
)))