Tighter floating-point type constraints in some cases
[sbcl.git] / src / code / thread.lisp
1 ;;;; support for threads needed at cross-compile time
2
3 ;;;; This software is part of the SBCL system. See the README file for
4 ;;;; more information.
5 ;;;;
6 ;;;; This software is derived from the CMU CL system, which was
7 ;;;; written at Carnegie Mellon University and released into the
8 ;;;; public domain. The software is in the public domain and is
9 ;;;; provided with absolutely no warranty. See the COPYING and CREDITS
10 ;;;; files for more information.
11
12 (in-package "SB!THREAD")
13
14 (def!type thread-name ()
15   'simple-string)
16
17 (def!struct (thread (:constructor %make-thread))
18   #!+sb-doc
19   "Thread type. Do not rely on threads being structs as it may change
20 in future versions."
21   (name          nil :type (or thread-name null))
22   (%alive-p      nil :type boolean)
23   (os-thread     nil :type (or integer null))
24   (interruptions nil :type list)
25   (result        nil :type list)
26   (interruptions-lock
27    (make-mutex :name "thread interruptions lock")
28    :type mutex)
29   (result-lock
30    (make-mutex :name "thread result lock")
31    :type mutex)
32   waiting-for)
33
34 (def!struct mutex
35   #!+sb-doc
36   "Mutex type."
37   (name   nil :type (or null thread-name))
38   (%owner nil :type (or null thread))
39   #!+(and sb-thread sb-futex)
40   (state    0 :type fixnum))
41
42 (defun mutex-value (mutex)
43   "Current owner of the mutex, NIL if the mutex is free. May return a
44 stale value, use MUTEX-OWNER instead."
45   (mutex-%owner mutex))
46
47 (defun holding-mutex-p (mutex)
48   "Test whether the current thread is holding MUTEX."
49   ;; This is about the only use for which a stale value of owner is
50   ;; sufficient.
51   (eq sb!thread:*current-thread* (mutex-%owner mutex)))
52
53 (defsetf mutex-value set-mutex-value)
54
55 (declaim (inline set-mutex-value))
56 (defun set-mutex-value (mutex value)
57   (declare (ignore mutex value))
58   (error "~S is no longer supported." '(setf mutex-value)))
59
60 (define-compiler-macro set-mutex-value (&whole form mutex value)
61   (declare (ignore mutex value))
62   (warn "~S is no longer supported, and will signal an error at runtime."
63         '(setf mutex-value))
64   form)
65
66 ;;; SPINLOCK no longer exists as a type -- provided for backwards compatibility.
67
68 (deftype spinlock ()
69   "Spinlock type."
70   (deprecation-warning :early "1.0.53.11" 'spinlock 'mutex)
71   'mutex)
72
73 (define-deprecated-function :early "1.0.53.11" make-spinlock make-mutex (&key name)
74   (make-mutex :name name))
75
76 (define-deprecated-function :early "1.0.53.11" spinlock-name mutex-name (lock)
77   (mutex-name lock))
78
79 (define-deprecated-function :early "1.0.53.11" (setf spinlock-name) (setf mutex-name) (name lock)
80   (setf (mutex-name lock) name))
81
82 (define-deprecated-function :early "1.0.53.11" spinlock-value mutex-owner (lock)
83   (mutex-owner lock))
84
85 (define-deprecated-function :early "1.0.53.11" get-spinlock grab-mutex (lock)
86   (grab-mutex lock))
87
88 (define-deprecated-function :early "1.0.53.11" release-spinlock release-mutex (lock)
89   (release-mutex lock))
90
91 (sb!xc:defmacro with-recursive-spinlock ((lock) &body body)
92   (deprecation-warning :early "1.0.53.11" 'with-recursive-spinlock 'with-recursive-lock)
93   `(with-recursive-lock (,lock)
94      ,@body))
95
96 (sb!xc:defmacro with-spinlock ((lock) &body body)
97   (deprecation-warning :early "1.0.53.11" 'with-recursive-spinlock 'with-mutex)
98   `(with-lock (,lock)
99      ,@body))
100
101 (sb!xc:defmacro without-thread-waiting-for ((&key already-without-interrupts) &body body)
102   (with-unique-names (thread prev)
103     (let ((without (if already-without-interrupts
104                        'progn
105                        'without-interrupts))
106           (with (if already-without-interrupts
107                     'progn
108                     'with-local-interrupts)))
109       `(let* ((,thread *current-thread*)
110               (,prev (progn
111                        (barrier (:read))
112                        (thread-waiting-for ,thread))))
113          (flet ((exec () ,@body))
114            (if ,prev
115                (,without
116                 (unwind-protect
117                      (progn
118                        (setf (thread-waiting-for ,thread) nil)
119                        (barrier (:write))
120                        (,with (exec)))
121                   ;; If we were waiting on a waitqueue, this becomes a bogus
122                   ;; wakeup.
123                   (when (mutex-p ,prev)
124                     (setf (thread-waiting-for ,thread) ,prev)
125                     (barrier (:write)))))
126                (exec)))))))
127
128 (sb!xc:defmacro with-mutex ((mutex &key (value '*current-thread*) (wait-p t))
129                             &body body)
130   #!+sb-doc
131   "Acquire MUTEX for the dynamic scope of BODY, setting it to VALUE or
132 some suitable default value if NIL.  If WAIT-P is non-NIL and the mutex
133 is in use, sleep until it is available"
134   `(dx-flet ((with-mutex-thunk () ,@body))
135      (call-with-mutex
136       #'with-mutex-thunk
137       ,mutex
138       ,value
139       ,wait-p)))
140
141 (sb!xc:defmacro with-system-mutex ((mutex
142                                     &key without-gcing allow-with-interrupts)
143                                    &body body)
144   `(dx-flet ((with-system-mutex-thunk () ,@body))
145      (,(cond (without-gcing
146                'call-with-system-mutex/without-gcing)
147              (allow-with-interrupts
148               'call-with-system-mutex/allow-with-interrupts)
149              (t
150               'call-with-system-mutex))
151        #'with-system-mutex-thunk
152        ,mutex)))
153
154 (sb!xc:defmacro with-recursive-lock ((mutex) &body body)
155   #!+sb-doc
156   "Acquires MUTEX for the dynamic scope of BODY. Within that scope
157 further recursive lock attempts for the same mutex succeed. It is
158 allowed to mix WITH-MUTEX and WITH-RECURSIVE-LOCK for the same mutex
159 provided the default value is used for the mutex."
160   `(dx-flet ((with-recursive-lock-thunk () ,@body))
161      (call-with-recursive-lock
162       #'with-recursive-lock-thunk
163       ,mutex)))
164
165 (sb!xc:defmacro with-recursive-system-lock ((lock
166                                              &key without-gcing)
167                                             &body body)
168   `(dx-flet ((with-recursive-system-lock-thunk () ,@body))
169      (,(cond (without-gcing
170               'call-with-recursive-system-lock/without-gcing)
171              (t
172               'call-with-recursive-system-lock))
173       #'with-recursive-system-lock-thunk
174        ,lock)))
175
176 (macrolet ((def (name &optional variant)
177              `(defun ,(if variant (symbolicate name "/" variant) name)
178                   (function mutex)
179                 (declare (function function))
180                 (flet ((%call-with-system-mutex ()
181                          (dx-let (got-it)
182                            (unwind-protect
183                                 (when (setf got-it (get-mutex mutex))
184                                   (funcall function))
185                              (when got-it
186                                (release-mutex mutex))))))
187                   (declare (inline %call-with-system-mutex))
188                   ,(ecase variant
189                      (:without-gcing
190                        `(without-gcing (%call-with-system-mutex)))
191                      (:allow-with-interrupts
192                        `(without-interrupts
193                           (allow-with-interrupts (%call-with-system-mutex))))
194                      ((nil)
195                       `(without-interrupts (%call-with-system-mutex))))))))
196   (def call-with-system-mutex)
197   (def call-with-system-mutex :without-gcing)
198   (def call-with-system-mutex :allow-with-interrupts))
199
200 #!-sb-thread
201 (progn
202   (defun call-with-mutex (function mutex value waitp)
203     (declare (ignore mutex value waitp)
204              (function function))
205     (funcall function))
206
207   (defun call-with-recursive-lock (function mutex)
208     (declare (ignore mutex) (function function))
209     (funcall function))
210
211   (defun call-with-recursive-system-lock (function lock)
212     (declare (function function) (ignore lock))
213     (without-interrupts
214       (funcall function)))
215
216   (defun call-with-recursive-system-lock/without-gcing (function mutex)
217     (declare (function function) (ignore mutex))
218     (without-gcing
219       (funcall function))))
220
221 #!+sb-thread
222 ;;; KLUDGE: These need to use DX-LET, because the cleanup form that
223 ;;; closes over GOT-IT causes a value-cell to be allocated for it --
224 ;;; and we prefer that to go on the stack since it can.
225 (progn
226   (defun call-with-mutex (function mutex value waitp)
227     (declare (function function))
228     (dx-let ((got-it nil))
229       (without-interrupts
230         (unwind-protect
231              (when (setq got-it (allow-with-interrupts
232                                  (get-mutex mutex value waitp)))
233                (with-local-interrupts (funcall function)))
234           (when got-it
235             (release-mutex mutex))))))
236
237   (defun call-with-recursive-lock (function mutex)
238     (declare (function function))
239     (dx-let ((inner-lock-p (eq (mutex-%owner mutex) *current-thread*))
240              (got-it nil))
241       (without-interrupts
242         (unwind-protect
243              (when (or inner-lock-p (setf got-it (allow-with-interrupts
244                                                   (get-mutex mutex))))
245                (with-local-interrupts (funcall function)))
246           (when got-it
247             (release-mutex mutex))))))
248
249   (macrolet ((def (name &optional variant)
250                `(defun ,(if variant (symbolicate name "/" variant) name)
251                     (function lock)
252                   (declare (function function))
253                   (flet ((%call-with-recursive-system-lock ()
254                            (dx-let ((inner-lock-p
255                                      (eq *current-thread* (mutex-owner lock)))
256                                     (got-it nil))
257                              (unwind-protect
258                                   (when (or inner-lock-p
259                                             (setf got-it (grab-mutex lock)))
260                                     (funcall function))
261                                (when got-it
262                                  (release-mutex lock))))))
263                     (declare (inline %call-with-recursive-system-lock))
264                     ,(ecase variant
265                       (:without-gcing
266                         `(without-gcing (%call-with-recursive-system-lock)))
267                       ((nil)
268                         `(without-interrupts (%call-with-recursive-system-lock))))))))
269     (def call-with-recursive-system-lock)
270     (def call-with-recursive-system-lock :without-gcing)))