1.0.5.11: fix non-threaded build
[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 ;;; Of the WITH-PINNED-OBJECTS in this file, not every single one is
15 ;;; necessary because threads are only supported with the conservative
16 ;;; gencgc and numbers on the stack (returned by GET-LISP-OBJ-ADDRESS)
17 ;;; are treated as references.
18
19 ;;; set the doc here because in early-thread FDOCUMENTATION is not
20 ;;; available, yet
21 #!+sb-doc
22 (setf (fdocumentation '*current-thread* 'variable)
23       "Bound in each thread to the thread itself.")
24
25 (defstruct (thread (:constructor %make-thread))
26   #!+sb-doc
27   "Thread type. Do not rely on threads being structs as it may change
28 in future versions."
29   name
30   %alive-p
31   os-thread
32   interruptions
33   (interruptions-lock (make-mutex :name "thread interruptions lock"))
34   result
35   (result-lock (make-mutex :name "thread result lock")))
36
37 #!+sb-doc
38 (setf (fdocumentation 'thread-name 'function)
39       "The name of the thread. Setfable.")
40
41 (def!method print-object ((thread thread) stream)
42   (if (thread-name thread)
43       (print-unreadable-object (thread stream :type t :identity t)
44         (prin1 (thread-name thread) stream))
45       (print-unreadable-object (thread stream :type t :identity t)
46         ;; body is empty => there is only one space between type and
47         ;; identity
48         ))
49   thread)
50
51 (defun thread-alive-p (thread)
52   #!+sb-doc
53   "Check if THREAD is running."
54   (thread-%alive-p thread))
55
56 ;; A thread is eligible for gc iff it has finished and there are no
57 ;; more references to it. This list is supposed to keep a reference to
58 ;; all running threads.
59 (defvar *all-threads* ())
60 (defvar *all-threads-lock* (make-mutex :name "all threads lock"))
61
62 (defmacro with-all-threads-lock (&body body)
63   #!-sb-thread
64   `(locally ,@body)
65   #!+sb-thread
66   `(without-interrupts
67      (with-mutex (*all-threads-lock*)
68        ,@body)))
69
70 (defun list-all-threads ()
71   #!+sb-doc
72   "Return a list of the live threads."
73   (with-all-threads-lock
74     (copy-list *all-threads*)))
75
76 (declaim (inline current-thread-sap))
77 (defun current-thread-sap ()
78   (sb!vm::current-thread-offset-sap sb!vm::thread-this-slot))
79
80 (declaim (inline current-thread-sap-id))
81 (defun current-thread-sap-id ()
82   (sap-int
83    (sb!vm::current-thread-offset-sap sb!vm::thread-os-thread-slot)))
84
85 (defun init-initial-thread ()
86   (/show0 "Entering INIT-INITIAL-THREAD")
87   (let ((initial-thread (%make-thread :name "initial thread"
88                                       :%alive-p t
89                                       :os-thread (current-thread-sap-id))))
90     (setq *current-thread* initial-thread)
91     ;; Either *all-threads* is empty or it contains exactly one thread
92     ;; in case we are in reinit since saving core with multiple
93     ;; threads doesn't work.
94     (setq *all-threads* (list initial-thread))))
95
96 ;;;;
97
98 #!+sb-thread
99 (progn
100   ;; FIXME it would be good to define what a thread id is or isn't
101   ;; (our current assumption is that it's a fixnum).  It so happens
102   ;; that on Linux it's a pid, but it might not be on posix thread
103   ;; implementations.
104   (define-alien-routine ("create_thread" %create-thread)
105       unsigned-long (lisp-fun-address unsigned-long))
106
107   (define-alien-routine "signal_interrupt_thread"
108       integer (os-thread unsigned-long))
109
110   (define-alien-routine "block_deferrable_signals"
111       void)
112
113   #!+sb-lutex
114   (progn
115     (declaim (inline %lutex-init %lutex-wait %lutex-wake
116                      %lutex-lock %lutex-unlock))
117
118     (sb!alien:define-alien-routine ("lutex_init" %lutex-init)
119         int (lutex unsigned-long))
120
121     (sb!alien:define-alien-routine ("lutex_wait" %lutex-wait)
122         int (queue-lutex unsigned-long) (mutex-lutex unsigned-long))
123
124     (sb!alien:define-alien-routine ("lutex_wake" %lutex-wake)
125         int (lutex unsigned-long) (n int))
126
127     (sb!alien:define-alien-routine ("lutex_lock" %lutex-lock)
128         int (lutex unsigned-long))
129
130     (sb!alien:define-alien-routine ("lutex_trylock" %lutex-trylock)
131         int (lutex unsigned-long))
132
133     (sb!alien:define-alien-routine ("lutex_unlock" %lutex-unlock)
134         int (lutex unsigned-long))
135
136     (sb!alien:define-alien-routine ("lutex_destroy" %lutex-destroy)
137         int (lutex unsigned-long))
138
139     ;; FIXME: Defining a whole bunch of alien-type machinery just for
140     ;; passing primitive lutex objects directly to foreign functions
141     ;; doesn't seem like fun right now. So instead we just manually
142     ;; pin the lutex, get its address, and let the callee untag it.
143     (defmacro with-lutex-address ((name lutex) &body body)
144       `(let ((,name ,lutex))
145          (with-pinned-objects (,name)
146            (let ((,name (get-lisp-obj-address ,name)))
147              ,@body))))
148
149     (defun make-lutex ()
150       (/show0 "Entering MAKE-LUTEX")
151       ;; Suppress GC until the lutex has been properly registered with
152       ;; the GC.
153       (without-gcing
154         (let ((lutex (sb!vm::%make-lutex)))
155           (/show0 "LUTEX=..")
156           (/hexstr lutex)
157           (with-lutex-address (lutex lutex)
158             (%lutex-init lutex))
159           lutex))))
160
161   #!-sb-lutex
162   (progn
163     (declaim (inline futex-wait futex-wake))
164
165     (sb!alien:define-alien-routine "futex_wait"
166         int (word unsigned-long) (old-value unsigned-long)
167         (to-sec long) (to-usec unsigned-long))
168
169     (sb!alien:define-alien-routine "futex_wake"
170         int (word unsigned-long) (n unsigned-long))))
171
172 ;;; used by debug-int.lisp to access interrupt contexts
173 #!-(or sb-fluid sb-thread) (declaim (inline sb!vm::current-thread-offset-sap))
174 #!-sb-thread
175 (defun sb!vm::current-thread-offset-sap (n)
176   (declare (type (unsigned-byte 27) n))
177   (sap-ref-sap (alien-sap (extern-alien "all_threads" (* t)))
178                (* n sb!vm:n-word-bytes)))
179
180 #!+sb-thread
181 (defun sb!vm::current-thread-offset-sap (n)
182   (declare (type (unsigned-byte 27) n))
183   (sb!vm::current-thread-offset-sap n))
184
185 ;;;; spinlocks
186 #!+sb-thread
187 (define-structure-slot-compare-and-swap
188     compare-and-swap-spinlock-value
189     :structure spinlock
190     :slot value)
191
192 (declaim (inline get-spinlock release-spinlock))
193
194 (defun get-spinlock (spinlock)
195   (declare (optimize (speed 3) (safety 0))
196            #!-sb-thread
197            (ignore spinlock))
198   ;; %instance-set-conditional can test for 0 (which is a fixnum) and
199   ;; store any value
200   #!+sb-thread
201   (loop until
202        (eql 0 (compare-and-swap-spinlock-value spinlock 0 1)))
203   t)
204
205 (defun release-spinlock (spinlock)
206   (declare (optimize (speed 3) (safety 0))
207            #!-sb-thread (ignore spinlock))
208   ;; %instance-set-conditional cannot compare arbitrary objects
209   ;; meaningfully, so (compare-and-swap-spinlock-value our-value 0)
210   ;; does not work for bignum thread ids.
211   #!+sb-thread
212   (setf (spinlock-value spinlock) 0)
213   nil)
214
215 ;;;; mutexes
216
217 #!+sb-doc
218 (setf (fdocumentation 'make-mutex 'function)
219       "Create a mutex."
220       (fdocumentation 'mutex-name 'function)
221       "The name of the mutex. Setfable."
222       (fdocumentation 'mutex-value 'function)
223       "The value of the mutex. NIL if the mutex is free. Setfable.")
224
225 #!+(and sb-thread (not sb-lutex))
226 (progn
227   (define-structure-slot-addressor mutex-value-address
228       :structure mutex
229       :slot value)
230   (define-structure-slot-compare-and-swap
231       compare-and-swap-mutex-value
232       :structure mutex
233       :slot value))
234
235 (defun get-mutex (mutex &optional (new-value *current-thread*) (waitp t))
236   #!+sb-doc
237   "Acquire MUTEX, setting it to NEW-VALUE or some suitable default
238 value if NIL. If WAITP is non-NIL and the mutex is in use, sleep
239 until it is available."
240   (declare (type mutex mutex) (optimize (speed 3)))
241   (/show0 "Entering GET-MUTEX")
242   (unless new-value
243     (setq new-value *current-thread*))
244   #!-sb-thread
245   (let ((old (mutex-value mutex)))
246     (when (and old waitp)
247       (error "In unithread mode, mutex ~S was requested with WAITP ~S and ~
248               new-value ~S, but has already been acquired (with value ~S)."
249              mutex waitp new-value old))
250     (setf (mutex-value mutex) new-value)
251     t)
252   #!+sb-thread
253   (progn
254     (when (eql new-value (mutex-value mutex))
255       (warn "recursive lock attempt ~S~%" mutex)
256       (format *debug-io* "Thread: ~A~%" *current-thread*)
257       (sb!debug:backtrace most-positive-fixnum *debug-io*)
258       (force-output *debug-io*))
259     ;; FIXME: Lutexes do not currently support deadlines, as at least
260     ;; on Darwin pthread_foo_timedbar functions are not supported:
261     ;; this means that we probably need to use the Carbon multiprocessing
262     ;; functions on Darwin.
263     #!+sb-lutex
264     (when (zerop (with-lutex-address (lutex (mutex-lutex mutex))
265                    (if waitp
266                        (%lutex-lock lutex)
267                        (%lutex-trylock lutex))))
268       (setf (mutex-value mutex) new-value))
269     #!-sb-lutex
270     (let (old)
271       (when (and (setf old (compare-and-exchange-mutex-value mutex nil new-value))
272                  waitp)
273         (loop while old
274               do (multiple-value-bind (to-sec to-usec) (decode-timeout nil)
275                    (when (= 1 (with-pinned-objects (mutex old)
276                                 (futex-wait (mutex-value-address mutex)
277                                             (get-lisp-obj-address old)
278                                             (or to-sec -1)
279                                             (or to-usec 0))))
280                      (signal-deadline)))
281               (setf old (compare-and-exchange-mutex-value mutex nil new-value))))
282       (not old))))
283
284 (defun release-mutex (mutex)
285   #!+sb-doc
286   "Release MUTEX by setting it to NIL. Wake up threads waiting for
287 this mutex."
288   (declare (type mutex mutex))
289   (/show0 "Entering RELEASE-MUTEX")
290   (setf (mutex-value mutex) nil)
291   #!+sb-thread
292   (progn
293     #!+sb-lutex
294     (with-lutex-address (lutex (mutex-lutex mutex))
295       (%lutex-unlock lutex))
296     #!-sb-lutex
297     (futex-wake (mutex-value-address mutex) 1)))
298
299 ;;;; waitqueues/condition variables
300
301 (defstruct (waitqueue (:constructor %make-waitqueue))
302   #!+sb-doc
303   "Waitqueue type."
304   (name nil :type (or null simple-string))
305   #!+(and sb-lutex sb-thread)
306   (lutex (make-lutex))
307   #!-sb-lutex
308   (data nil))
309
310 (defun make-waitqueue (&key name)
311   #!+sb-doc
312   "Create a waitqueue."
313   (%make-waitqueue :name name))
314
315 #!+sb-doc
316 (setf (fdocumentation 'waitqueue-name 'function)
317       "The name of the waitqueue. Setfable.")
318
319 #!+(and sb-thread (not sb-lutex))
320 (define-structure-slot-addressor waitqueue-data-address
321     :structure waitqueue
322     :slot data)
323
324 (defun condition-wait (queue mutex)
325   #!+sb-doc
326   "Atomically release MUTEX and enqueue ourselves on QUEUE.  Another
327 thread may subsequently notify us using CONDITION-NOTIFY, at which
328 time we reacquire MUTEX and return to the caller."
329   #!-sb-thread (declare (ignore queue))
330   (assert mutex)
331   #!-sb-thread (error "Not supported in unithread builds.")
332   #!+sb-thread
333   (let ((value (mutex-value mutex)))
334     (/show0 "CONDITION-WAITing")
335     #!+sb-lutex
336     (progn
337       (setf (mutex-value mutex) nil)
338       (with-lutex-address (queue-lutex-address (waitqueue-lutex queue))
339         (with-lutex-address (mutex-lutex-address (mutex-lutex mutex))
340           (%lutex-wait queue-lutex-address mutex-lutex-address)))
341       (setf (mutex-value mutex) value))
342     #!-sb-lutex
343     (unwind-protect
344          (let ((me *current-thread*))
345            ;; XXX we should do something to ensure that the result of this setf
346            ;; is visible to all CPUs
347            (setf (waitqueue-data queue) me)
348            (release-mutex mutex)
349            ;; Now we go to sleep using futex-wait.  If anyone else
350            ;; manages to grab MUTEX and call CONDITION-NOTIFY during
351            ;; this comment, it will change queue->data, and so
352            ;; futex-wait returns immediately instead of sleeping.
353            ;; Ergo, no lost wakeup. We may get spurious wakeups,
354            ;; but that's ok.
355            (multiple-value-bind (to-sec to-usec) (decode-timeout nil)
356              (when (= 1 (with-pinned-objects (queue me)
357                           (futex-wait (waitqueue-data-address queue)
358                                       (get-lisp-obj-address me)
359                                       (or to-sec -1) ;; our way if saying "no timeout"
360                                       (or to-usec 0))))
361                (signal-deadline))))
362       ;; If we are interrupted while waiting, we should do these things
363       ;; before returning.  Ideally, in the case of an unhandled signal,
364       ;; we should do them before entering the debugger, but this is
365       ;; better than nothing.
366       (get-mutex mutex value))))
367
368 (defun condition-notify (queue &optional (n 1))
369   #!+sb-doc
370   "Notify N threads waiting on QUEUE."
371   #!-sb-thread (declare (ignore queue n))
372   #!-sb-thread (error "Not supported in unithread builds.")
373   #!+sb-thread
374   (declare (type (and fixnum (integer 1)) n))
375   (/show0 "Entering CONDITION-NOTIFY")
376   #!+sb-thread
377   (progn
378     #!+sb-lutex
379     (with-lutex-address (lutex (waitqueue-lutex queue))
380       (%lutex-wake lutex n))
381     ;; no problem if >1 thread notifies during the comment in
382     ;; condition-wait: as long as the value in queue-data isn't the
383     ;; waiting thread's id, it matters not what it is
384     ;; XXX we should do something to ensure that the result of this setf
385     ;; is visible to all CPUs
386     #!-sb-lutex
387     (let ((me *current-thread*))
388       (progn
389         (setf (waitqueue-data queue) me)
390         (with-pinned-objects (queue)
391           (futex-wake (waitqueue-data-address queue) n))))))
392
393 (defun condition-broadcast (queue)
394   #!+sb-doc
395   "Notify all threads waiting on QUEUE."
396   (condition-notify queue
397                     ;; On a 64-bit platform truncating M-P-F to an int results
398                     ;; in -1, which wakes up only one thread.
399                     (ldb (byte 29 0)
400                          most-positive-fixnum)))
401
402 ;;;; semaphores
403
404 (defstruct (semaphore (:constructor %make-semaphore))
405   #!+sb-doc
406   "Semaphore type."
407   (name nil :type (or null simple-string))
408   (count 0 :type (integer 0))
409   (mutex (make-mutex))
410   (queue (make-waitqueue)))
411
412 (defun make-semaphore (&key name (count 0))
413   #!+sb-doc
414   "Create a semaphore with the supplied COUNT."
415   (%make-semaphore :name name :count count))
416
417 (setf (fdocumentation 'semaphore-name 'function)
418       "The name of the semaphore. Setfable.")
419
420 (defun wait-on-semaphore (sem)
421   #!+sb-doc
422   "Decrement the count of SEM if the count would not be negative. Else
423 block until the semaphore can be decremented."
424   ;; a more direct implementation based directly on futexes should be
425   ;; possible
426   (with-mutex ((semaphore-mutex sem))
427     (loop until (> (semaphore-count sem) 0)
428           do (condition-wait (semaphore-queue sem) (semaphore-mutex sem))
429           finally (decf (semaphore-count sem)))))
430
431 (defun signal-semaphore (sem &optional (n 1))
432   #!+sb-doc
433   "Increment the count of SEM by N. If there are threads waiting on
434 this semaphore, then N of them is woken up."
435   (declare (type (and fixnum (integer 1)) n))
436   (with-mutex ((semaphore-mutex sem))
437     (when (= n (incf (semaphore-count sem) n))
438       (condition-notify (semaphore-queue sem) n))))
439
440 ;;;; job control, independent listeners
441
442 (defstruct session
443   (lock (make-mutex :name "session lock"))
444   (threads nil)
445   (interactive-threads nil)
446   (interactive-threads-queue (make-waitqueue)))
447
448 (defvar *session* nil)
449
450 ;;; the debugger itself tries to acquire the session lock, don't let
451 ;;; funny situations (like getting a sigint while holding the session
452 ;;; lock) occur
453 (defmacro with-session-lock ((session) &body body)
454   #!-sb-thread (declare (ignore session))
455   #!-sb-thread
456   `(locally ,@body)
457   #!+sb-thread
458   `(without-interrupts
459      (with-mutex ((session-lock ,session))
460        ,@body)))
461
462 (defun new-session ()
463   (make-session :threads (list *current-thread*)
464                 :interactive-threads (list *current-thread*)))
465
466 (defun init-job-control ()
467   (/show0 "Entering INIT-JOB-CONTROL")
468   (setf *session* (new-session))
469   (/show0 "Exiting INIT-JOB-CONTROL"))
470
471 (defun %delete-thread-from-session (thread session)
472   (with-session-lock (session)
473     (setf (session-threads session)
474           (delete thread (session-threads session))
475           (session-interactive-threads session)
476           (delete thread (session-interactive-threads session)))))
477
478 (defun call-with-new-session (fn)
479   (%delete-thread-from-session *current-thread* *session*)
480   (let ((*session* (new-session)))
481     (funcall fn)))
482
483 (defmacro with-new-session (args &body forms)
484   (declare (ignore args))               ;for extensibility
485   (sb!int:with-unique-names (fb-name)
486     `(labels ((,fb-name () ,@forms))
487       (call-with-new-session (function ,fb-name)))))
488
489 ;;; Remove thread from its session, if it has one.
490 #!+sb-thread
491 (defun handle-thread-exit (thread)
492   (/show0 "HANDLING THREAD EXIT")
493   ;; We're going down, can't handle interrupts sanely anymore.
494   ;; GC remains enabled.
495   (block-deferrable-signals)
496   ;; Lisp-side cleanup
497   (with-all-threads-lock
498     (setf (thread-%alive-p thread) nil)
499     (setf (thread-os-thread thread) nil)
500     (setq *all-threads* (delete thread *all-threads*))
501     (when *session*
502       (%delete-thread-from-session thread *session*)))
503   #!+sb-lutex
504   (without-gcing
505     (/show0 "FREEING MUTEX LUTEX")
506     (with-lutex-address (lutex (mutex-lutex (thread-interruptions-lock thread)))
507       (%lutex-destroy lutex))))
508
509 (defun terminate-session ()
510   #!+sb-doc
511   "Kill all threads in session except for this one.  Does nothing if current
512 thread is not the foreground thread."
513   ;; FIXME: threads created in other threads may escape termination
514   (let ((to-kill
515          (with-session-lock (*session*)
516            (and (eq *current-thread*
517                     (car (session-interactive-threads *session*)))
518                 (session-threads *session*)))))
519     ;; do the kill after dropping the mutex; unwind forms in dying
520     ;; threads may want to do session things
521     (dolist (thread to-kill)
522       (unless (eq thread *current-thread*)
523         ;; terminate the thread but don't be surprised if it has
524         ;; exited in the meantime
525         (handler-case (terminate-thread thread)
526           (interrupt-thread-error ()))))))
527
528 ;;; called from top of invoke-debugger
529 (defun debugger-wait-until-foreground-thread (stream)
530   "Returns T if thread had been running in background, NIL if it was
531 interactive."
532   (declare (ignore stream))
533   #!-sb-thread nil
534   #!+sb-thread
535   (prog1
536       (with-session-lock (*session*)
537         (not (member *current-thread*
538                      (session-interactive-threads *session*))))
539     (get-foreground)))
540
541 (defun get-foreground ()
542   #!-sb-thread t
543   #!+sb-thread
544   (let ((was-foreground t))
545     (loop
546      (/show0 "Looping in GET-FOREGROUND")
547      (with-session-lock (*session*)
548        (let ((int-t (session-interactive-threads *session*)))
549          (when (eq (car int-t) *current-thread*)
550            (unless was-foreground
551              (format *query-io* "Resuming thread ~A~%" *current-thread*))
552            (return-from get-foreground t))
553          (setf was-foreground nil)
554          (unless (member *current-thread* int-t)
555            (setf (cdr (last int-t))
556                  (list *current-thread*)))
557          (condition-wait
558           (session-interactive-threads-queue *session*)
559           (session-lock *session*)))))))
560
561 (defun release-foreground (&optional next)
562   #!+sb-doc
563   "Background this thread.  If NEXT is supplied, arrange for it to
564 have the foreground next."
565   #!-sb-thread (declare (ignore next))
566   #!-sb-thread nil
567   #!+sb-thread
568   (with-session-lock (*session*)
569     (when (rest (session-interactive-threads *session*))
570       (setf (session-interactive-threads *session*)
571             (delete *current-thread* (session-interactive-threads *session*))))
572     (when next
573       (setf (session-interactive-threads *session*)
574             (list* next
575                    (delete next (session-interactive-threads *session*)))))
576     (condition-broadcast (session-interactive-threads-queue *session*))))
577
578 (defun foreground-thread ()
579   (car (session-interactive-threads *session*)))
580
581 (defun make-listener-thread (tty-name)
582   (assert (probe-file tty-name))
583   (let* ((in (sb!unix:unix-open (namestring tty-name) sb!unix:o_rdwr #o666))
584          (out (sb!unix:unix-dup in))
585          (err (sb!unix:unix-dup in)))
586     (labels ((thread-repl ()
587                (sb!unix::unix-setsid)
588                (let* ((sb!impl::*stdin*
589                        (make-fd-stream in :input t :buffering :line
590                                        :dual-channel-p t))
591                       (sb!impl::*stdout*
592                        (make-fd-stream out :output t :buffering :line
593                                               :dual-channel-p t))
594                       (sb!impl::*stderr*
595                        (make-fd-stream err :output t :buffering :line
596                                               :dual-channel-p t))
597                       (sb!impl::*tty*
598                        (make-fd-stream err :input t :output t
599                                               :buffering :line
600                                               :dual-channel-p t))
601                       (sb!impl::*descriptor-handlers* nil))
602                  (with-new-session ()
603                    (unwind-protect
604                         (sb!impl::toplevel-repl nil)
605                      (sb!int:flush-standard-output-streams))))))
606       (make-thread #'thread-repl))))
607
608 ;;;; the beef
609
610 (defun make-thread (function &key name)
611   #!+sb-doc
612   "Create a new thread of NAME that runs FUNCTION. When the function
613 returns the thread exits. The return values of FUNCTION are kept
614 around and can be retrieved by JOIN-THREAD."
615   #!-sb-thread (declare (ignore function name))
616   #!-sb-thread (error "Not supported in unithread builds.")
617   #!+sb-thread
618   (let* ((thread (%make-thread :name name))
619          (setup-sem (make-semaphore :name "Thread setup semaphore"))
620          (real-function (coerce function 'function))
621          (initial-function
622           (lambda ()
623             ;; In time we'll move some of the binding presently done in C
624             ;; here too.
625             ;;
626             ;; KLUDGE: Here we have a magic list of variables that are
627             ;; not thread-safe for one reason or another.  As people
628             ;; report problems with the thread safety of certain
629             ;; variables, (e.g. "*print-case* in multiple threads
630             ;; broken", sbcl-devel 2006-07-14), we add a few more
631             ;; bindings here.  The Right Thing is probably some variant
632             ;; of Allegro's *cl-default-special-bindings*, as that is at
633             ;; least accessible to users to secure their own libraries.
634             ;;   --njf, 2006-07-15
635             (let ((*current-thread* thread)
636                   (*restart-clusters* nil)
637                   (*handler-clusters* nil)
638                   (*condition-restarts* nil)
639                   (sb!impl::*step-out* nil)
640                   ;; internal printer variables
641                   (sb!impl::*previous-case* nil)
642                   (sb!impl::*previous-readtable-case* nil)
643                   (sb!impl::*merge-sort-temp-vector* (vector)) ; keep these small!
644                   (sb!impl::*zap-array-data-temp* (vector))    ;
645                   (sb!impl::*internal-symbol-output-fun* nil)
646                   (sb!impl::*descriptor-handlers* nil)) ; serve-event
647               (setf (thread-os-thread thread) (current-thread-sap-id))
648               (with-mutex ((thread-result-lock thread))
649                 (with-all-threads-lock
650                   (push thread *all-threads*))
651                 (with-session-lock (*session*)
652                   (push thread (session-threads *session*)))
653                 (setf (thread-%alive-p thread) t)
654                 (signal-semaphore setup-sem)
655                 ;; can't use handling-end-of-the-world, because that flushes
656                 ;; output streams, and we don't necessarily have any (or we
657                 ;; could be sharing them)
658                 (catch 'sb!impl::toplevel-catcher
659                   (catch 'sb!impl::%end-of-the-world
660                     (with-simple-restart
661                         (terminate-thread
662                          (format nil
663                                  "~~@<Terminate this thread (~A)~~@:>"
664                                  *current-thread*))
665                       (unwind-protect
666                            (progn
667                              ;; now that most things have a chance to
668                              ;; work properly without messing up other
669                              ;; threads, it's time to enable signals
670                              (sb!unix::reset-signal-mask)
671                              (setf (thread-result thread)
672                                    (cons t
673                                          (multiple-value-list
674                                           (funcall real-function)))))
675                         (handle-thread-exit thread)))))))
676             (values))))
677     ;; Keep INITIAL-FUNCTION pinned until the child thread is
678     ;; initialized properly.
679     (with-pinned-objects (initial-function)
680       (let ((os-thread
681              (%create-thread
682               (get-lisp-obj-address initial-function))))
683         (when (zerop os-thread)
684           (error "Can't create a new thread"))
685         (wait-on-semaphore setup-sem)
686         thread))))
687
688 (define-condition join-thread-error (error)
689   ((thread :reader join-thread-error-thread :initarg :thread))
690   #!+sb-doc
691   (:documentation "Joining thread failed.")
692   (:report (lambda (c s)
693              (format s "Joining thread failed: thread ~A ~
694                         has not returned normally."
695                      (join-thread-error-thread c)))))
696
697 #!+sb-doc
698 (setf (fdocumentation 'join-thread-error-thread 'function)
699       "The thread that we failed to join.")
700
701 (defun join-thread (thread &key (default nil defaultp))
702   #!+sb-doc
703   "Suspend current thread until THREAD exits. Returns the result
704 values of the thread function. If the thread does not exit normally,
705 return DEFAULT if given or else signal JOIN-THREAD-ERROR."
706   (with-mutex ((thread-result-lock thread))
707     (cond ((car (thread-result thread))
708            (values-list (cdr (thread-result thread))))
709           (defaultp
710            default)
711           (t
712            (error 'join-thread-error :thread thread)))))
713
714 (defun destroy-thread (thread)
715   #!+sb-doc
716   "Deprecated. Same as TERMINATE-THREAD."
717   (terminate-thread thread))
718
719 (define-condition interrupt-thread-error (error)
720   ((thread :reader interrupt-thread-error-thread :initarg :thread))
721   #!+sb-doc
722   (:documentation "Interrupting thread failed.")
723   (:report (lambda (c s)
724              (format s "Interrupt thread failed: thread ~A has exited."
725                      (interrupt-thread-error-thread c)))))
726
727 #!+sb-doc
728 (setf (fdocumentation 'interrupt-thread-error-thread 'function)
729       "The thread that was not interrupted.")
730
731 (defmacro with-interruptions-lock ((thread) &body body)
732   `(without-interrupts
733      (with-mutex ((thread-interruptions-lock ,thread))
734        ,@body)))
735
736 ;; Called from the signal handler.
737 (defun run-interruption ()
738   (in-interruption ()
739     (loop
740        (let ((interruption (with-interruptions-lock (*current-thread*)
741                              (pop (thread-interruptions *current-thread*)))))
742          (if interruption
743              (with-interrupts
744                (funcall interruption))
745              (return))))))
746
747 ;; The order of interrupt execution is peculiar. If thread A
748 ;; interrupts thread B with I1, I2 and B for some reason receives I1
749 ;; when FUN2 is already on the list, then it is FUN2 that gets to run
750 ;; first. But when FUN2 is run SIG_INTERRUPT_THREAD is enabled again
751 ;; and I2 hits pretty soon in FUN2 and run FUN1. This is of course
752 ;; just one scenario, and the order of thread interrupt execution is
753 ;; undefined.
754 (defun interrupt-thread (thread function)
755   #!+sb-doc
756   "Interrupt the live THREAD and make it run FUNCTION. A moderate
757 degree of care is expected for use of INTERRUPT-THREAD, due to its
758 nature: if you interrupt a thread that was holding important locks
759 then do something that turns out to need those locks, you probably
760 won't like the effect."
761   #!-sb-thread (declare (ignore thread))
762   ;; not quite perfect, because it does not take WITHOUT-INTERRUPTS
763   ;; into account
764   #!-sb-thread
765   (funcall function)
766   #!+sb-thread
767   (if (eq thread *current-thread*)
768       (funcall function)
769       (let ((os-thread (thread-os-thread thread)))
770         (cond ((not os-thread)
771                (error 'interrupt-thread-error :thread thread))
772               (t
773                (with-interruptions-lock (thread)
774                  (push function (thread-interruptions thread)))
775                (when (minusp (signal-interrupt-thread os-thread))
776                  (error 'interrupt-thread-error :thread thread)))))))
777
778 (defun terminate-thread (thread)
779   #!+sb-doc
780   "Terminate the thread identified by THREAD, by causing it to run
781 SB-EXT:QUIT - the usual cleanup forms will be evaluated"
782   (interrupt-thread thread 'sb!ext:quit))
783
784 ;;; internal use only.  If you think you need to use this, either you
785 ;;; are an SBCL developer, are doing something that you should discuss
786 ;;; with an SBCL developer first, or are doing something that you
787 ;;; should probably discuss with a professional psychiatrist first
788 #!+sb-thread
789 (defun thread-sap-for-id (id)
790   (let ((thread-sap (alien-sap (extern-alien "all_threads" (* t)))))
791     (loop
792      (when (sap= thread-sap (int-sap 0)) (return nil))
793      (let ((os-thread (sap-ref-word thread-sap
794                                     (* sb!vm:n-word-bytes
795                                        sb!vm::thread-os-thread-slot))))
796        (when (= os-thread id) (return thread-sap))
797        (setf thread-sap
798              (sap-ref-sap thread-sap (* sb!vm:n-word-bytes
799                                         sb!vm::thread-next-slot)))))))
800
801 #!+sb-thread
802 (defun symbol-value-in-thread (symbol thread-sap)
803   (let* ((index (sb!vm::symbol-tls-index symbol))
804          (tl-val (sap-ref-word thread-sap
805                                (* sb!vm:n-word-bytes index))))
806     (if (eql tl-val sb!vm::no-tls-value-marker-widetag)
807         (sb!vm::symbol-global-value symbol)
808         (make-lisp-obj tl-val))))
809
810 (defun sb!vm::locked-symbol-global-value-add (symbol-name delta)
811   (sb!vm::locked-symbol-global-value-add symbol-name delta))
812
813 ;;; Stepping
814
815 (defun thread-stepping ()
816   (make-lisp-obj
817    (sap-ref-word (current-thread-sap)
818                  (* sb!vm::thread-stepping-slot sb!vm:n-word-bytes))))
819
820 (defun (setf thread-stepping) (value)
821   (setf (sap-ref-word (current-thread-sap)
822                       (* sb!vm::thread-stepping-slot sb!vm:n-word-bytes))
823         (get-lisp-obj-address value)))