1 ;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; indent-tabs-mode: nil -*-
4 Copyright
2006, 2007 Greg Pfeil
5 Copyright
2010 Jean-Claude Beaudoin.
7 Distributed under the MIT license
(see LICENSE file
)
10 (in-package #:bordeaux-threads
)
17 (defun %make-thread
(function name
)
18 (mt:thread-run-function name function
))
20 (defun current-thread ()
23 (defun threadp (object)
24 (typep object
'mt
:thread
))
26 (defun thread-name (thread)
27 (mt:thread-name thread
))
29 ;;; Resource contention: locks and recursive locks
31 (defun make-lock (&optional name
)
32 (mt:make-lock
:name
(or name
"Anonymous lock")))
34 (defun acquire-lock (lock &optional
(wait-p t
))
35 (mt:get-lock lock wait-p
))
37 (defun release-lock (lock)
38 (mt:giveup-lock lock
))
40 (defmacro with-lock-held
((place) &body body
)
41 `(mt:with-lock
(,place
) ,@body
))
43 (defun make-recursive-lock (&optional name
)
44 (mt:make-lock
:name
(or name
"Anonymous recursive lock") :recursive t
))
46 (defun acquire-recursive-lock (lock &optional
(wait-p t
))
47 (mt:get-lock lock wait-p
))
49 (defun release-recursive-lock (lock)
50 (mt:giveup-lock lock
))
52 (defmacro with-recursive-lock-held
((place) &body body
)
53 `(mt:with-lock
(,place
) ,@body
))
55 ;;; Resource contention: condition variables
57 (defun make-condition-variable (&key name
)
58 (declare (ignore name
))
59 (mt:make-condition-variable
))
61 (defun condition-wait (condition-variable lock
)
62 (mt:condition-wait condition-variable lock
))
64 (defun condition-notify (condition-variable)
65 (mt:condition-signal condition-variable
))
67 (defun thread-yield ()
70 ;;; Introspection/debugging
75 (defun interrupt-thread (thread function
&rest args
)
76 (flet ((apply-function ()
78 (lambda () (apply function args
))
80 (declare (dynamic-extent #'apply-function
))
81 (mt:interrupt-thread thread
(apply-function))))
83 (defun destroy-thread (thread)
84 (signal-error-if-current-thread thread
)
85 (mt:thread-kill thread
))
87 (defun thread-alive-p (thread)
88 (mt:thread-active-p thread
))
90 (defun join-thread (thread)
91 (mt:thread-join thread
))