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 ECL Multiprocessing interface can be found at
12 ;;; http://ecls.sourceforge.net/cgi-bin/view/Main/MultiProcessing
19 (defun %make-thread
(function name
)
20 (mp:process-run-function name function bordeaux-threads
:*default-special-bindings
*))
22 (defun current-thread ()
25 (defun threadp (object)
26 (typep object
'mp
:process
))
28 (defun thread-name (thread)
29 (mp:process-name thread
))
31 ;;; Resource contention: locks and recursive locks
33 (deftype lock
() 'mp
:mutex
)
35 (deftype recursive-lock
()
36 '(and mp
:mutex
(satisfies mp
:recursive-lock-p
)))
38 (defun lock-p (object)
39 (typep object
'mp
:mutex
))
41 (defun recursive-lock-p (object)
42 (and (typep object
'mp
:mutex
)
43 (mp:recursive-lock-p object
)))
45 (defun make-lock (&optional name
)
46 (mp:make-lock
:name
(or name
:anonymous
)))
48 (defun acquire-lock (lock &optional
(wait-p t
))
49 (mp:get-lock lock wait-p
))
51 (defun release-lock (lock)
52 (mp:giveup-lock lock
))
55 (defmacro with-lock-held
((place) &body body
)
56 `(mp:with-lock
(,place
) ,@body
))
58 (defun make-recursive-lock (&optional name
)
59 (mp:make-recursive-mutex
(or name
:anonymous-recursive-lock
)))
61 (defun acquire-recursive-lock (lock &optional
(wait-p t
))
62 (mp:get-lock lock wait-p
))
64 (defun release-recursive-lock (lock)
65 (mp:giveup-lock lock
))
67 (defmacro with-recursive-lock-held
((place) &body body
)
68 `(mp:with-lock
(,place
) ,@body
))
70 ;;; Resource contention: condition variables
72 (defun make-condition-variable (&key name
)
73 (declare (ignore name
))
74 (mp:make-condition-variable
))
76 (defun condition-wait (condition-variable lock
&key timeout
)
78 (mp:condition-variable-timedwait condition-variable lock timeout
)
79 (mp:condition-variable-wait condition-variable lock
))
82 (defun condition-notify (condition-variable)
83 (mp:condition-variable-signal condition-variable
))
85 (defun thread-yield ()
88 ;;; Introspection/debugging
93 (defun interrupt-thread (thread function
&rest args
)
94 (flet ((apply-function ()
96 (named-lambda %interrupt-thread-wrapper
()
97 (apply function args
))
99 (declare (dynamic-extent #'apply-function
))
100 (mp:interrupt-process thread
(apply-function))))
102 (defun destroy-thread (thread)
103 (signal-error-if-current-thread thread
)
104 (mp:process-kill thread
))
106 (defun thread-alive-p (thread)
107 (mp:process-active-p thread
))
109 (defun join-thread (thread)
110 (mp:process-join thread
))