Remove deprecation warnings
[bordeaux-threads.git] / apiv1 / impl-cmucl.lisp
blob38281ba072854184a89897c2702a1c763754af29
1 ;;;; -*- indent-tabs-mode: nil -*-
3 #|
4 Copyright 2006, 2007 Greg Pfeil
6 Distributed under the MIT license (see LICENSE file)
7 |#
9 (in-package #:bordeaux-threads)
11 (deftype thread ()
12 'mp::process)
14 ;;; Thread Creation
16 (defun start-multiprocessing ()
17 (mp::startup-idle-and-top-level-loops))
19 (defun %make-thread (function name)
20 #+#.(cl:if (cl:find-symbol (cl:string '#:process-join) :mp) '(and) '(or))
21 (mp:make-process function :name name)
22 #-#.(cl:if (cl:find-symbol (cl:string '#:process-join) :mp) '(and) '(or))
23 (mp:make-process (named-lambda %join-thread-wrapper ()
24 (let ((return-values
25 (multiple-value-list (funcall function))))
26 (setf (getf (mp:process-property-list mp:*current-process*)
27 'return-values)
28 return-values)
29 (values-list return-values)))
30 :name name))
32 (defun current-thread ()
33 mp:*current-process*)
35 (defun threadp (object)
36 (mp:processp object))
38 (defun thread-name (thread)
39 (mp:process-name thread))
41 ;;; Resource contention: locks and recursive locks
43 (deftype lock () 'mp::error-check-lock)
45 (deftype recursive-lock () 'mp::recursive-lock)
47 (defun lock-p (object)
48 (typep object 'mp::error-check-lock))
50 (defun recursive-lock-p (object)
51 (typep object 'mp::recursive-lock))
53 (defun make-lock (&optional name)
54 (mp:make-lock (or name "Anonymous lock")
55 :kind :error-check))
57 (defun acquire-lock (lock &optional (wait-p t))
58 (if wait-p
59 (mp::lock-wait lock "Lock wait")
60 (mp::lock-wait-with-timeout lock "Lock wait" 0)))
62 (defun release-lock (lock)
63 (setf (mp::lock-process lock) nil))
65 (defmacro with-lock-held ((place) &body body)
66 `(mp:with-lock-held (,place "Lock wait") ,@body))
68 (defun make-recursive-lock (&optional name)
69 (mp:make-lock (or name "Anonymous recursive lock")
70 :kind :recursive))
72 (defun acquire-recursive-lock (lock &optional (wait-p t))
73 (acquire-lock lock))
75 (defun release-recursive-lock (lock)
76 (release-lock lock))
78 (defmacro with-recursive-lock-held ((place &key timeout) &body body)
79 `(mp:with-lock-held (,place "Lock Wait" :timeout ,timeout) ,@body))
81 ;;; Note that the locks _are_ recursive, but not "balanced", and only
82 ;;; checked if they are being held by the same process by with-lock-held.
83 ;;; The default with-lock-held in bordeaux-mp.lisp sort of works, in that
84 ;;; it will wait for recursive locks by the same process as well.
86 ;;; Resource contention: condition variables
88 ;;; There's some stuff in x86-vm.lisp that might be worth investigating
89 ;;; whether to build on. There's also process-wait and friends.
91 (defstruct condition-var
92 "CMUCL doesn't have conditions, so we need to create our own type."
93 name
94 lock
95 active)
97 (defun make-condition-variable (&key name)
98 (make-condition-var :lock (make-lock)
99 :name (or name "Anonymous condition variable")))
101 (defun condition-wait (condition-variable lock &key timeout)
102 (signal-error-if-condition-wait-timeout timeout)
103 (check-type condition-variable condition-var)
104 (with-lock-held ((condition-var-lock condition-variable))
105 (setf (condition-var-active condition-variable) nil))
106 (release-lock lock)
107 (mp:process-wait "Condition Wait"
108 #'(lambda () (condition-var-active condition-variable)))
109 (acquire-lock lock)
112 (defun condition-notify (condition-variable)
113 (check-type condition-variable condition-var)
114 (with-lock-held ((condition-var-lock condition-variable))
115 (setf (condition-var-active condition-variable) t))
116 (thread-yield))
118 (defun thread-yield ()
119 (mp:process-yield))
121 ;;; Timeouts
123 (defmacro with-timeout ((timeout) &body body)
124 (once-only (timeout)
125 `(mp:with-timeout (,timeout (error 'timeout :length ,timeout))
126 ,@body)))
128 ;;; Introspection/debugging
130 (defun all-threads ()
131 (mp:all-processes))
133 (defun interrupt-thread (thread function &rest args)
134 (flet ((apply-function ()
135 (if args
136 (lambda () (apply function args))
137 function)))
138 (declare (dynamic-extent #'apply-function))
139 (mp:process-interrupt thread (apply-function))))
141 (defun destroy-thread (thread)
142 (signal-error-if-current-thread thread)
143 (mp:destroy-process thread))
145 (defun thread-alive-p (thread)
146 (mp:process-active-p thread))
148 (defun join-thread (thread)
149 #+#.(cl:if (cl:find-symbol (cl:string '#:process-join) :mp) '(and) '(or))
150 (mp:process-join thread)
151 #-#.(cl:if (cl:find-symbol (cl:string '#:process-join) :mp) '(and) '(or))
152 (progn
153 (mp:process-wait (format nil "Waiting for thread ~A to complete" thread)
154 (lambda () (not (mp:process-alive-p thread))))
155 (let ((return-values
156 (getf (mp:process-property-list thread) 'return-values)))
157 (values-list return-values))))
159 (mark-supported)