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 (eval-when (:compile-toplevel
:execute
)
12 (when (>= ext
:+ecl-version-number
+ 230909)
13 (pushnew :has-timeouts
*features
*)))
15 ;;; documentation on the ECL Multiprocessing interface can be found at
16 ;;; https://ecl.common-lisp.dev/static/manual/Native-threads.html
23 (defun %make-thread
(function name
)
24 (mp:process-run-function name function
))
26 (defun current-thread ()
27 mp
::*current-process
*)
29 (defun threadp (object)
30 (typep object
'mp
:process
))
32 (defun thread-name (thread)
33 (mp:process-name thread
))
35 ;;; Resource contention: locks and recursive locks
37 (deftype lock
() 'mp
:lock
)
39 (deftype recursive-lock
()
40 '(and mp
:lock
(satisfies mp
:recursive-lock-p
)))
42 (defun lock-p (object)
43 (typep object
'mp
:lock
))
45 (defun recursive-lock-p (object)
46 (and (typep object
'mp
:lock
)
47 (mp:recursive-lock-p object
)))
49 (defun make-lock (&optional name
)
50 (mp:make-lock
:name
(or name
"Anonymous lock")))
52 (defun acquire-lock (lock &optional
(wait-p t
))
53 (mp:get-lock lock wait-p
))
55 (defun release-lock (lock)
56 (mp:giveup-lock lock
))
58 (defmacro with-lock-held
((place) &body body
)
59 `(mp:with-lock
(,place
) ,@body
))
61 (defun make-recursive-lock (&optional name
)
62 (mp:make-lock
:name
(or name
"Anonymous recursive lock") :recursive t
))
64 (defun acquire-recursive-lock (lock &optional
(wait-p t
))
65 (mp:get-lock lock wait-p
))
67 (defun release-recursive-lock (lock)
68 (mp:giveup-lock lock
))
70 (defmacro with-recursive-lock-held
((place) &body body
)
71 `(mp:with-lock
(,place
) ,@body
))
73 ;;; Resource contention: condition variables
75 (defun make-condition-variable (&key name
)
76 (declare (ignore name
))
77 (mp:make-condition-variable
))
79 (defun condition-wait (condition-variable lock
&key timeout
)
83 (with-timeout (timeout)
84 (mp:condition-variable-wait condition-variable lock
))
89 (mp:condition-variable-timedwait condition-variable lock timeout
)
90 (mp:condition-variable-wait condition-variable lock
)))
92 (defun condition-notify (condition-variable)
93 (mp:condition-variable-signal condition-variable
))
95 (defun thread-yield ()
98 ;;; Introspection/debugging
100 (defun all-threads ()
103 (defun interrupt-thread (thread function
&rest args
)
104 (flet ((apply-function ()
106 (named-lambda %interrupt-thread-wrapper
()
107 (apply function args
))
109 (declare (dynamic-extent #'apply-function
))
110 (mp:interrupt-process thread
(apply-function))))
112 (defun destroy-thread (thread)
113 (signal-error-if-current-thread thread
)
114 (mp:process-kill thread
))
116 (defun thread-alive-p (thread)
117 (mp:process-active-p thread
))
119 (defun join-thread (thread)
120 (mp:process-join thread
))
122 (eval-when (:compile-toplevel
:execute
)
123 (setf *features
* (remove :has-timeouts
*features
*)))