2 Copyright
2006, 2007 Greg Pfeil
4 Distributed under the MIT license
(see LICENSE file
)
7 (in-package #:bordeaux-threads
)
11 (defun make-thread (function &key name
)
12 (mp:make-process function
:name name
))
14 (defun current-thread ()
17 (defmethod threadp (object)
20 (defun thread-name (thread)
21 (mp:process-name thread
))
23 ;;; Resource contention: locks and recursive locks
25 (defun make-lock (&optional name
)
28 (defun acquire-lock (lock &optional
(wait-p t
))
30 (mp::lock-wait lock
"Lock")
31 (mp::lock-wait-with-timeout lock
"Lock" 0)))
33 (defun release-lock (lock)
34 (setf (mp::lock-process lock
) nil
))
36 (defmacro with-lock-held
((place) &body body
)
37 `(mp:with-lock-held
(,place
) ,@body
))
39 (defmacro with-recursive-lock-held
((place &key timeout
) &body body
)
40 `(mp:with-lock-held
(,place
"Lock Wait" :timeout
,timeout
) ,@body
))
42 ;;; Note that the locks _are_ recursive, but not "balanced", and only
43 ;;; checked if they are being held by the same process by with-lock-held.
44 ;;; The default with-lock-held in bordeaux-mp.lisp sort of works, in that
45 ;;; it will wait for recursive locks by the same process as well.
47 ;;; Resource contention: condition variables
49 ;;; There's some stuff in x86-vm.lisp that might be worth investigating
50 ;;; whether to build on. There's also process-wait and friends.
52 (defstruct condition-var
53 "CMUCL doesn't have conditions, so we need to create our own type."
57 (defun make-condition-variable ()
58 (make-condition-var :lock
(make-lock)))
60 (defun condition-wait (condition-variable lock
)
61 (check-type condition-variable condition-var
)
62 (with-lock-held ((condition-var-lock condition-variable
))
63 (setf (condition-var-active condition-variable
) nil
))
65 (mp:process-wait
"Condition Wait"
66 #'(lambda () (condition-var-active condition-variable
)))
70 (defun condition-notify (condition-variable)
71 (check-type condition-variable condition-var
)
72 (with-lock-held ((condition-var-lock condition-variable
))
73 (setf (condition-var-active condition-variable
) t
))
76 (defun thread-yield ()
79 ;;; Introspection/debugging
84 (defun interrupt-thread (thread function
)
85 (mp:process-interrupt thread function
))
87 (defun destroy-thread (thread)
88 (signal-error-if-current-thread thread
)
89 (mp:destroy-process thread
))
91 (defun thread-alive-p (thread)
92 (mp:process-active-p thread
))