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 (defun make-lock (&optional name
)
32 (mt:make-mutex
:name
(or name
"Anonymous lock")))
34 (defun acquire-lock (lock &optional
(wait-p t
))
35 (mt:mutex-lock lock
:timeout
(if wait-p nil
0)))
37 (defun release-lock (lock)
38 (mt:mutex-unlock lock
))
40 (defmacro with-lock-held
((place) &body body
)
41 `(mt:with-mutex-lock
(,place
) ,@body
))
43 (defun make-recursive-lock (&optional name
)
44 (mt:make-mutex
:name
(or name
"Anonymous recursive lock")
47 (defmacro with-recursive-lock-held
((place) &body body
)
48 `(mt:with-mutex-lock
(,place
) ,@body
))
50 ;;; Resource contention: condition variables
52 (defun make-condition-variable (&key name
)
53 (mt:make-exemption
:name
(or name
"Anonymous condition variable")))
55 (defun condition-wait (condition-variable lock
&key timeout
)
56 (mt:exemption-wait condition-variable lock
:timeout timeout
)
59 (defun condition-notify (condition-variable)
60 (mt:exemption-signal condition-variable
))
62 (defun thread-yield ()
67 (defmacro with-timeout
((timeout) &body body
)
69 `(mt:with-timeout
(,timeout
(error 'timeout
:length
,timeout
))
72 ;;; Introspection/debugging
74 ;;; VTZ: mt:list-threads returns all threads that are not garbage collected.
76 (delete-if-not #'mt
:thread-active-p
(mt:list-threads
)))
78 (defun interrupt-thread (thread function
&rest args
)
79 (mt:thread-interrupt thread
:function function
:arguments args
))
81 (defun destroy-thread (thread)
82 ;;; VTZ: actually we can kill ourselelf.
83 ;;; suicide is part of our contemporary life :)
84 (signal-error-if-current-thread thread
)
85 (mt:thread-interrupt thread
:function t
))
87 (defun thread-alive-p (thread)
88 (mt:thread-active-p thread
))
90 (defun join-thread (thread)
91 (mt:thread-join thread
))