Add safepoint mechanism
[sbcl.git] / src / code / target-thread.lisp
1 ;;;; support for threads in the target machine
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 ;;; CAS Lock
15 ;;;
16 ;;; Locks don't come any simpler -- or more lightweight than this. While
17 ;;; this is probably a premature optimization for most users, we still
18 ;;; need it internally for implementing condition variables outside Futex
19 ;;; builds.
20
21 (defmacro with-cas-lock ((place) &body body)
22   #!+sb-doc
23   "Runs BODY with interrupts disabled and *CURRENT-THREAD* compare-and-swapped
24 into PLACE instead of NIL. PLACE must be a place acceptable to
25 COMPARE-AND-SWAP, and must initially hold NIL.
26
27 WITH-CAS-LOCK is suitable mostly when the critical section needing protection
28 is very small, and cost of allocating a separate lock object would be
29 prohibitive. While it is the most lightweight locking constructed offered by
30 SBCL, it is also the least scalable if the section is heavily contested or
31 long.
32
33 WITH-CAS-LOCK can be entered recursively."
34   `(without-interrupts
35      (%with-cas-lock (,place) ,@body)))
36
37 (defmacro %with-cas-lock ((place) &body body &environment env)
38   (with-unique-names (owner self)
39     (multiple-value-bind (vars vals old new cas-form read-form)
40         (sb!ext:get-cas-expansion place env)
41       `(let* (,@(mapcar #'list vars vals)
42               (,owner (progn
43                         (barrier (:read))
44                         ,read-form))
45               (,self *current-thread*)
46               (,old nil)
47               (,new ,self))
48          (unwind-protect
49               (progn
50                 (unless (eq ,owner ,self)
51                   (loop until (loop repeat 100
52                                     when (and (progn
53                                                 (barrier (:read))
54                                                 (not ,read-form))
55                                               (not (setf ,owner ,cas-form)))
56                                     return t
57                                     else
58                                     do (sb!ext:spin-loop-hint))
59                         do (thread-yield)))
60                 ,@body)
61            (unless (eq ,owner ,self)
62              (let ((,old ,self)
63                    (,new nil))
64                (unless (eq ,old ,cas-form)
65                  (bug "Failed to release CAS lock!")))))))))
66
67 ;;; Conditions
68
69 (define-condition thread-error (error)
70   ((thread :reader thread-error-thread :initarg :thread))
71   #!+sb-doc
72   (:documentation
73    "Conditions of type THREAD-ERROR are signalled when thread operations fail.
74 The offending thread is initialized by the :THREAD initialization argument and
75 read by the function THREAD-ERROR-THREAD."))
76
77 (define-condition simple-thread-error (thread-error simple-condition)
78   ())
79
80 (define-condition thread-deadlock (thread-error)
81   ((cycle :initarg :cycle :reader thread-deadlock-cycle))
82   (:report
83    (lambda (condition stream)
84      (let* ((*print-circle* t)
85             (cycle (thread-deadlock-cycle condition))
86             (start (caar cycle)))
87        (format stream "Deadlock cycle detected:~%")
88        (loop for part = (pop cycle)
89              while part
90              do (format stream "    ~S~%  waited for:~%    ~S~%  owned by:~%"
91                         (car part)
92                         (cdr part)))
93        (format stream "    ~S~%" start)))))
94
95 #!+sb-doc
96 (setf
97  (fdocumentation 'thread-error-thread 'function)
98  "Return the offending thread that the THREAD-ERROR pertains to.")
99
100 (define-condition symbol-value-in-thread-error (cell-error thread-error)
101   ((info :reader symbol-value-in-thread-error-info :initarg :info))
102   (:report
103    (lambda (condition stream)
104      (destructuring-bind (op problem)
105          (symbol-value-in-thread-error-info condition)
106        (format stream "Cannot ~(~A~) value of ~S in ~S: ~S"
107                op
108                (cell-error-name condition)
109                (thread-error-thread condition)
110                (ecase problem
111                  (:unbound-in-thread "the symbol is unbound in thread.")
112                  (:no-tls-value "the symbol has no thread-local value.")
113                  (:thread-dead "the thread has exited.")
114                  (:invalid-tls-value "the thread-local value is not valid."))))))
115   #!+sb-doc
116   (:documentation
117    "Signalled when SYMBOL-VALUE-IN-THREAD or its SETF version fails due to eg.
118 the symbol not having a thread-local value, or the target thread having
119 exited. The offending symbol can be accessed using CELL-ERROR-NAME, and the
120 offending thread using THREAD-ERROR-THREAD."))
121
122 (define-condition join-thread-error (thread-error)
123   ((problem :initarg :problem :reader join-thread-problem))
124   (:report (lambda (c s)
125              (ecase (join-thread-problem c)
126                (:abort
127                 (format s "Joining thread failed: thread ~A ~
128                            did not return normally."
129                         (thread-error-thread c)))
130                (:timeout
131                 (format s "Joining thread timed out: thread ~A ~
132                            did not exit in time."
133                         (thread-error-thread c))))))
134   #!+sb-doc
135   (:documentation
136    "Signalled when joining a thread fails due to abnormal exit of the thread
137 to be joined. The offending thread can be accessed using
138 THREAD-ERROR-THREAD."))
139
140 (define-deprecated-function :late "1.0.29.17" join-thread-error-thread thread-error-thread
141     (condition)
142   (thread-error-thread condition))
143
144 (define-condition interrupt-thread-error (thread-error) ()
145   (:report (lambda (c s)
146              (format s "Interrupt thread failed: thread ~A has exited."
147                      (thread-error-thread c))))
148   #!+sb-doc
149   (:documentation
150    "Signalled when interrupting a thread fails because the thread has already
151 exited. The offending thread can be accessed using THREAD-ERROR-THREAD."))
152
153 (define-deprecated-function :late "1.0.29.17" interrupt-thread-error-thread thread-error-thread
154     (condition)
155   (thread-error-thread condition))
156
157 ;;; Of the WITH-PINNED-OBJECTS in this file, not every single one is
158 ;;; necessary because threads are only supported with the conservative
159 ;;; gencgc and numbers on the stack (returned by GET-LISP-OBJ-ADDRESS)
160 ;;; are treated as references.
161
162 ;;; set the doc here because in early-thread FDOCUMENTATION is not
163 ;;; available, yet
164 #!+sb-doc
165 (setf (fdocumentation '*current-thread* 'variable)
166       "Bound in each thread to the thread itself.")
167
168 #!+sb-doc
169 (setf
170  (fdocumentation 'thread-name 'function)
171  "Name of the thread. Can be assigned to using SETF. Thread names can be
172 arbitrary printable objects, and need not be unique.")
173
174 (def!method print-object ((thread thread) stream)
175   (print-unreadable-object (thread stream :type t :identity t)
176     (let* ((cookie (list thread))
177            (info (if (thread-alive-p thread)
178                      :running
179                      (multiple-value-list
180                       (join-thread thread :default cookie))))
181            (state (if (eq :running info)
182                       (let* ((thing (progn
183                                       (barrier (:read))
184                                       (thread-waiting-for thread))))
185                         (typecase thing
186                           (cons
187                            (list "waiting on:" (cdr thing)
188                                  "timeout: " (car thing)))
189                           (null
190                            (list info))
191                           (t
192                            (list "waiting on:" thing))))
193                       (if (eq cookie (car info))
194                           (list :aborted)
195                           :finished)))
196            (values (when (eq :finished state)
197                      info))
198            (*print-level* 4))
199       (format stream
200               "~@[~S ~]~:[~{~I~A~^~2I~_ ~}~_~;~A~:[ no values~; values: ~:*~{~S~^, ~}~]~]"
201               (thread-name thread)
202               (eq :finished state)
203               state
204               values))))
205
206 (defun print-lock (lock name owner stream)
207   (let ((*print-circle* t))
208     (print-unreadable-object (lock stream :type t :identity (not name))
209       (if owner
210           (format stream "~@[~S ~]~2I~_owner: ~S" name owner)
211           (format stream "~@[~S ~](free)" name)))))
212
213 (def!method print-object ((mutex mutex) stream)
214   (print-lock mutex (mutex-name mutex) (mutex-owner mutex) stream))
215
216 (defun thread-alive-p (thread)
217   #!+sb-doc
218   "Return T if THREAD is still alive. Note that the return value is
219 potentially stale even before the function returns, as the thread may exit at
220 any time."
221   (thread-%alive-p thread))
222
223 ;; A thread is eligible for gc iff it has finished and there are no
224 ;; more references to it. This list is supposed to keep a reference to
225 ;; all running threads.
226 (defvar *all-threads* ())
227 (defvar *all-threads-lock* (make-mutex :name "all threads lock"))
228
229 (defvar *default-alloc-signal* nil)
230
231 (defmacro with-all-threads-lock (&body body)
232   `(with-system-mutex (*all-threads-lock*)
233      ,@body))
234
235 (defun list-all-threads ()
236   #!+sb-doc
237   "Return a list of the live threads. Note that the return value is
238 potentially stale even before the function returns, as new threads may be
239 created and old ones may exit at any time."
240   (with-all-threads-lock
241     (copy-list *all-threads*)))
242
243 (declaim (inline current-thread-sap))
244 (defun current-thread-sap ()
245   (sb!vm::current-thread-offset-sap sb!vm::thread-this-slot))
246
247 (declaim (inline current-thread-os-thread))
248 (defun current-thread-os-thread ()
249   #!+sb-thread
250   (sap-int (sb!vm::current-thread-offset-sap sb!vm::thread-os-thread-slot))
251   #!-sb-thread
252   0)
253
254 (defvar *initial-thread* nil)
255 (defvar *make-thread-lock*)
256
257 (defun init-initial-thread ()
258   (/show0 "Entering INIT-INITIAL-THREAD")
259   (setf sb!impl::*exit-lock* (make-mutex :name "Exit Lock")
260         *make-thread-lock* (make-mutex :name "Make-Thread Lock"))
261   (let ((initial-thread (%make-thread :name "main thread"
262                                       :%alive-p t
263                                       :os-thread (current-thread-os-thread))))
264     (setq *initial-thread* initial-thread
265           *current-thread* initial-thread)
266     (grab-mutex (thread-result-lock *initial-thread*))
267     ;; Either *all-threads* is empty or it contains exactly one thread
268     ;; in case we are in reinit since saving core with multiple
269     ;; threads doesn't work.
270     (setq *all-threads* (list initial-thread))))
271
272 (defun main-thread ()
273   "Returns the main thread of the process."
274   *initial-thread*)
275
276 (defun main-thread-p (&optional (thread *current-thread*))
277   "True if THREAD, defaulting to current thread, is the main thread of the process."
278   (eq thread *initial-thread*))
279
280 (defmacro return-from-thread (values-form &key allow-exit)
281   "Unwinds from and terminates the current thread, with values from
282 VALUES-FORM as the results visible to JOIN-THREAD.
283
284 If current thread is the main thread of the process (see
285 MAIN-THREAD-P), signals an error unless ALLOW-EXIT is true, as
286 terminating the main thread would terminate the entire process. If
287 ALLOW-EXIT is true, returning from the main thread is equivalent to
288 calling SB-EXT:EXIT with :CODE 0 and :ABORT NIL.
289
290 See also: ABORT-THREAD and SB-EXT:EXIT."
291   `(%return-from-thread (multiple-value-list ,values-form) ,allow-exit))
292
293 (defun %return-from-thread (values allow-exit)
294   (let ((self *current-thread*))
295     (cond ((main-thread-p self)
296            (unless allow-exit
297              (error 'simple-thread-error
298                     :format-control "~@<Tried to return ~S as values from main thread, ~
299                                      but exit was not allowed.~:@>"
300                     :format-arguments (list values)
301                     :thread self))
302            (sb!ext:exit :code 0))
303           (t
304            (throw '%return-from-thread (values-list values))))))
305
306 (defun abort-thread (&key allow-exit)
307   "Unwinds from and terminates the current thread abnormally, causing
308 JOIN-THREAD on current thread to signal an error unless a
309 default-value is provided.
310
311 If current thread is the main thread of the process (see
312 MAIN-THREAD-P), signals an error unless ALLOW-EXIT is true, as
313 terminating the main thread would terminate the entire process. If
314 ALLOW-EXIT is true, aborting the main thread is equivalent to calling
315 SB-EXT:EXIT code 1 and :ABORT NIL.
316
317 Invoking the initial ABORT restart estabilished by MAKE-THREAD is
318 equivalent to calling ABORT-THREAD in other than main threads.
319 However, whereas ABORT restart may be rebound, ABORT-THREAD always
320 unwinds the entire thread. (Behaviour of the initial ABORT restart for
321 main thread depends on the :TOPLEVEL argument to
322 SB-EXT:SAVE-LISP-AND-DIE.)
323
324 See also: RETURN-FROM-THREAD and SB-EXT:EXIT."
325   (let ((self *current-thread*))
326     (cond ((main-thread-p self)
327            (unless allow-exit
328              (error 'simple-thread-error
329                     :format-control "~@<Tried to abort initial thread, but ~
330                                      exit was not allowed.~:@>"))
331            (sb!ext:exit :code 1))
332           (t
333            ;; We /could/ use TOPLEVEL-CATCHER or %END-OF-THE-WORLD as well, but
334            ;; this seems tidier. Those to are a bit too overloaded already.
335            (throw '%abort-thread t)))))
336 \f
337
338 ;;;; Aliens, low level stuff
339
340 (define-alien-routine "kill_safely"
341     integer
342   (os-thread #!-alpha unsigned-long #!+alpha unsigned-int)
343   (signal int))
344
345 #!+sb-thread
346 (progn
347   ;; FIXME it would be good to define what a thread id is or isn't
348   ;; (our current assumption is that it's a fixnum).  It so happens
349   ;; that on Linux it's a pid, but it might not be on posix thread
350   ;; implementations.
351   (define-alien-routine ("create_thread" %create-thread)
352       unsigned-long (lisp-fun-address unsigned-long))
353
354   (declaim (inline %block-deferrable-signals))
355   (define-alien-routine ("block_deferrable_signals" %block-deferrable-signals)
356       void
357     (where sb!alien:unsigned-long)
358     (old sb!alien:unsigned-long))
359
360   (defun block-deferrable-signals ()
361     (%block-deferrable-signals 0 0))
362
363   #!+sb-futex
364   (progn
365     (declaim (inline futex-wait %futex-wait futex-wake))
366
367     (define-alien-routine ("futex_wait" %futex-wait)
368         int (word unsigned-long) (old-value unsigned-long)
369         (to-sec long) (to-usec unsigned-long))
370
371     (defun futex-wait (word old to-sec to-usec)
372       (with-interrupts
373         (%futex-wait word old to-sec to-usec)))
374
375     (define-alien-routine "futex_wake"
376         int (word unsigned-long) (n unsigned-long))))
377
378 ;;; used by debug-int.lisp to access interrupt contexts
379 #!-(or sb-fluid sb-thread) (declaim (inline sb!vm::current-thread-offset-sap))
380 #!-sb-thread
381 (defun sb!vm::current-thread-offset-sap (n)
382   (declare (type (unsigned-byte 27) n))
383   (sap-ref-sap (alien-sap (extern-alien "all_threads" (* t)))
384                (* n sb!vm:n-word-bytes)))
385
386 #!+sb-thread
387 (defun sb!vm::current-thread-offset-sap (n)
388   (declare (type (unsigned-byte 27) n))
389   (sb!vm::current-thread-offset-sap n))
390 \f
391
392 (defmacro with-deadlocks ((thread lock &optional (timeout nil timeoutp)) &body forms)
393   (with-unique-names (n-thread n-lock new n-timeout)
394     `(let* ((,n-thread ,thread)
395             (,n-lock ,lock)
396             (,n-timeout ,(when timeoutp
397                            `(or ,timeout
398                                 (when sb!impl::*deadline*
399                                   sb!impl::*deadline-seconds*))))
400             (,new (if ,n-timeout
401                       ;; Using CONS tells the rest of the system there's a
402                       ;; timeout in place, so it isn't considered a deadlock.
403                       (cons ,n-timeout ,n-lock)
404                       ,n-lock)))
405        (declare (dynamic-extent ,new))
406        ;; No WITHOUT-INTERRUPTS, since WITH-DEADLOCKS is used
407        ;; in places where interrupts should already be disabled.
408        (unwind-protect
409             (progn
410               (setf (thread-waiting-for ,n-thread) ,new)
411               (barrier (:write))
412               ,@forms)
413          ;; Interrupt handlers and GC save and restore any
414          ;; previous wait marks using WITHOUT-DEADLOCKS below.
415          (setf (thread-waiting-for ,n-thread) nil)
416          (barrier (:write))))))
417 \f
418 ;;;; Mutexes
419
420 #!+sb-doc
421 (setf (fdocumentation 'make-mutex 'function)
422       "Create a mutex."
423       (fdocumentation 'mutex-name 'function)
424       "The name of the mutex. Setfable.")
425
426 #!+(and sb-thread sb-futex)
427 (progn
428   (define-structure-slot-addressor mutex-state-address
429       :structure mutex
430       :slot state)
431   ;; Important: current code assumes these are fixnums or other
432   ;; lisp objects that don't need pinning.
433   (defconstant +lock-free+ 0)
434   (defconstant +lock-taken+ 1)
435   (defconstant +lock-contested+ 2))
436
437 (defun mutex-owner (mutex)
438   "Current owner of the mutex, NIL if the mutex is free. Naturally,
439 this is racy by design (another thread may acquire the mutex after
440 this function returns), it is intended for informative purposes. For
441 testing whether the current thread is holding a mutex see
442 HOLDING-MUTEX-P."
443   ;; Make sure to get the current value.
444   (sb!ext:compare-and-swap (mutex-%owner mutex) nil nil))
445
446 (sb!ext:defglobal **deadlock-lock** nil)
447
448 ;;; Signals an error if owner of LOCK is waiting on a lock whose release
449 ;;; depends on the current thread. Does not detect deadlocks from sempahores.
450 (defun check-deadlock ()
451   (let* ((self *current-thread*)
452          (origin (progn
453                    (barrier (:read))
454                    (thread-waiting-for self))))
455     (labels ((detect-deadlock (lock)
456                (let ((other-thread (mutex-%owner lock)))
457                  (cond ((not other-thread))
458                        ((eq self other-thread)
459                         (let ((chain
460                                 (with-cas-lock ((symbol-value '**deadlock-lock**))
461                                   (prog1 (deadlock-chain self origin)
462                                     ;; We're now committed to signaling the
463                                     ;; error and breaking the deadlock, so
464                                     ;; mark us as no longer waiting on the
465                                     ;; lock. This ensures that a single
466                                     ;; deadlock is reported in only one
467                                     ;; thread, and that we don't look like
468                                     ;; we're waiting on the lock when print
469                                     ;; stuff -- because that may lead to
470                                     ;; further deadlock checking, in turn
471                                     ;; possibly leading to a bogus vicious
472                                     ;; metacycle on PRINT-OBJECT.
473                                     (setf (thread-waiting-for self) nil)))))
474                           (error 'thread-deadlock
475                                  :thread *current-thread*
476                                  :cycle chain)))
477                        (t
478                         (let ((other-lock (progn
479                                             (barrier (:read))
480                                             (thread-waiting-for other-thread))))
481                           ;; If the thread is waiting with a timeout OTHER-LOCK
482                           ;; is a cons, and we don't consider it a deadlock -- since
483                           ;; it will time out on its own sooner or later.
484                           (when (mutex-p other-lock)
485                             (detect-deadlock other-lock)))))))
486              (deadlock-chain (thread lock)
487                (let* ((other-thread (mutex-owner lock))
488                       (other-lock (when other-thread
489                                     (barrier (:read))
490                                     (thread-waiting-for other-thread))))
491                  (cond ((not other-thread)
492                         ;; The deadlock is gone -- maybe someone unwound
493                         ;; from the same deadlock already?
494                         (return-from check-deadlock nil))
495                        ((consp other-lock)
496                         ;; There's a timeout -- no deadlock.
497                         (return-from check-deadlock nil))
498                        ((waitqueue-p other-lock)
499                         ;; Not a lock.
500                         (return-from check-deadlock nil))
501                        ((eq self other-thread)
502                         ;; Done
503                         (list (list thread lock)))
504                        (t
505                         (if other-lock
506                             (cons (cons thread lock)
507                                   (deadlock-chain other-thread other-lock))
508                             ;; Again, the deadlock is gone?
509                             (return-from check-deadlock nil)))))))
510       ;; Timeout means there is no deadlock
511       (when (mutex-p origin)
512         (detect-deadlock origin)
513         t))))
514
515 (defun %try-mutex (mutex new-owner)
516   (declare (type mutex mutex) (optimize (speed 3)))
517   (barrier (:read))
518   (let ((old (mutex-%owner mutex)))
519     (when (eq new-owner old)
520       (error "Recursive lock attempt ~S." mutex))
521     #!-sb-thread
522     (when old
523       (error "Strange deadlock on ~S in an unithreaded build?" mutex))
524     #!-(and sb-thread sb-futex)
525     (and (not old)
526          ;; Don't even bother to try to CAS if it looks bad.
527          (not (sb!ext:compare-and-swap (mutex-%owner mutex) nil new-owner)))
528     #!+(and sb-thread sb-futex)
529     ;; From the Mutex 2 algorithm from "Futexes are Tricky" by Ulrich Drepper.
530     (when (eql +lock-free+ (sb!ext:compare-and-swap (mutex-state mutex)
531                                                     +lock-free+
532                                                     +lock-taken+))
533       (let ((prev (sb!ext:compare-and-swap (mutex-%owner mutex) nil new-owner)))
534         (when prev
535           (bug "Old owner in free mutex: ~S" prev))
536         t))))
537
538 #!+sb-thread
539 (defun %%wait-for-mutex (mutex new-owner to-sec to-usec stop-sec stop-usec)
540   (declare (type mutex mutex) (optimize (speed 3)))
541   #!-sb-futex
542   (declare (ignore to-sec to-usec))
543   #!-sb-futex
544   (flet ((cas ()
545            (loop repeat 100
546                  when (and (progn
547                              (barrier (:read))
548                              (not (mutex-%owner mutex)))
549                            (not (sb!ext:compare-and-swap (mutex-%owner mutex) nil
550                                                          new-owner)))
551                  do (return-from cas t)
552                  else
553                  do
554                     (sb!ext:spin-loop-hint))
555            ;; Check for pending interrupts.
556            (with-interrupts nil)))
557     (declare (dynamic-extent #'cas))
558     (sb!impl::%%wait-for #'cas stop-sec stop-usec))
559   #!+sb-futex
560   ;; This is a fairly direct translation of the Mutex 2 algorithm from
561   ;; "Futexes are Tricky" by Ulrich Drepper.
562   (flet ((maybe (old)
563            (when (eql +lock-free+ old)
564              (let ((prev (sb!ext:compare-and-swap (mutex-%owner mutex)
565                                                   nil new-owner)))
566                (when prev
567                  (bug "Old owner in free mutex: ~S" prev))
568                (return-from %%wait-for-mutex t)))))
569     (prog ((old (sb!ext:compare-and-swap (mutex-state mutex)
570                                          +lock-free+ +lock-taken+)))
571        ;; Got it right off the bat?
572        (maybe old)
573      :retry
574        ;; Mark it as contested, and sleep. (Exception: it was just released.)
575        (when (or (eql +lock-contested+ old)
576                  (not (eql +lock-free+
577                            (sb!ext:compare-and-swap
578                             (mutex-state mutex) +lock-taken+ +lock-contested+))))
579          (when (eql 1 (with-pinned-objects (mutex)
580                         (futex-wait (mutex-state-address mutex)
581                                     (get-lisp-obj-address +lock-contested+)
582                                     (or to-sec -1)
583                                     (or to-usec 0))))
584            ;; -1 = EWOULDBLOCK, possibly spurious wakeup
585            ;;  0 = normal wakeup
586            ;;  1 = ETIMEDOUT ***DONE***
587            ;;  2 = EINTR, a spurious wakeup
588            (return-from %%wait-for-mutex nil)))
589        ;; Try to get it, still marking it as contested.
590        (maybe
591         (sb!ext:compare-and-swap (mutex-state mutex) +lock-free+ +lock-contested+))
592        ;; Update timeout if necessary.
593        (when stop-sec
594          (setf (values to-sec to-usec)
595                (sb!impl::relative-decoded-times stop-sec stop-usec)))
596        ;; Spin.
597        (go :retry))))
598
599 #!+sb-thread
600 (defun %wait-for-mutex (mutex self timeout to-sec to-usec stop-sec stop-usec deadlinep)
601   (with-deadlocks (self mutex timeout)
602     (with-interrupts (check-deadlock))
603     (tagbody
604      :again
605        (return-from %wait-for-mutex
606          (or (%%wait-for-mutex mutex self to-sec to-usec stop-sec stop-usec)
607              (when deadlinep
608                (signal-deadline)
609                ;; FIXME: substract elapsed time from timeout...
610                (setf (values to-sec to-usec stop-sec stop-usec deadlinep)
611                      (decode-timeout timeout))
612                (go :again)))))))
613
614 (defun get-mutex (mutex &optional new-owner (waitp t) (timeout nil))
615   #!+sb-doc
616   "Deprecated in favor of GRAB-MUTEX."
617   (declare (ignorable waitp timeout))
618   (let ((new-owner (or new-owner *current-thread*)))
619     (or (%try-mutex mutex new-owner)
620         #!+sb-thread
621         (when waitp
622           (multiple-value-call #'%wait-for-mutex
623             mutex new-owner timeout (decode-timeout timeout))))))
624
625 (defun grab-mutex (mutex &key (waitp t) (timeout nil))
626   #!+sb-doc
627   "Acquire MUTEX for the current thread. If WAITP is true (the default) and
628 the mutex is not immediately available, sleep until it is available.
629
630 If TIMEOUT is given, it specifies a relative timeout, in seconds, on how long
631 GRAB-MUTEX should try to acquire the lock in the contested case.
632
633 If GRAB-MUTEX returns T, the lock acquisition was successful. In case of WAITP
634 being NIL, or an expired TIMEOUT, GRAB-MUTEX may also return NIL which denotes
635 that GRAB-MUTEX did -not- acquire the lock.
636
637 Notes:
638
639   - GRAB-MUTEX is not interrupt safe. The correct way to call it is:
640
641       (WITHOUT-INTERRUPTS
642         ...
643         (ALLOW-WITH-INTERRUPTS (GRAB-MUTEX ...))
644         ...)
645
646     WITHOUT-INTERRUPTS is necessary to avoid an interrupt unwinding the call
647     while the mutex is in an inconsistent state while ALLOW-WITH-INTERRUPTS
648     allows the call to be interrupted from sleep.
649
650   - (GRAB-MUTEX <mutex> :timeout 0.0) differs from
651     (GRAB-MUTEX <mutex> :waitp nil) in that the former may signal a
652     DEADLINE-TIMEOUT if the global deadline was due already on entering
653     GRAB-MUTEX.
654
655     The exact interplay of GRAB-MUTEX and deadlines are reserved to change in
656     future versions.
657
658   - It is recommended that you use WITH-MUTEX instead of calling GRAB-MUTEX
659     directly.
660 "
661   (declare (ignorable waitp timeout))
662   (let ((self *current-thread*))
663     (or (%try-mutex mutex self)
664         #!+sb-thread
665         (when waitp
666           (multiple-value-call #'%wait-for-mutex
667             mutex self timeout (decode-timeout timeout))))))
668
669 (defun release-mutex (mutex &key (if-not-owner :punt))
670   #!+sb-doc
671   "Release MUTEX by setting it to NIL. Wake up threads waiting for
672 this mutex.
673
674 RELEASE-MUTEX is not interrupt safe: interrupts should be disabled
675 around calls to it.
676
677 If the current thread is not the owner of the mutex then it silently
678 returns without doing anything (if IF-NOT-OWNER is :PUNT), signals a
679 WARNING (if IF-NOT-OWNER is :WARN), or releases the mutex anyway (if
680 IF-NOT-OWNER is :FORCE)."
681   (declare (type mutex mutex))
682   ;; Order matters: set owner to NIL before releasing state.
683   (let* ((self *current-thread*)
684          (old-owner (sb!ext:compare-and-swap (mutex-%owner mutex) self nil)))
685     (unless (eq self old-owner)
686       (ecase if-not-owner
687         ((:punt) (return-from release-mutex nil))
688         ((:warn)
689          (warn "Releasing ~S, owned by another thread: ~S" mutex old-owner))
690         ((:force)))
691       (setf (mutex-%owner mutex) nil)
692       ;; FIXME: Is a :memory barrier too strong here?  Can we use a :write
693       ;; barrier instead?
694       (barrier (:memory)))
695     #!+(and sb-thread sb-futex)
696     (when old-owner
697       ;; FIXME: once ATOMIC-INCF supports struct slots with word sized
698       ;; unsigned-byte type this can be used:
699       ;;
700       ;;     (let ((old (sb!ext:atomic-incf (mutex-state mutex) -1)))
701       ;;       (unless (eql old +lock-free+)
702       ;;         (setf (mutex-state mutex) +lock-free+)
703       ;;         (with-pinned-objects (mutex)
704       ;;           (futex-wake (mutex-state-address mutex) 1))))
705       (let ((old (sb!ext:compare-and-swap (mutex-state mutex)
706                                           +lock-taken+ +lock-free+)))
707         (when (eql old +lock-contested+)
708           (sb!ext:compare-and-swap (mutex-state mutex)
709                                    +lock-contested+ +lock-free+)
710           (with-pinned-objects (mutex)
711             (futex-wake (mutex-state-address mutex) 1))))
712       nil)))
713 \f
714
715 ;;;; Waitqueues/condition variables
716
717 #!+(or (not sb-thread) sb-futex)
718 (defstruct (waitqueue (:constructor %make-waitqueue))
719   #!+sb-doc
720   "Waitqueue type."
721   (name nil :type (or null thread-name))
722   #!+(and sb-thread sb-futex)
723   (token nil))
724
725 #!+(and sb-thread (not sb-futex))
726 (progn
727   (defstruct (waitqueue (:constructor %make-waitqueue))
728     #!+sb-doc
729     "Waitqueue type."
730     (name nil :type (or null thread-name))
731     ;; For WITH-CAS-LOCK: because CONDITION-WAIT must be able to call
732     ;; %WAITQUEUE-WAKEUP without re-aquiring the mutex, we need a separate
733     ;; lock. In most cases this should be uncontested thanks to the mutex --
734     ;; the only case where that might not be true is when CONDITION-WAIT
735     ;; unwinds and %WAITQUEUE-DROP is called.
736     %owner
737     %head
738     %tail)
739
740   (defun %waitqueue-enqueue (thread queue)
741     (setf (thread-waiting-for thread) queue)
742     (let ((head (waitqueue-%head queue))
743           (tail (waitqueue-%tail queue))
744           (new (list thread)))
745       (unless head
746         (setf (waitqueue-%head queue) new))
747       (when tail
748         (setf (cdr tail) new))
749       (setf (waitqueue-%tail queue) new)
750       nil))
751   (defun %waitqueue-drop (thread queue)
752     (setf (thread-waiting-for thread) nil)
753     (let ((head (waitqueue-%head queue)))
754       (do ((list head (cdr list))
755            (prev nil list))
756           ((or (null list)
757                (eq (car list) thread))
758            (when list
759              (let ((rest (cdr list)))
760                (cond (prev
761                       (setf (cdr prev) rest))
762                      (t
763                       (setf (waitqueue-%head queue) rest
764                             prev rest)))
765                (unless rest
766                  (setf (waitqueue-%tail queue) prev)))))))
767     nil)
768   (defun %waitqueue-wakeup (queue n)
769     (declare (fixnum n))
770     (loop while (plusp n)
771           for next = (let ((head (waitqueue-%head queue))
772                            (tail (waitqueue-%tail queue)))
773                        (when head
774                          (if (eq head tail)
775                              (setf (waitqueue-%head queue) nil
776                                    (waitqueue-%tail queue) nil)
777                              (setf (waitqueue-%head queue) (cdr head)))
778                          (car head)))
779           while next
780           do (when (eq queue (sb!ext:compare-and-swap
781                               (thread-waiting-for next) queue nil))
782                (decf n)))
783     nil))
784
785 (def!method print-object ((waitqueue waitqueue) stream)
786   (print-unreadable-object (waitqueue stream :type t :identity t)
787     (format stream "~@[~A~]" (waitqueue-name waitqueue))))
788
789 (defun make-waitqueue (&key name)
790   #!+sb-doc
791   "Create a waitqueue."
792   (%make-waitqueue :name name))
793
794 #!+sb-doc
795 (setf (fdocumentation 'waitqueue-name 'function)
796       "The name of the waitqueue. Setfable.")
797
798 #!+(and sb-thread sb-futex)
799 (define-structure-slot-addressor waitqueue-token-address
800     :structure waitqueue
801     :slot token)
802
803 (defun condition-wait (queue mutex &key timeout)
804   #!+sb-doc
805   "Atomically release MUTEX and start waiting on QUEUE for till another thread
806 wakes us up using either CONDITION-NOTIFY or CONDITION-BROADCAST on that
807 queue, at which point we re-acquire MUTEX and return T.
808
809 Spurious wakeups are possible.
810
811 If TIMEOUT is given, it is the maximum number of seconds to wait, including
812 both waiting for the wakeup and the time to re-acquire MUTEX. Unless both
813 wakeup and re-acquisition do not occur within the given time, returns NIL
814 without re-acquiring the mutex.
815
816 If CONDITION-WAIT unwinds, it may do so with or without the mutex being held.
817
818 Important: Since CONDITION-WAIT may return without CONDITION-NOTIFY having
819 occurred the correct way to write code that uses CONDITION-WAIT is to loop
820 around the call, checking the the associated data:
821
822   (defvar *data* nil)
823   (defvar *queue* (make-waitqueue))
824   (defvar *lock* (make-mutex))
825
826   ;; Consumer
827   (defun pop-data (&optional timeout)
828     (with-mutex (*lock*)
829       (loop until *data*
830             do (or (condition-wait *queue* *lock* :timeout timeout)
831                    ;; Lock not held, must unwind without touching *data*.
832                    (return-from pop-data nil)))
833       (pop *data*)))
834
835   ;; Producer
836   (defun push-data (data)
837     (with-mutex (*lock*)
838       (push data *data*)
839       (condition-notify *queue*)))
840 "
841   #!-sb-thread
842   (declare (ignore queue))
843   (assert mutex)
844   #!-sb-thread
845   (sb!ext:wait-for nil :timeout timeout) ; Yeah...
846   #!+sb-thread
847   (let ((me *current-thread*))
848     (barrier (:read))
849     (assert (eq me (mutex-%owner mutex)))
850     (multiple-value-bind (to-sec to-usec stop-sec stop-usec deadlinep)
851         (decode-timeout timeout)
852       (let ((status :interrupted))
853         ;; Need to disable interrupts so that we don't miss grabbing the
854         ;; mutex on our way out.
855         (without-interrupts
856           (unwind-protect
857                (progn
858                  #!-sb-futex
859                  (progn
860                    (%with-cas-lock ((waitqueue-%owner queue))
861                      (%waitqueue-enqueue me queue))
862                    (release-mutex mutex)
863                    (setf status
864                          (or (flet ((wakeup ()
865                                       (barrier (:read))
866                                       (unless (eq queue (thread-waiting-for me))
867                                         :ok)))
868                                (declare (dynamic-extent #'wakeup))
869                                (allow-with-interrupts
870                                  (sb!impl::%%wait-for #'wakeup stop-sec stop-usec)))
871                              :timeout)))
872                  #!+sb-futex
873                  (with-pinned-objects (queue me)
874                    (setf (waitqueue-token queue) me)
875                    (release-mutex mutex)
876                    ;; Now we go to sleep using futex-wait. If anyone else
877                    ;; manages to grab MUTEX and call CONDITION-NOTIFY during
878                    ;; this comment, it will change the token, and so futex-wait
879                    ;; returns immediately instead of sleeping. Ergo, no lost
880                    ;; wakeup. We may get spurious wakeups, but that's ok.
881                    (setf status
882                          (case (allow-with-interrupts
883                                  (futex-wait (waitqueue-token-address queue)
884                                              (get-lisp-obj-address me)
885                                              ;; our way of saying "no
886                                              ;; timeout":
887                                              (or to-sec -1)
888                                              (or to-usec 0)))
889                            ((1)
890                             ;;  1 = ETIMEDOUT
891                             :timeout)
892                            (t
893                             ;; -1 = EWOULDBLOCK, possibly spurious wakeup
894                             ;;  0 = normal wakeup
895                             ;;  2 = EINTR, a spurious wakeup
896                             :ok)))))
897             #!-sb-futex
898             (%with-cas-lock ((waitqueue-%owner queue))
899               (if (eq queue (thread-waiting-for me))
900                   (%waitqueue-drop me queue)
901                   (unless (eq :ok status)
902                     ;; CONDITION-NOTIFY thinks we've been woken up, but really
903                     ;; we're unwinding. Wake someone else up.
904                     (%waitqueue-wakeup queue 1))))
905             ;; Update timeout for mutex re-aquisition.
906             (when (and (eq :ok status) to-sec)
907               (setf (values to-sec to-usec)
908                     (sb!impl::relative-decoded-times stop-sec stop-usec)))
909             ;; If we ran into deadline, try to get the mutex before
910             ;; signaling. If we don't unwind it will look like a normal
911             ;; return from user perspective.
912             (when (and (eq :timeout status) deadlinep)
913               (let ((got-it (%try-mutex mutex me)))
914                 (allow-with-interrupts
915                   (signal-deadline)
916                   (cond (got-it
917                          (return-from condition-wait t))
918                         (t
919                          ;; The deadline may have changed.
920                          (setf (values to-sec to-usec stop-sec stop-usec deadlinep)
921                                (decode-timeout timeout))
922                          (setf status :ok))))))
923             ;; Re-acquire the mutex for normal return.
924             (when (eq :ok status)
925               (unless (or (%try-mutex mutex me)
926                           (allow-with-interrupts
927                             (%wait-for-mutex mutex me timeout
928                                              to-sec to-usec
929                                              stop-sec stop-usec deadlinep)))
930                 (setf status :timeout)))))
931         (or (eq :ok status)
932             (unless (eq :timeout status)
933               ;; The only case we return normally without re-acquiring the
934               ;; mutex is when there is a :TIMEOUT that runs out.
935               (bug "CONDITION-WAIT: invalid status on normal return: ~S" status)))))))
936
937 (defun condition-notify (queue &optional (n 1))
938   #!+sb-doc
939   "Notify N threads waiting on QUEUE.
940
941 IMPORTANT: The same mutex that is used in the corresponding CONDITION-WAIT
942 must be held by this thread during this call."
943   #!-sb-thread
944   (declare (ignore queue n))
945   #!-sb-thread
946   (error "Not supported in unithread builds.")
947   #!+sb-thread
948   (declare (type (and fixnum (integer 1)) n))
949   (/show0 "Entering CONDITION-NOTIFY")
950   #!+sb-thread
951   (progn
952     #!-sb-futex
953     (with-cas-lock ((waitqueue-%owner queue))
954       (%waitqueue-wakeup queue n))
955     #!+sb-futex
956     (progn
957     ;; No problem if >1 thread notifies during the comment in condition-wait:
958     ;; as long as the value in queue-data isn't the waiting thread's id, it
959     ;; matters not what it is -- using the queue object itself is handy.
960     ;;
961     ;; XXX we should do something to ensure that the result of this setf
962     ;; is visible to all CPUs.
963     ;;
964     ;; ^-- surely futex_wake() involves a memory barrier?
965       (setf (waitqueue-token queue) queue)
966       (with-pinned-objects (queue)
967         (futex-wake (waitqueue-token-address queue) n)))))
968
969 (defun condition-broadcast (queue)
970   #!+sb-doc
971   "Notify all threads waiting on QUEUE.
972
973 IMPORTANT: The same mutex that is used in the corresponding CONDITION-WAIT
974 must be held by this thread during this call."
975   (condition-notify queue
976                     ;; On a 64-bit platform truncating M-P-F to an int
977                     ;; results in -1, which wakes up only one thread.
978                     (ldb (byte 29 0)
979                          most-positive-fixnum)))
980 \f
981
982 ;;;; Semaphores
983
984 (defstruct (semaphore (:constructor %make-semaphore (name %count)))
985   #!+sb-doc
986   "Semaphore type. The fact that a SEMAPHORE is a STRUCTURE-OBJECT
987 should be considered an implementation detail, and may change in the
988 future."
989   (name    nil :type (or null thread-name))
990   (%count    0 :type (integer 0))
991   (waitcount 0 :type sb!vm:word)
992   (mutex (make-mutex))
993   (queue (make-waitqueue)))
994
995 (setf (fdocumentation 'semaphore-name 'function)
996       "The name of the semaphore INSTANCE. Setfable.")
997
998 (defstruct (semaphore-notification (:constructor make-semaphore-notification ())
999                                    (:copier nil))
1000   #!+sb-doc
1001   "Semaphore notification object. Can be passed to WAIT-ON-SEMAPHORE and
1002 TRY-SEMAPHORE as the :NOTIFICATION argument. Consequences are undefined if
1003 multiple threads are using the same notification object in parallel."
1004   (%status nil :type boolean))
1005
1006 (setf (fdocumentation 'make-semaphore-notification 'function)
1007       "Constructor for SEMAPHORE-NOTIFICATION objects. SEMAPHORE-NOTIFICATION-STATUS
1008 is initially NIL.")
1009
1010 (declaim (inline semaphore-notification-status))
1011 (defun semaphore-notification-status (semaphore-notification)
1012   #!+sb-doc
1013   "Returns T if a WAIT-ON-SEMAPHORE or TRY-SEMAPHORE using
1014 SEMAPHORE-NOTICATION has succeeded since the notification object was created
1015 or cleared."
1016   (barrier (:read))
1017   (semaphore-notification-%status semaphore-notification))
1018
1019 (declaim (inline clear-semaphore-notification))
1020 (defun clear-semaphore-notification (semaphore-notification)
1021   #!+sb-doc
1022   "Resets the SEMAPHORE-NOTIFICATION object for use with another call to
1023 WAIT-ON-SEMAPHORE or TRY-SEMAPHORE."
1024   (barrier (:write)
1025     (setf (semaphore-notification-%status semaphore-notification) nil)))
1026
1027 (declaim (inline semaphore-count))
1028 (defun semaphore-count (instance)
1029   #!+sb-doc
1030   "Returns the current count of the semaphore INSTANCE."
1031   (barrier (:read))
1032   (semaphore-%count instance))
1033
1034 (defun make-semaphore (&key name (count 0))
1035   #!+sb-doc
1036   "Create a semaphore with the supplied COUNT and NAME."
1037   (%make-semaphore name count))
1038
1039 (defun wait-on-semaphore (semaphore &key timeout notification)
1040   #!+sb-doc
1041   "Decrement the count of SEMAPHORE if the count would not be negative. Else
1042 blocks until the semaphore can be decremented. Returns T on success.
1043
1044 If TIMEOUT is given, it is the maximum number of seconds to wait. If the count
1045 cannot be decremented in that time, returns NIL without decrementing the
1046 count.
1047
1048 If NOTIFICATION is given, it must be a SEMAPHORE-NOTIFICATION object whose
1049 SEMAPHORE-NOTIFICATION-STATUS is NIL. If WAIT-ON-SEMAPHORE succeeds and
1050 decrements the count, the status is set to T."
1051   (when (and notification (semaphore-notification-status notification))
1052     (with-simple-restart (continue "Clear notification status and continue.")
1053       (error "~@<Semaphore notification object status not cleared on entry to ~S on ~S.~:@>"
1054              'wait-on-semaphore semaphore))
1055     (clear-semaphore-notification notification))
1056   ;; A more direct implementation based directly on futexes should be
1057   ;; possible.
1058   ;;
1059   ;; We need to disable interrupts so that we don't forget to
1060   ;; decrement the waitcount (which would happen if an asynch
1061   ;; interrupt should catch us on our way out from the loop.)
1062   ;;
1063   ;; FIXME: No timeout on initial mutex acquisition.
1064   (with-system-mutex ((semaphore-mutex semaphore) :allow-with-interrupts t)
1065     ;; Quick check: is it positive? If not, enter the wait loop.
1066     (let ((count (semaphore-%count semaphore)))
1067       (cond ((plusp count)
1068              (setf (semaphore-%count semaphore) (1- count))
1069              (when notification
1070                (setf (semaphore-notification-%status notification) t)))
1071             (t
1072              (unwind-protect
1073                   (progn
1074                     ;; Need to use ATOMIC-INCF despite the lock, because on our
1075                     ;; way out from here we might not be locked anymore -- so
1076                     ;; another thread might be tweaking this in parallel using
1077                     ;; ATOMIC-DECF. No danger over overflow, since there it
1078                     ;; at most one increment per thread waiting on the semaphore.
1079                     (sb!ext:atomic-incf (semaphore-waitcount semaphore))
1080                     (loop until (plusp (setf count (semaphore-%count semaphore)))
1081                           do (or (condition-wait (semaphore-queue semaphore)
1082                                                  (semaphore-mutex semaphore)
1083                                                  :timeout timeout)
1084                                  (return-from wait-on-semaphore nil)))
1085                     (setf (semaphore-%count semaphore) (1- count))
1086                     (when notification
1087                       (setf (semaphore-notification-%status notification) t)))
1088                ;; Need to use ATOMIC-DECF as we may unwind without the lock
1089                ;; being held!
1090                (sb!ext:atomic-decf (semaphore-waitcount semaphore)))))))
1091   t)
1092
1093 (defun try-semaphore (semaphore &optional (n 1) notification)
1094   #!+sb-doc
1095   "Try to decrement the count of SEMAPHORE by N. If the count were to
1096 become negative, punt and return NIL, otherwise return true.
1097
1098 If NOTIFICATION is given it must be a semaphore notification object
1099 with SEMAPHORE-NOTIFICATION-STATUS of NIL. If the count is decremented,
1100 the status is set to T."
1101   (declare (type (integer 1) n))
1102   (when (and notification (semaphore-notification-status notification))
1103     (with-simple-restart (continue "Clear notification status and continue.")
1104       (error "~@<Semaphore notification object status not cleared on entry to ~S on ~S.~:@>"
1105              'try-semaphore semaphore))
1106     (clear-semaphore-notification notification))
1107   (with-system-mutex ((semaphore-mutex semaphore) :allow-with-interrupts t)
1108     (let ((new-count (- (semaphore-%count semaphore) n)))
1109       (when (not (minusp new-count))
1110         (setf (semaphore-%count semaphore) new-count)
1111         (when notification
1112           (setf (semaphore-notification-%status notification) t))
1113         ;; FIXME: We don't actually document this -- should we just
1114         ;; return T, or document new count as the return?
1115         new-count))))
1116
1117 (defun signal-semaphore (semaphore &optional (n 1))
1118   #!+sb-doc
1119   "Increment the count of SEMAPHORE by N. If there are threads waiting
1120 on this semaphore, then N of them is woken up."
1121   (declare (type (integer 1) n))
1122   ;; Need to disable interrupts so that we don't lose a wakeup after
1123   ;; we have incremented the count.
1124   (with-system-mutex ((semaphore-mutex semaphore) :allow-with-interrupts t)
1125     (let ((waitcount (semaphore-waitcount semaphore))
1126           (count (incf (semaphore-%count semaphore) n)))
1127       (when (plusp waitcount)
1128         (condition-notify (semaphore-queue semaphore) (min waitcount count))))))
1129 \f
1130
1131 ;;;; Job control, independent listeners
1132
1133 (defstruct session
1134   (lock (make-mutex :name "session lock"))
1135   (threads nil)
1136   (interactive-threads nil)
1137   (interactive-threads-queue (make-waitqueue)))
1138
1139 (defvar *session* nil)
1140
1141 ;;; The debugger itself tries to acquire the session lock, don't let
1142 ;;; funny situations (like getting a sigint while holding the session
1143 ;;; lock) occur. At the same time we need to allow interrupts while
1144 ;;; *waiting* for the session lock for things like GET-FOREGROUND to
1145 ;;; be interruptible.
1146 ;;;
1147 ;;; Take care: we sometimes need to obtain the session lock while
1148 ;;; holding on to *ALL-THREADS-LOCK*, so we must _never_ obtain it
1149 ;;; _after_ getting a session lock! (Deadlock risk.)
1150 ;;;
1151 ;;; FIXME: It would be good to have ordered locks to ensure invariants
1152 ;;; like the above.
1153 (defmacro with-session-lock ((session) &body body)
1154   `(with-system-mutex ((session-lock ,session) :allow-with-interrupts t)
1155      ,@body))
1156
1157 (defun new-session ()
1158   (make-session :threads (list *current-thread*)
1159                 :interactive-threads (list *current-thread*)))
1160
1161 (defun init-job-control ()
1162   (/show0 "Entering INIT-JOB-CONTROL")
1163   (setf *session* (new-session))
1164   (/show0 "Exiting INIT-JOB-CONTROL"))
1165
1166 (defun %delete-thread-from-session (thread session)
1167   (with-session-lock (session)
1168     (setf (session-threads session)
1169           (delete thread (session-threads session))
1170           (session-interactive-threads session)
1171           (delete thread (session-interactive-threads session)))))
1172
1173 (defun call-with-new-session (fn)
1174   (%delete-thread-from-session *current-thread* *session*)
1175   (let ((*session* (new-session)))
1176     (funcall fn)))
1177
1178 (defmacro with-new-session (args &body forms)
1179   (declare (ignore args))               ;for extensibility
1180   (sb!int:with-unique-names (fb-name)
1181     `(labels ((,fb-name () ,@forms))
1182       (call-with-new-session (function ,fb-name)))))
1183
1184 ;;; Remove thread from its session, if it has one.
1185 #!+sb-thread
1186 (defun handle-thread-exit (thread)
1187   (/show0 "HANDLING THREAD EXIT")
1188   (when *exit-in-process*
1189     (%exit))
1190   ;; Lisp-side cleanup
1191   (with-all-threads-lock
1192     (setf (thread-%alive-p thread) nil)
1193     (setf (thread-os-thread thread) nil)
1194     (setq *all-threads* (delete thread *all-threads*))
1195     (when *session*
1196       (%delete-thread-from-session thread *session*))))
1197
1198 (defun %exit-other-threads ()
1199   ;; Grabbing this lock prevents new threads from
1200   ;; being spawned, and guarantees that *ALL-THREADS*
1201   ;; is up to date.
1202   (with-deadline (:seconds nil :override t)
1203     (grab-mutex *make-thread-lock*)
1204     (let ((timeout sb!ext:*exit-timeout*)
1205           (code *exit-in-process*)
1206           (current *current-thread*)
1207           (joinees nil)
1208           (main nil))
1209       (dolist (thread (list-all-threads))
1210         (cond ((eq thread current))
1211               ((main-thread-p thread)
1212                (setf main thread))
1213               (t
1214                (handler-case
1215                    (progn
1216                      (terminate-thread thread)
1217                      (push thread joinees))
1218                  (interrupt-thread-error ())))))
1219       (with-progressive-timeout (time-left :seconds timeout)
1220         (dolist (thread joinees)
1221           (join-thread thread :default t :timeout (time-left)))
1222         ;; Need to defer till others have joined, because when main
1223         ;; thread exits, we're gone. Can't use TERMINATE-THREAD -- would
1224         ;; get the exit code wrong.
1225         (when main
1226           (handler-case
1227               (interrupt-thread
1228                main
1229                (lambda ()
1230                  (setf *exit-in-process* (list code))
1231                  (throw 'sb!impl::%end-of-the-world t)))
1232             (interrupt-thread-error ()))
1233           ;; Normally this never finishes, as once the main-thread unwinds we
1234           ;; exit with the right code, but if times out before that happens,
1235           ;; we will exit after returning -- or rathe racing the main thread
1236           ;; to calling OS-EXIT.
1237           (join-thread main :default t :timeout (time-left)))))))
1238
1239 (defun terminate-session ()
1240   #!+sb-doc
1241   "Kill all threads in session except for this one.  Does nothing if current
1242 thread is not the foreground thread."
1243   ;; FIXME: threads created in other threads may escape termination
1244   (let ((to-kill
1245          (with-session-lock (*session*)
1246            (and (eq *current-thread*
1247                     (car (session-interactive-threads *session*)))
1248                 (session-threads *session*)))))
1249     ;; do the kill after dropping the mutex; unwind forms in dying
1250     ;; threads may want to do session things
1251     (dolist (thread to-kill)
1252       (unless (eq thread *current-thread*)
1253         ;; terminate the thread but don't be surprised if it has
1254         ;; exited in the meantime
1255         (handler-case (terminate-thread thread)
1256           (interrupt-thread-error ()))))))
1257
1258 ;;; called from top of invoke-debugger
1259 (defun debugger-wait-until-foreground-thread (stream)
1260   "Returns T if thread had been running in background, NIL if it was
1261 interactive."
1262   (declare (ignore stream))
1263   #!-sb-thread nil
1264   #!+sb-thread
1265   (prog1
1266       (with-session-lock (*session*)
1267         (not (member *current-thread*
1268                      (session-interactive-threads *session*))))
1269     (get-foreground)))
1270
1271 (defun get-foreground ()
1272   #!-sb-thread t
1273   #!+sb-thread
1274   (let ((was-foreground t))
1275     (loop
1276      (/show0 "Looping in GET-FOREGROUND")
1277      (with-session-lock (*session*)
1278        (let ((int-t (session-interactive-threads *session*)))
1279          (when (eq (car int-t) *current-thread*)
1280            (unless was-foreground
1281              (format *query-io* "Resuming thread ~A~%" *current-thread*))
1282            (return-from get-foreground t))
1283          (setf was-foreground nil)
1284          (unless (member *current-thread* int-t)
1285            (setf (cdr (last int-t))
1286                  (list *current-thread*)))
1287          (condition-wait
1288           (session-interactive-threads-queue *session*)
1289           (session-lock *session*)))))))
1290
1291 (defun release-foreground (&optional next)
1292   #!+sb-doc
1293   "Background this thread.  If NEXT is supplied, arrange for it to
1294 have the foreground next."
1295   #!-sb-thread (declare (ignore next))
1296   #!-sb-thread nil
1297   #!+sb-thread
1298   (with-session-lock (*session*)
1299     (when (rest (session-interactive-threads *session*))
1300       (setf (session-interactive-threads *session*)
1301             (delete *current-thread* (session-interactive-threads *session*))))
1302     (when next
1303       (setf (session-interactive-threads *session*)
1304             (list* next
1305                    (delete next (session-interactive-threads *session*)))))
1306     (condition-broadcast (session-interactive-threads-queue *session*))))
1307
1308 (defun foreground-thread ()
1309   (car (session-interactive-threads *session*)))
1310
1311 (defun make-listener-thread (tty-name)
1312   (assert (probe-file tty-name))
1313   (let* ((in (sb!unix:unix-open (namestring tty-name) sb!unix:o_rdwr #o666))
1314          (out (sb!unix:unix-dup in))
1315          (err (sb!unix:unix-dup in)))
1316     (labels ((thread-repl ()
1317                (sb!unix::unix-setsid)
1318                (let* ((sb!impl::*stdin*
1319                        (make-fd-stream in :input t :buffering :line
1320                                        :dual-channel-p t))
1321                       (sb!impl::*stdout*
1322                        (make-fd-stream out :output t :buffering :line
1323                                               :dual-channel-p t))
1324                       (sb!impl::*stderr*
1325                        (make-fd-stream err :output t :buffering :line
1326                                               :dual-channel-p t))
1327                       (sb!impl::*tty*
1328                        (make-fd-stream err :input t :output t
1329                                               :buffering :line
1330                                               :dual-channel-p t))
1331                       (sb!impl::*descriptor-handlers* nil))
1332                  (with-new-session ()
1333                    (unwind-protect
1334                         (sb!impl::toplevel-repl nil)
1335                      (sb!int:flush-standard-output-streams))))))
1336       (make-thread #'thread-repl))))
1337 \f
1338
1339 ;;;; The beef
1340
1341 (defun make-thread (function &key name arguments)
1342   #!+sb-doc
1343   "Create a new thread of NAME that runs FUNCTION with the argument
1344 list designator provided (defaults to no argument). Thread exits when
1345 the function returns. The return values of FUNCTION are kept around
1346 and can be retrieved by JOIN-THREAD.
1347
1348 Invoking the initial ABORT restart estabilished by MAKE-THREAD
1349 terminates the thread.
1350
1351 See also: RETURN-FROM-THREAD, ABORT-THREAD."
1352   #!-sb-thread (declare (ignore function name arguments))
1353   #!-sb-thread (error "Not supported in unithread builds.")
1354   #!+sb-thread (assert (or (atom arguments)
1355                            (null (cdr (last arguments))))
1356                        (arguments)
1357                        "Argument passed to ~S, ~S, is an improper list."
1358                        'make-thread arguments)
1359   #!+sb-thread
1360   (tagbody
1361      (with-mutex (*make-thread-lock*)
1362        (let* ((thread (%make-thread :name name))
1363               (setup-sem (make-semaphore :name "Thread setup semaphore"))
1364               (real-function (coerce function 'function))
1365               (arguments     (if (listp arguments)
1366                                  arguments
1367                                  (list arguments)))
1368               (initial-function
1369                 (named-lambda initial-thread-function ()
1370                   ;; In time we'll move some of the binding presently done in C
1371                   ;; here too.
1372                   ;;
1373                   ;; KLUDGE: Here we have a magic list of variables that are
1374                   ;; not thread-safe for one reason or another.  As people
1375                   ;; report problems with the thread safety of certain
1376                   ;; variables, (e.g. "*print-case* in multiple threads
1377                   ;; broken", sbcl-devel 2006-07-14), we add a few more
1378                   ;; bindings here.  The Right Thing is probably some variant
1379                   ;; of Allegro's *cl-default-special-bindings*, as that is at
1380                   ;; least accessible to users to secure their own libraries.
1381                   ;;   --njf, 2006-07-15
1382                   ;;
1383                   ;; As it is, this lambda must not cons until we are ready
1384                   ;; to run GC. Be very careful.
1385                   (let* ((*current-thread* thread)
1386                          (*restart-clusters* nil)
1387                          (*handler-clusters* (sb!kernel::initial-handler-clusters))
1388                          (*condition-restarts* nil)
1389                          (*exit-in-process* nil)
1390                          (sb!impl::*deadline* nil)
1391                          (sb!impl::*deadline-seconds* nil)
1392                          (sb!impl::*step-out* nil)
1393                          ;; internal printer variables
1394                          (sb!impl::*previous-case* nil)
1395                          (sb!impl::*previous-readtable-case* nil)
1396                          (sb!impl::*internal-symbol-output-fun* nil)
1397                          (sb!impl::*descriptor-handlers* nil)) ; serve-event
1398                     ;; Binding from C
1399                     (setf sb!vm:*alloc-signal* *default-alloc-signal*)
1400                     (setf (thread-os-thread thread) (current-thread-os-thread))
1401                     (with-mutex ((thread-result-lock thread))
1402                       (with-all-threads-lock
1403                         (push thread *all-threads*))
1404                       (with-session-lock (*session*)
1405                         (push thread (session-threads *session*)))
1406                       (setf (thread-%alive-p thread) t)
1407                       (signal-semaphore setup-sem)
1408                       ;; Using handling-end-of-the-world would be a bit tricky
1409                       ;; due to other catches and interrupts, so we essentially
1410                       ;; re-implement it here. Once and only once more.
1411                       (catch 'sb!impl::toplevel-catcher
1412                         (catch 'sb!impl::%end-of-the-world
1413                           (catch '%abort-thread
1414                             (with-simple-restart
1415                                 (abort "~@<Abort thread (~A)~@:>" *current-thread*)
1416                               (without-interrupts
1417                                 (unwind-protect
1418                                      (with-local-interrupts
1419                                        (sb!unix::unblock-deferrable-signals)
1420                                        (setf (thread-result thread)
1421                                              (prog1
1422                                                  (cons t
1423                                                        (multiple-value-list
1424                                                         (unwind-protect
1425                                                              (catch '%return-from-thread
1426                                                                (apply real-function arguments))
1427                                                           (when *exit-in-process*
1428                                                             (sb!impl::call-exit-hooks)))))
1429                                                #!+sb-safepoint
1430                                                (sb!kernel::gc-safepoint))))
1431                                   ;; We're going down, can't handle interrupts
1432                                   ;; sanely anymore. GC remains enabled.
1433                                   (block-deferrable-signals)
1434                                   ;; We don't want to run interrupts in a dead
1435                                   ;; thread when we leave WITHOUT-INTERRUPTS.
1436                                   ;; This potentially causes important
1437                                   ;; interupts to be lost: SIGINT comes to
1438                                   ;; mind.
1439                                   (setq *interrupt-pending* nil)
1440                                   (handle-thread-exit thread)))))))))
1441                   (values))))
1442          ;; If the starting thread is stopped for gc before it signals the
1443          ;; semaphore then we'd be stuck.
1444          (assert (not *gc-inhibit*))
1445          ;; Keep INITIAL-FUNCTION pinned until the child thread is
1446          ;; initialized properly. Wrap the whole thing in
1447          ;; WITHOUT-INTERRUPTS because we pass INITIAL-FUNCTION to another
1448          ;; thread.
1449          (without-interrupts
1450            (with-pinned-objects (initial-function)
1451              (let ((os-thread
1452                      (%create-thread
1453                       (get-lisp-obj-address initial-function))))
1454                (when (zerop os-thread)
1455                  (go :cant-spawn))
1456                (wait-on-semaphore setup-sem)
1457                (return-from make-thread thread))))))
1458    :cant-spawn
1459      (error "Could not create a new thread.")))
1460
1461 (defun join-thread (thread &key (default nil defaultp) timeout)
1462   #!+sb-doc
1463   "Suspend current thread until THREAD exits. Return the result values
1464 of the thread function.
1465
1466 If the thread does not exit normally within TIMEOUT seconds return
1467 DEFAULT if given, or else signal JOIN-THREAD-ERROR.
1468
1469 Trying to join the main thread will cause JOIN-THREAD to block until
1470 TIMEOUT occurs or the process exits: when main thread exits, the
1471 entire process exits.
1472
1473 NOTE: Return convention in case of a timeout is exprimental and
1474 subject to change."
1475   (let ((lock (thread-result-lock thread))
1476         (got-it nil)
1477         (problem :timeout))
1478     (without-interrupts
1479       (unwind-protect
1480            (if (setf got-it
1481                      (allow-with-interrupts
1482                        ;; Don't use the timeout if the thread is not alive anymore.
1483                        (grab-mutex lock :timeout (and (thread-alive-p thread) timeout))))
1484                (cond ((car (thread-result thread))
1485                       (return-from join-thread
1486                         (values-list (cdr (thread-result thread)))))
1487                      (defaultp
1488                       (return-from join-thread default))
1489                      (t
1490                       (setf problem :abort)))
1491                (when defaultp
1492                  (return-from join-thread default)))
1493         (when got-it
1494           (release-mutex lock))))
1495     (error 'join-thread-error :thread thread :problem problem)))
1496
1497 (defun destroy-thread (thread)
1498   #!+sb-doc
1499   "Deprecated. Same as TERMINATE-THREAD."
1500   (terminate-thread thread))
1501
1502 (defmacro with-interruptions-lock ((thread) &body body)
1503   `(with-system-mutex ((thread-interruptions-lock ,thread))
1504      ,@body))
1505
1506 ;;; Called from the signal handler.
1507 #!-win32
1508 (defun run-interruption ()
1509   (let ((interruption (with-interruptions-lock (*current-thread*)
1510                         (pop (thread-interruptions *current-thread*)))))
1511     ;; If there is more to do, then resignal and let the normal
1512     ;; interrupt deferral mechanism take care of the rest. From the
1513     ;; OS's point of view the signal we are in the handler for is no
1514     ;; longer pending, so the signal will not be lost.
1515     (when (thread-interruptions *current-thread*)
1516       (kill-safely (thread-os-thread *current-thread*) sb!unix:sigpipe))
1517     (when interruption
1518       (funcall interruption))))
1519
1520 (defun interrupt-thread (thread function)
1521   #!+sb-doc
1522   "Interrupt THREAD and make it run FUNCTION.
1523
1524 The interrupt is asynchronous, and can occur anywhere with the exception of
1525 sections protected using SB-SYS:WITHOUT-INTERRUPTS.
1526
1527 FUNCTION is called with interrupts disabled, under
1528 SB-SYS:ALLOW-WITH-INTERRUPTS. Since functions such as GRAB-MUTEX may try to
1529 enable interrupts internally, in most cases FUNCTION should either enter
1530 SB-SYS:WITH-INTERRUPTS to allow nested interrupts, or
1531 SB-SYS:WITHOUT-INTERRUPTS to prevent them completely.
1532
1533 When a thread receives multiple interrupts, they are executed in the order
1534 they were sent -- first in, first out.
1535
1536 This means that a great degree of care is required to use INTERRUPT-THREAD
1537 safely and sanely in a production environment. The general recommendation is
1538 to limit uses of INTERRUPT-THREAD for interactive debugging, banning it
1539 entirely from production environments -- it is simply exceedingly hard to use
1540 correctly.
1541
1542 With those caveats in mind, what you need to know when using it:
1543
1544  * If calling FUNCTION causes a non-local transfer of control (ie. an
1545    unwind), all normal cleanup forms will be executed.
1546
1547    However, if the interrupt occurs during cleanup forms of an UNWIND-PROTECT,
1548    it is just as if that had happened due to a regular GO, THROW, or
1549    RETURN-FROM: the interrupted cleanup form and those following it in the
1550    same UNWIND-PROTECT do not get executed.
1551
1552    SBCL tries to keep its own internals asynch-unwind-safe, but this is
1553    frankly an unreasonable expectation for third party libraries, especially
1554    given that asynch-unwind-safety does not compose: a function calling
1555    only asynch-unwind-safe function isn't automatically asynch-unwind-safe.
1556
1557    This means that in order for an asych unwind to be safe, the entire
1558    callstack at the point of interruption needs to be asynch-unwind-safe.
1559
1560  * In addition to asynch-unwind-safety you must consider the issue of
1561    re-entrancy. INTERRUPT-THREAD can cause function that are never normally
1562    called recursively to be re-entered during their dynamic contour,
1563    which may cause them to misbehave. (Consider binding of special variables,
1564    values of global variables, etc.)
1565
1566 Take togather, these two restrict the \"safe\" things to do using
1567 INTERRUPT-THREAD to a fairly minimal set. One useful one -- exclusively for
1568 interactive development use is using it to force entry to debugger to inspect
1569 the state of a thread:
1570
1571   (interrupt-thread thread #'break)
1572
1573 Short version: be careful out there."
1574  #!+win32
1575   (declare (ignore thread))
1576   #!+win32
1577   (with-interrupt-bindings
1578     (with-interrupts (funcall function)))
1579   #!-win32
1580   (let ((os-thread (thread-os-thread thread)))
1581     (cond ((not os-thread)
1582            (error 'interrupt-thread-error :thread thread))
1583           (t
1584            (with-interruptions-lock (thread)
1585              ;; Append to the end of the interruptions queue. It's
1586              ;; O(N), but it does not hurt to slow interruptors down a
1587              ;; bit when the queue gets long.
1588              (setf (thread-interruptions thread)
1589                    (append (thread-interruptions thread)
1590                            (list (lambda ()
1591                                    (without-interrupts
1592                                      (allow-with-interrupts
1593                                        (funcall function))))))))
1594            (when (minusp (kill-safely os-thread sb!unix:sigpipe))
1595              (error 'interrupt-thread-error :thread thread))))))
1596
1597 (defun terminate-thread (thread)
1598   #!+sb-doc
1599   "Terminate the thread identified by THREAD, by interrupting it and
1600 causing it to call SB-EXT:ABORT-THREAD with :ALLOW-EXIT T.
1601
1602 The unwind caused by TERMINATE-THREAD is asynchronous, meaning that
1603 eg. thread executing
1604
1605   (let (foo)
1606      (unwind-protect
1607          (progn
1608             (setf foo (get-foo))
1609             (work-on-foo foo))
1610        (when foo
1611          ;; An interrupt occurring inside the cleanup clause
1612          ;; will cause cleanups from the current UNWIND-PROTECT
1613          ;; to be dropped.
1614          (release-foo foo))))
1615
1616 might miss calling RELEASE-FOO despite GET-FOO having returned true if
1617 the interrupt occurs inside the cleanup clause, eg. during execution
1618 of RELEASE-FOO.
1619
1620 Thus, in order to write an asynch unwind safe UNWIND-PROTECT you need
1621 to use WITHOUT-INTERRUPTS:
1622
1623   (let (foo)
1624     (sb-sys:without-interrupts
1625       (unwind-protect
1626           (progn
1627             (setf foo (sb-sys:allow-with-interrupts
1628                         (get-foo)))
1629             (sb-sys:with-local-interrupts
1630               (work-on-foo foo)))
1631        (when foo
1632          (release-foo foo)))))
1633
1634 Since most libraries using UNWIND-PROTECT do not do this, you should never
1635 assume that unknown code can safely be terminated using TERMINATE-THREAD."
1636   (interrupt-thread thread (lambda () (abort-thread :allow-exit t))))
1637
1638 (define-alien-routine "thread_yield" int)
1639
1640 #!+sb-doc
1641 (setf (fdocumentation 'thread-yield 'function)
1642       "Yield the processor to other threads.")
1643
1644 ;;; internal use only.  If you think you need to use these, either you
1645 ;;; are an SBCL developer, are doing something that you should discuss
1646 ;;; with an SBCL developer first, or are doing something that you
1647 ;;; should probably discuss with a professional psychiatrist first
1648 #!+sb-thread
1649 (progn
1650   (defun %thread-sap (thread)
1651     (let ((thread-sap (alien-sap (extern-alien "all_threads" (* t))))
1652           (target (thread-os-thread thread)))
1653       (loop
1654         (when (sap= thread-sap (int-sap 0)) (return nil))
1655         (let ((os-thread (sap-ref-word thread-sap
1656                                        (* sb!vm:n-word-bytes
1657                                           sb!vm::thread-os-thread-slot))))
1658           (when (= os-thread target) (return thread-sap))
1659           (setf thread-sap
1660                 (sap-ref-sap thread-sap (* sb!vm:n-word-bytes
1661                                            sb!vm::thread-next-slot)))))))
1662
1663   (defun %symbol-value-in-thread (symbol thread)
1664     ;; Prevent the thread from dying completely while we look for the TLS
1665     ;; area...
1666     (with-all-threads-lock
1667       (if (thread-alive-p thread)
1668           (let* ((offset (sb!kernel:get-lisp-obj-address
1669                           (sb!vm::symbol-tls-index symbol)))
1670                  (obj (sap-ref-lispobj (%thread-sap thread) offset))
1671                  (tl-val (sb!kernel:get-lisp-obj-address obj)))
1672             (cond ((zerop offset)
1673                    (values nil :no-tls-value))
1674                   ((or (eql tl-val sb!vm:no-tls-value-marker-widetag)
1675                        (eql tl-val sb!vm:unbound-marker-widetag))
1676                    (values nil :unbound-in-thread))
1677                   (t
1678                    (values obj :ok))))
1679           (values nil :thread-dead))))
1680
1681   (defun %set-symbol-value-in-thread (symbol thread value)
1682     (with-pinned-objects (value)
1683       ;; Prevent the thread from dying completely while we look for the TLS
1684       ;; area...
1685       (with-all-threads-lock
1686         (if (thread-alive-p thread)
1687             (let ((offset (sb!kernel:get-lisp-obj-address
1688                            (sb!vm::symbol-tls-index symbol))))
1689               (cond ((zerop offset)
1690                      (values nil :no-tls-value))
1691                     (t
1692                      (setf (sap-ref-lispobj (%thread-sap thread) offset)
1693                            value)
1694                      (values value :ok))))
1695             (values nil :thread-dead)))))
1696
1697   (define-alien-variable tls-index-start unsigned-int)
1698
1699   ;; Get values from the TLS area of the current thread.
1700   (defun %thread-local-references ()
1701     (without-gcing
1702       (let ((sap (%thread-sap *current-thread*)))
1703         (loop for index from tls-index-start
1704                 below (symbol-value 'sb!vm::*free-tls-index*)
1705               for value = (sap-ref-word sap (* sb!vm:n-word-bytes index))
1706               for (obj ok) = (multiple-value-list (sb!kernel:make-lisp-obj value nil))
1707               unless (or (not ok)
1708                          (typep obj '(or fixnum character))
1709                          (member value
1710                                  '(#.sb!vm:no-tls-value-marker-widetag
1711                                    #.sb!vm:unbound-marker-widetag))
1712                          (member obj seen :test #'eq))
1713                 collect obj into seen
1714               finally (return seen))))))
1715
1716 (defun symbol-value-in-thread (symbol thread &optional (errorp t))
1717   "Return the local value of SYMBOL in THREAD, and a secondary value of T
1718 on success.
1719
1720 If the value cannot be retrieved (because the thread has exited or because it
1721 has no local binding for NAME) and ERRORP is true signals an error of type
1722 SYMBOL-VALUE-IN-THREAD-ERROR; if ERRORP is false returns a primary value of
1723 NIL, and a secondary value of NIL.
1724
1725 Can also be used with SETF to change the thread-local value of SYMBOL.
1726
1727 SYMBOL-VALUE-IN-THREAD is primarily intended as a debugging tool, and not as a
1728 mechanism for inter-thread communication."
1729   (declare (symbol symbol) (thread thread))
1730   #!+sb-thread
1731   (multiple-value-bind (res status) (%symbol-value-in-thread symbol thread)
1732     (if (eq :ok status)
1733         (values res t)
1734         (if errorp
1735             (error 'symbol-value-in-thread-error
1736                    :name symbol
1737                    :thread thread
1738                    :info (list :read status))
1739             (values nil nil))))
1740   #!-sb-thread
1741   (if (boundp symbol)
1742       (values (symbol-value symbol) t)
1743       (if errorp
1744           (error 'symbol-value-in-thread-error
1745                  :name symbol
1746                  :thread thread
1747                  :info (list :read :unbound-in-thread))
1748           (values nil nil))))
1749
1750 (defun (setf symbol-value-in-thread) (value symbol thread &optional (errorp t))
1751   (declare (symbol symbol) (thread thread))
1752   #!+sb-thread
1753   (multiple-value-bind (res status) (%set-symbol-value-in-thread symbol thread value)
1754     (if (eq :ok status)
1755         (values res t)
1756         (if errorp
1757             (error 'symbol-value-in-thread-error
1758                    :name symbol
1759                    :thread thread
1760                    :info (list :write status))
1761             (values nil nil))))
1762   #!-sb-thread
1763   (if (boundp symbol)
1764       (values (setf (symbol-value symbol) value) t)
1765       (if errorp
1766           (error 'symbol-value-in-thread-error
1767                  :name symbol
1768                  :thread thread
1769                  :info (list :write :unbound-in-thread))
1770           (values nil nil))))
1771
1772 (defun sb!vm::locked-symbol-global-value-add (symbol-name delta)
1773   (sb!vm::locked-symbol-global-value-add symbol-name delta))
1774 \f
1775
1776 ;;;; Stepping
1777
1778 (defun thread-stepping ()
1779   (make-lisp-obj
1780    (sap-ref-word (current-thread-sap)
1781                  (* sb!vm::thread-stepping-slot sb!vm:n-word-bytes))))
1782
1783 (defun (setf thread-stepping) (value)
1784   (setf (sap-ref-word (current-thread-sap)
1785                       (* sb!vm::thread-stepping-slot sb!vm:n-word-bytes))
1786         (get-lisp-obj-address value)))