1.0.4.34: return true from GET-SPINLOCK
[sbcl/lichteblau.git] / src / code / target-thread.lisp
blob513acf637a0c7eae8eca5a78877b80e8d5b69650
1 ;;;; support for threads in the target machine
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.
12 (in-package "SB!THREAD")
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.
19 ;;; set the doc here because in early-thread FDOCUMENTATION is not
20 ;;; available, yet
21 #!+sb-doc
22 (setf (sb!kernel:fdocumentation '*current-thread* 'variable)
23 "Bound in each thread to the thread itself.")
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")))
37 #!+sb-doc
38 (setf (sb!kernel:fdocumentation 'thread-name 'function)
39 "The name of the thread. Setfable.")
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
49 thread)
51 (defun thread-alive-p (thread)
52 #!+sb-doc
53 "Check if THREAD is running."
54 (thread-%alive-p thread))
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"))
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)))
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*)))
76 (declaim (inline current-thread-sap))
77 (defun current-thread-sap ()
78 (sb!vm::current-thread-offset-sap sb!vm::thread-this-slot))
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)))
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))))
96 ;;;;
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))
107 (define-alien-routine "signal_interrupt_thread"
108 integer (os-thread unsigned-long))
110 (define-alien-routine "block_deferrable_signals"
111 void)
113 #!+sb-lutex
114 (progn
115 (declaim (inline %lutex-init %lutex-wait %lutex-wake
116 %lutex-lock %lutex-unlock))
118 (sb!alien:define-alien-routine ("lutex_init" %lutex-init)
119 int (lutex unsigned-long))
121 (sb!alien:define-alien-routine ("lutex_wait" %lutex-wait)
122 int (queue-lutex unsigned-long) (mutex-lutex unsigned-long))
124 (sb!alien:define-alien-routine ("lutex_wake" %lutex-wake)
125 int (lutex unsigned-long) (n int))
127 (sb!alien:define-alien-routine ("lutex_lock" %lutex-lock)
128 int (lutex unsigned-long))
130 (sb!alien:define-alien-routine ("lutex_trylock" %lutex-trylock)
131 int (lutex unsigned-long))
133 (sb!alien:define-alien-routine ("lutex_unlock" %lutex-unlock)
134 int (lutex unsigned-long))
136 (sb!alien:define-alien-routine ("lutex_destroy" %lutex-destroy)
137 int (lutex unsigned-long))
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 (sb!kernel:get-lisp-obj-address ,name)))
147 ,@body))))
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))))
161 #!-sb-lutex
162 (progn
163 (declaim (inline futex-wait futex-wake))
165 (sb!alien:define-alien-routine "futex_wait"
166 int (word unsigned-long) (old-value unsigned-long))
168 (sb!alien:define-alien-routine "futex_wake"
169 int (word unsigned-long) (n unsigned-long))))
171 ;;; used by debug-int.lisp to access interrupt contexts
172 #!-(or sb-fluid sb-thread) (declaim (inline sb!vm::current-thread-offset-sap))
173 #!-sb-thread
174 (defun sb!vm::current-thread-offset-sap (n)
175 (declare (type (unsigned-byte 27) n))
176 (sap-ref-sap (alien-sap (extern-alien "all_threads" (* t)))
177 (* n sb!vm:n-word-bytes)))
179 #!+sb-thread
180 (defun sb!vm::current-thread-offset-sap (n)
181 (declare (type (unsigned-byte 27) n))
182 (sb!vm::current-thread-offset-sap n))
184 ;;;; spinlocks
186 (declaim (inline get-spinlock release-spinlock))
188 ;;; The bare 2 here and below are offsets of the slots in the struct.
189 ;;; There ought to be some better way to get these numbers
190 (defun get-spinlock (spinlock)
191 (declare (optimize (speed 3) (safety 0))
192 #!-sb-thread
193 (ignore spinlock new-value))
194 ;; %instance-set-conditional can test for 0 (which is a fixnum) and
195 ;; store any value
196 #!+sb-thread
197 (loop until
198 (eql (sb!vm::%instance-set-conditional spinlock 2 0 1) 0))
201 (defun release-spinlock (spinlock)
202 (declare (optimize (speed 3) (safety 0))
203 #!-sb-thread (ignore spinlock))
204 ;; %instance-set-conditional cannot compare arbitrary objects
205 ;; meaningfully, so
206 ;; (sb!vm::%instance-set-conditional spinlock 2 our-value 0)
207 ;; does not work for bignum thread ids.
208 #!+sb-thread
209 (sb!vm::%instance-set spinlock 2 0))
211 (defmacro with-spinlock ((spinlock) &body body)
212 (sb!int:with-unique-names (lock got-it)
213 `(let ((,lock ,spinlock)
214 (,got-it nil))
215 (unwind-protect
216 (progn
217 (setf ,got-it (get-spinlock ,lock))
218 (locally ,@body))
219 (when ,got-it
220 (release-spinlock ,lock))))))
222 ;;;; mutexes
224 #!+sb-doc
225 (setf (sb!kernel:fdocumentation 'make-mutex 'function)
226 "Create a mutex."
227 (sb!kernel:fdocumentation 'mutex-name 'function)
228 "The name of the mutex. Setfable."
229 (sb!kernel:fdocumentation 'mutex-value 'function)
230 "The value of the mutex. NIL if the mutex is free. Setfable.")
232 #!+(and sb-thread (not sb-lutex))
233 (progn
234 (declaim (inline mutex-value-address))
235 (defun mutex-value-address (mutex)
236 (declare (optimize (speed 3)))
237 (sb!ext:truly-the
238 sb!vm:word
239 (+ (sb!kernel:get-lisp-obj-address mutex)
240 (- (* 3 sb!vm:n-word-bytes) sb!vm:instance-pointer-lowtag)))))
242 (defun get-mutex (mutex &optional (new-value *current-thread*) (wait-p t))
243 #!+sb-doc
244 "Acquire MUTEX, setting it to NEW-VALUE or some suitable default
245 value if NIL. If WAIT-P is non-NIL and the mutex is in use, sleep
246 until it is available"
247 (declare (type mutex mutex) (optimize (speed 3)))
248 (/show0 "Entering GET-MUTEX")
249 (unless new-value
250 (setq new-value *current-thread*))
251 #!-sb-thread
252 (let ((old-value (mutex-value mutex)))
253 (when (and old-value wait-p)
254 (error "In unithread mode, mutex ~S was requested with WAIT-P ~S and ~
255 new-value ~S, but has already been acquired (with value ~S)."
256 mutex wait-p new-value old-value))
257 (setf (mutex-value mutex) new-value)
259 #!+sb-thread
260 (progn
261 (when (eql new-value (mutex-value mutex))
262 (warn "recursive lock attempt ~S~%" mutex)
263 (format *debug-io* "Thread: ~A~%" *current-thread*)
264 (sb!debug:backtrace most-positive-fixnum *debug-io*)
265 (force-output *debug-io*))
266 #!+sb-lutex
267 (when (zerop (with-lutex-address (lutex (mutex-lutex mutex))
268 (if wait-p
269 (%lutex-lock lutex)
270 (%lutex-trylock lutex))))
271 (setf (mutex-value mutex) new-value))
272 #!-sb-lutex
273 (let (old)
274 (loop
275 (unless
276 (setf old (sb!vm::%instance-set-conditional mutex 2 nil
277 new-value))
278 (return t))
279 (unless wait-p (return nil))
280 (with-pinned-objects (mutex old)
281 (futex-wait (mutex-value-address mutex)
282 (sb!kernel:get-lisp-obj-address old)))))))
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)))
299 ;;;; waitqueues/condition variables
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))
310 (defun make-waitqueue (&key name)
311 #!+sb-doc
312 "Create a waitqueue."
313 (%make-waitqueue :name name))
315 #!+sb-doc
316 (setf (sb!kernel:fdocumentation 'waitqueue-name 'function)
317 "The name of the waitqueue. Setfable.")
319 #!+(and sb-thread (not sb-lutex))
320 (progn
321 (declaim (inline waitqueue-data-address))
322 (defun waitqueue-data-address (waitqueue)
323 (declare (optimize (speed 3)))
324 (sb!ext:truly-the
325 sb!vm:word
326 (+ (sb!kernel:get-lisp-obj-address waitqueue)
327 (- (* 3 sb!vm:n-word-bytes) sb!vm:instance-pointer-lowtag)))))
329 (defun condition-wait (queue mutex)
330 #!+sb-doc
331 "Atomically release MUTEX and enqueue ourselves on QUEUE. Another
332 thread may subsequently notify us using CONDITION-NOTIFY, at which
333 time we reacquire MUTEX and return to the caller."
334 #!-sb-thread (declare (ignore queue))
335 (assert mutex)
336 #!-sb-thread (error "Not supported in unithread builds.")
337 #!+sb-thread
338 (let ((value (mutex-value mutex)))
339 (/show0 "CONDITION-WAITing")
340 #!+sb-lutex
341 (progn
342 (setf (mutex-value mutex) nil)
343 (with-lutex-address (queue-lutex-address (waitqueue-lutex queue))
344 (with-lutex-address (mutex-lutex-address (mutex-lutex mutex))
345 (%lutex-wait queue-lutex-address mutex-lutex-address)))
346 (setf (mutex-value mutex) value))
347 #!-sb-lutex
348 (unwind-protect
349 (let ((me *current-thread*))
350 ;; XXX we should do something to ensure that the result of this setf
351 ;; is visible to all CPUs
352 (setf (waitqueue-data queue) me)
353 (release-mutex mutex)
354 ;; Now we go to sleep using futex-wait. If anyone else
355 ;; manages to grab MUTEX and call CONDITION-NOTIFY during
356 ;; this comment, it will change queue->data, and so
357 ;; futex-wait returns immediately instead of sleeping.
358 ;; Ergo, no lost wakeup
359 (with-pinned-objects (queue me)
360 (futex-wait (waitqueue-data-address queue)
361 (sb!kernel:get-lisp-obj-address me))))
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))))
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))))))
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)))
402 ;;;; semaphores
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)))
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))
417 (setf (sb!kernel:fdocumentation 'semaphore-name 'function)
418 "The name of the semaphore. Setfable.")
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)))))
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))))
440 ;;;; job control, independent listeners
442 (defstruct session
443 (lock (make-mutex :name "session lock"))
444 (threads nil)
445 (interactive-threads nil)
446 (interactive-threads-queue (make-waitqueue)))
448 (defvar *session* nil)
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)))
462 (defun new-session ()
463 (make-session :threads (list *current-thread*)
464 :interactive-threads (list *current-thread*)))
466 (defun init-job-control ()
467 (/show0 "Entering INIT-JOB-CONTROL")
468 (setf *session* (new-session))
469 (/show0 "Exiting INIT-JOB-CONTROL"))
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)))))
478 (defun call-with-new-session (fn)
479 (%delete-thread-from-session *current-thread* *session*)
480 (let ((*session* (new-session)))
481 (funcall fn)))
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)))))
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 (when (thread-interruptions-lock thread)
505 (/show0 "FREEING MUTEX LUTEX")
506 (with-lutex-address (lutex (mutex-lutex (thread-interruptions-lock thread)))
507 (%lutex-destroy lutex))))
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 ()))))))
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)))
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*)))))))
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*))))
578 (defun foreground-thread ()
579 (car (session-interactive-threads *session*)))
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))))
608 ;;;; the beef
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.
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 (sb!kernel::*restart-clusters* nil)
637 (sb!kernel::*handler-clusters* nil)
638 (sb!kernel::*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 (sb!kernel: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))))
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)))))
697 #!+sb-doc
698 (setf (sb!kernel:fdocumentation 'join-thread-error-thread 'function)
699 "The thread that we failed to join.")
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)
712 (error 'join-thread-error :thread thread)))))
714 (defun destroy-thread (thread)
715 #!+sb-doc
716 "Deprecated. Same as TERMINATE-THREAD."
717 (terminate-thread thread))
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)))))
727 #!+sb-doc
728 (setf (sb!kernel:fdocumentation 'interrupt-thread-error-thread 'function)
729 "The thread that was not interrupted.")
731 (defmacro with-interruptions-lock ((thread) &body body)
732 `(without-interrupts
733 (with-mutex ((thread-interruptions-lock ,thread))
734 ,@body)))
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))))))
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))
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)))))))
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))
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)))))))
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 (sb!kernel:make-lisp-obj tl-val))))
810 (defun sb!vm::locked-symbol-global-value-add (symbol-name delta)
811 (sb!vm::locked-symbol-global-value-add symbol-name delta))
813 ;;; Stepping
815 (defun thread-stepping ()
816 (sb!kernel:make-lisp-obj
817 (sap-ref-word (current-thread-sap)
818 (* sb!vm::thread-stepping-slot sb!vm:n-word-bytes))))
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 (sb!kernel:get-lisp-obj-address value)))