1 ;;;; support for threads in the target machine
3 ;;;; This software is part of the SBCL system. See the README file for
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")
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
21 (defmacro with-cas-lock ((place) &body body)
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.
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
33 WITH-CAS-LOCK can be entered recursively."
35 (%with-cas-lock (,place) ,@body)))
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)
45 (,self *current-thread*)
50 (unless (eq ,owner ,self)
51 (loop until (loop repeat 100
55 (not (setf ,owner ,cas-form)))
58 do (sb!ext:spin-loop-hint))
61 (unless (eq ,owner ,self)
64 (unless (eq ,old ,cas-form)
65 (bug "Failed to release CAS lock!")))))))))
69 (define-condition thread-error (error)
70 ((thread :reader thread-error-thread :initarg :thread))
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."))
77 (define-condition simple-thread-error (thread-error simple-condition)
80 (define-condition thread-deadlock (thread-error)
81 ((cycle :initarg :cycle :reader thread-deadlock-cycle))
83 (lambda (condition stream)
84 (let* ((*print-circle* t)
85 (cycle (thread-deadlock-cycle condition))
87 (format stream "Deadlock cycle detected:~%")
88 (loop for part = (pop cycle)
90 do (format stream " ~S~% waited for:~% ~S~% owned by:~%"
93 (format stream " ~S~%" start)))))
97 (fdocumentation 'thread-error-thread 'function)
98 "Return the offending thread that the THREAD-ERROR pertains to.")
100 (define-condition symbol-value-in-thread-error (cell-error thread-error)
101 ((info :reader symbol-value-in-thread-error-info :initarg :info))
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"
108 (cell-error-name condition)
109 (thread-error-thread condition)
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."))))))
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."))
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)
127 (format s "Joining thread failed: thread ~A ~
128 did not return normally."
129 (thread-error-thread c)))
131 (format s "Joining thread timed out: thread ~A ~
132 did not exit in time."
133 (thread-error-thread c))))))
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."))
140 (define-deprecated-function :late "1.0.29.17" join-thread-error-thread thread-error-thread
142 (thread-error-thread condition))
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))))
150 "Signalled when interrupting a thread fails because the thread has already
151 exited. The offending thread can be accessed using THREAD-ERROR-THREAD."))
153 (define-deprecated-function :late "1.0.29.17" interrupt-thread-error-thread thread-error-thread
155 (thread-error-thread condition))
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.
162 ;;; set the doc here because in early-thread FDOCUMENTATION is not
165 (setf (fdocumentation '*current-thread* 'variable)
166 "Bound in each thread to the thread itself.")
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.")
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)
180 (join-thread thread :default cookie))))
181 (state (if (eq :running info)
184 (thread-waiting-for thread))))
187 (list "waiting on:" (cdr thing)
188 "timeout: " (car thing)))
192 (list "waiting on:" thing))))
193 (if (eq cookie (car info))
196 (values (when (eq :finished state)
200 "~@[~S ~]~:[~{~I~A~^~2I~_ ~}~_~;~A~:[ no values~; values: ~:*~{~S~^, ~}~]~]"
206 (defun print-lock (lock name owner stream)
207 (let ((*print-circle* t))
208 (print-unreadable-object (lock stream :type t :identity (not name))
210 (format stream "~@[~S ~]~2I~_owner: ~S" name owner)
211 (format stream "~@[~S ~](free)" name)))))
213 (def!method print-object ((mutex mutex) stream)
214 (print-lock mutex (mutex-name mutex) (mutex-owner mutex) stream))
216 (defun thread-alive-p (thread)
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
221 (thread-%alive-p thread))
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"))
229 (defvar *default-alloc-signal* nil)
231 (defmacro with-all-threads-lock (&body body)
232 `(with-system-mutex (*all-threads-lock*)
235 (defun list-all-threads ()
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*)))
243 (declaim (inline current-thread-sap))
244 (defun current-thread-sap ()
245 (sb!vm::current-thread-offset-sap sb!vm::thread-this-slot))
247 (declaim (inline current-thread-os-thread))
248 (defun current-thread-os-thread ()
250 (sap-int (sb!vm::current-thread-offset-sap sb!vm::thread-os-thread-slot))
254 (defvar *initial-thread* nil)
255 (defvar *make-thread-lock*)
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"
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))))
272 (defun main-thread ()
273 "Returns the main thread of the process."
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*))
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.
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.
290 See also: ABORT-THREAD and SB-EXT:EXIT."
291 `(%return-from-thread (multiple-value-list ,values-form) ,allow-exit))
293 (defun %return-from-thread (values allow-exit)
294 (let ((self *current-thread*))
295 (cond ((main-thread-p self)
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)
302 (sb!ext:exit :code 0))
304 (throw '%return-from-thread (values-list values))))))
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.
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.
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.)
324 See also: RETURN-FROM-THREAD and SB-EXT:EXIT."
325 (let ((self *current-thread*))
326 (cond ((main-thread-p self)
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))
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)))))
338 ;;;; Aliens, low level stuff
340 (define-alien-routine "kill_safely"
342 (os-thread #!-alpha unsigned-long #!+alpha unsigned-int)
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
351 (define-alien-routine ("create_thread" %create-thread)
352 unsigned-long (lisp-fun-address unsigned-long))
354 (declaim (inline %block-deferrable-signals))
355 (define-alien-routine ("block_deferrable_signals" %block-deferrable-signals)
357 (where sb!alien:unsigned-long)
358 (old sb!alien:unsigned-long))
360 (defun block-deferrable-signals ()
361 (%block-deferrable-signals 0 0))
365 (declaim (inline futex-wait %futex-wait futex-wake))
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))
371 (defun futex-wait (word old to-sec to-usec)
373 (%futex-wait word old to-sec to-usec)))
375 (define-alien-routine "futex_wake"
376 int (word unsigned-long) (n unsigned-long))))
378 ;;; used by debug-int.lisp to access interrupt contexts
379 #!-(or sb-fluid sb-thread) (declaim (inline sb!vm::current-thread-offset-sap))
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)))
387 (defun sb!vm::current-thread-offset-sap (n)
388 (declare (type (unsigned-byte 27) n))
389 (sb!vm::current-thread-offset-sap n))
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)
396 (,n-timeout ,(when timeoutp
398 (when sb!impl::*deadline*
399 sb!impl::*deadline-seconds*))))
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)
405 (declare (dynamic-extent ,new))
406 ;; No WITHOUT-INTERRUPTS, since WITH-DEADLOCKS is used
407 ;; in places where interrupts should already be disabled.
410 (setf (thread-waiting-for ,n-thread) ,new)
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))))))
421 (setf (fdocumentation 'make-mutex 'function)
423 (fdocumentation 'mutex-name 'function)
424 "The name of the mutex. Setfable.")
426 #!+(and sb-thread sb-futex)
428 (define-structure-slot-addressor mutex-state-address
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))
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
443 ;; Make sure to get the current value.
444 (sb!ext:compare-and-swap (mutex-%owner mutex) nil nil))
446 (sb!ext:defglobal **deadlock-lock** nil)
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*)
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)
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*
478 (let ((other-lock (progn
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
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))
496 ;; There's a timeout -- no deadlock.
497 (return-from check-deadlock nil))
498 ((waitqueue-p other-lock)
500 (return-from check-deadlock nil))
501 ((eq self other-thread)
503 (list (list thread 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)
515 (defun %try-mutex (mutex new-owner)
516 (declare (type mutex mutex) (optimize (speed 3)))
518 (let ((old (mutex-%owner mutex)))
519 (when (eq new-owner old)
520 (error "Recursive lock attempt ~S." mutex))
523 (error "Strange deadlock on ~S in an unithreaded build?" mutex))
524 #!-(and sb-thread sb-futex)
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)
533 (let ((prev (sb!ext:compare-and-swap (mutex-%owner mutex) nil new-owner)))
535 (bug "Old owner in free mutex: ~S" prev))
539 (defun %%wait-for-mutex (mutex new-owner to-sec to-usec stop-sec stop-usec)
540 (declare (type mutex mutex) (optimize (speed 3)))
542 (declare (ignore to-sec to-usec))
548 (not (mutex-%owner mutex)))
549 (not (sb!ext:compare-and-swap (mutex-%owner mutex) nil
551 do (return-from cas t)
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))
560 ;; This is a fairly direct translation of the Mutex 2 algorithm from
561 ;; "Futexes are Tricky" by Ulrich Drepper.
563 (when (eql +lock-free+ old)
564 (let ((prev (sb!ext:compare-and-swap (mutex-%owner mutex)
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?
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+)
584 ;; -1 = EWOULDBLOCK, possibly spurious 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.
591 (sb!ext:compare-and-swap (mutex-state mutex) +lock-free+ +lock-contested+))
592 ;; Update timeout if necessary.
594 (setf (values to-sec to-usec)
595 (sb!impl::relative-decoded-times stop-sec stop-usec)))
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))
605 (return-from %wait-for-mutex
606 (or (%%wait-for-mutex mutex self to-sec to-usec stop-sec stop-usec)
609 ;; FIXME: substract elapsed time from timeout...
610 (setf (values to-sec to-usec stop-sec stop-usec deadlinep)
611 (decode-timeout timeout))
614 (defun get-mutex (mutex &optional new-owner (waitp t) (timeout nil))
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)
622 (multiple-value-call #'%wait-for-mutex
623 mutex new-owner timeout (decode-timeout timeout))))))
625 (defun grab-mutex (mutex &key (waitp t) (timeout nil))
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.
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.
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.
639 - GRAB-MUTEX is not interrupt safe. The correct way to call it is:
643 (ALLOW-WITH-INTERRUPTS (GRAB-MUTEX ...))
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.
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
655 The exact interplay of GRAB-MUTEX and deadlines are reserved to change in
658 - It is recommended that you use WITH-MUTEX instead of calling GRAB-MUTEX
661 (declare (ignorable waitp timeout))
662 (let ((self *current-thread*))
663 (or (%try-mutex mutex self)
666 (multiple-value-call #'%wait-for-mutex
667 mutex self timeout (decode-timeout timeout))))))
669 (defun release-mutex (mutex &key (if-not-owner :punt))
671 "Release MUTEX by setting it to NIL. Wake up threads waiting for
674 RELEASE-MUTEX is not interrupt safe: interrupts should be disabled
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)
687 ((:punt) (return-from release-mutex nil))
689 (warn "Releasing ~S, owned by another thread: ~S" mutex old-owner))
691 (setf (mutex-%owner mutex) nil)
692 ;; FIXME: Is a :memory barrier too strong here? Can we use a :write
695 #!+(and sb-thread sb-futex)
697 ;; FIXME: once ATOMIC-INCF supports struct slots with word sized
698 ;; unsigned-byte type this can be used:
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))))
715 ;;;; Waitqueues/condition variables
717 #!+(or (not sb-thread) sb-futex)
718 (defstruct (waitqueue (:constructor %make-waitqueue))
721 (name nil :type (or null thread-name))
722 #!+(and sb-thread sb-futex)
725 #!+(and sb-thread (not sb-futex))
727 (defstruct (waitqueue (:constructor %make-waitqueue))
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.
740 (defun %waitqueue-enqueue (thread queue)
741 (setf (thread-waiting-for thread) queue)
742 (let ((head (waitqueue-%head queue))
743 (tail (waitqueue-%tail queue))
746 (setf (waitqueue-%head queue) new))
748 (setf (cdr tail) new))
749 (setf (waitqueue-%tail queue) new)
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))
757 (eq (car list) thread))
759 (let ((rest (cdr list)))
761 (setf (cdr prev) rest))
763 (setf (waitqueue-%head queue) rest
766 (setf (waitqueue-%tail queue) prev)))))))
768 (defun %waitqueue-wakeup (queue n)
770 (loop while (plusp n)
771 for next = (let ((head (waitqueue-%head queue))
772 (tail (waitqueue-%tail queue)))
775 (setf (waitqueue-%head queue) nil
776 (waitqueue-%tail queue) nil)
777 (setf (waitqueue-%head queue) (cdr head)))
780 do (when (eq queue (sb!ext:compare-and-swap
781 (thread-waiting-for next) queue nil))
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))))
789 (defun make-waitqueue (&key name)
791 "Create a waitqueue."
792 (%make-waitqueue :name name))
795 (setf (fdocumentation 'waitqueue-name 'function)
796 "The name of the waitqueue. Setfable.")
798 #!+(and sb-thread sb-futex)
799 (define-structure-slot-addressor waitqueue-token-address
803 (defun condition-wait (queue mutex &key timeout)
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.
809 Spurious wakeups are possible.
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.
816 If CONDITION-WAIT unwinds, it may do so with or without the mutex being held.
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:
823 (defvar *queue* (make-waitqueue))
824 (defvar *lock* (make-mutex))
827 (defun pop-data (&optional timeout)
830 do (or (condition-wait *queue* *lock* :timeout timeout)
831 ;; Lock not held, must unwind without touching *data*.
832 (return-from pop-data nil)))
836 (defun push-data (data)
839 (condition-notify *queue*)))
842 (declare (ignore queue))
845 (sb!ext:wait-for nil :timeout timeout) ; Yeah...
847 (let ((me *current-thread*))
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.
860 (%with-cas-lock ((waitqueue-%owner queue))
861 (%waitqueue-enqueue me queue))
862 (release-mutex mutex)
864 (or (flet ((wakeup ()
866 (unless (eq queue (thread-waiting-for me))
868 (declare (dynamic-extent #'wakeup))
869 (allow-with-interrupts
870 (sb!impl::%%wait-for #'wakeup stop-sec stop-usec)))
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.
882 (case (allow-with-interrupts
883 (futex-wait (waitqueue-token-address queue)
884 (get-lisp-obj-address me)
885 ;; our way of saying "no
893 ;; -1 = EWOULDBLOCK, possibly spurious wakeup
895 ;; 2 = EINTR, a spurious wakeup
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
917 (return-from condition-wait 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
929 stop-sec stop-usec deadlinep)))
930 (setf status :timeout)))))
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)))))))
937 (defun condition-notify (queue &optional (n 1))
939 "Notify N threads waiting on QUEUE.
941 IMPORTANT: The same mutex that is used in the corresponding CONDITION-WAIT
942 must be held by this thread during this call."
944 (declare (ignore queue n))
946 (error "Not supported in unithread builds.")
948 (declare (type (and fixnum (integer 1)) n))
949 (/show0 "Entering CONDITION-NOTIFY")
953 (with-cas-lock ((waitqueue-%owner queue))
954 (%waitqueue-wakeup queue n))
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.
961 ;; XXX we should do something to ensure that the result of this setf
962 ;; is visible to all CPUs.
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)))))
969 (defun condition-broadcast (queue)
971 "Notify all threads waiting on QUEUE.
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.
979 most-positive-fixnum)))
984 (defstruct (semaphore (:constructor %make-semaphore (name %count)))
986 "Semaphore type. The fact that a SEMAPHORE is a STRUCTURE-OBJECT
987 should be considered an implementation detail, and may change in the
989 (name nil :type (or null thread-name))
990 (%count 0 :type (integer 0))
991 (waitcount 0 :type sb!vm:word)
993 (queue (make-waitqueue)))
995 (setf (fdocumentation 'semaphore-name 'function)
996 "The name of the semaphore INSTANCE. Setfable.")
998 (defstruct (semaphore-notification (:constructor make-semaphore-notification ())
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))
1006 (setf (fdocumentation 'make-semaphore-notification 'function)
1007 "Constructor for SEMAPHORE-NOTIFICATION objects. SEMAPHORE-NOTIFICATION-STATUS
1010 (declaim (inline semaphore-notification-status))
1011 (defun semaphore-notification-status (semaphore-notification)
1013 "Returns T if a WAIT-ON-SEMAPHORE or TRY-SEMAPHORE using
1014 SEMAPHORE-NOTICATION has succeeded since the notification object was created
1017 (semaphore-notification-%status semaphore-notification))
1019 (declaim (inline clear-semaphore-notification))
1020 (defun clear-semaphore-notification (semaphore-notification)
1022 "Resets the SEMAPHORE-NOTIFICATION object for use with another call to
1023 WAIT-ON-SEMAPHORE or TRY-SEMAPHORE."
1025 (setf (semaphore-notification-%status semaphore-notification) nil)))
1027 (declaim (inline semaphore-count))
1028 (defun semaphore-count (instance)
1030 "Returns the current count of the semaphore INSTANCE."
1032 (semaphore-%count instance))
1034 (defun make-semaphore (&key name (count 0))
1036 "Create a semaphore with the supplied COUNT and NAME."
1037 (%make-semaphore name count))
1039 (defun wait-on-semaphore (semaphore &key timeout notification)
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.
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
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
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.)
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))
1070 (setf (semaphore-notification-%status notification) t)))
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)
1084 (return-from wait-on-semaphore nil)))
1085 (setf (semaphore-%count semaphore) (1- count))
1087 (setf (semaphore-notification-%status notification) t)))
1088 ;; Need to use ATOMIC-DECF as we may unwind without the lock
1090 (sb!ext:atomic-decf (semaphore-waitcount semaphore)))))))
1093 (defun try-semaphore (semaphore &optional (n 1) notification)
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.
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)
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?
1117 (defun signal-semaphore (semaphore &optional (n 1))
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))))))
1131 ;;;; Job control, independent listeners
1134 (lock (make-mutex :name "session lock"))
1136 (interactive-threads nil)
1137 (interactive-threads-queue (make-waitqueue)))
1139 (defvar *session* nil)
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.
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.)
1151 ;;; FIXME: It would be good to have ordered locks to ensure invariants
1153 (defmacro with-session-lock ((session) &body body)
1154 `(with-system-mutex ((session-lock ,session) :allow-with-interrupts t)
1157 (defun new-session ()
1158 (make-session :threads (list *current-thread*)
1159 :interactive-threads (list *current-thread*)))
1161 (defun init-job-control ()
1162 (/show0 "Entering INIT-JOB-CONTROL")
1163 (setf *session* (new-session))
1164 (/show0 "Exiting INIT-JOB-CONTROL"))
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)))))
1173 (defun call-with-new-session (fn)
1174 (%delete-thread-from-session *current-thread* *session*)
1175 (let ((*session* (new-session)))
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)))))
1184 ;;; Remove thread from its session, if it has one.
1186 (defun handle-thread-exit (thread)
1187 (/show0 "HANDLING THREAD EXIT")
1188 (when *exit-in-process*
1189 (if (consp *exit-in-process*)
1190 ;; This means we're the main thread, but someone else
1191 ;; requested the exit and exiting with the right code is the
1192 ;; only thing left to do.
1193 (os-exit (car *exit-in-process*) :abort nil)
1195 ;; Lisp-side cleanup
1196 (with-all-threads-lock
1197 (setf (thread-%alive-p thread) nil)
1198 (setf (thread-os-thread thread) nil)
1199 (setq *all-threads* (delete thread *all-threads*))
1201 (%delete-thread-from-session thread *session*))))
1203 (defun %exit-other-threads ()
1204 ;; Grabbing this lock prevents new threads from
1205 ;; being spawned, and guarantees that *ALL-THREADS*
1207 (with-deadline (:seconds nil :override t)
1208 (grab-mutex *make-thread-lock*)
1209 (let ((timeout sb!ext:*exit-timeout*)
1210 (code *exit-in-process*)
1213 (dolist (thread (list-all-threads))
1214 (cond ((eq thread *current-thread*))
1215 ((main-thread-p thread)
1220 (terminate-thread thread)
1221 (push thread joinees))
1222 (interrupt-thread-error ())))))
1223 (dolist (thread (nreverse joinees))
1224 (join-thread thread :default t :timeout timeout))
1225 ;; Need to defer till others have joined, because when main
1226 ;; thread exits, we're gone. Can't use TERMINATE-THREAD -- would
1227 ;; get the exit code wrong.
1233 (setf *exit-in-process* (list code))
1234 (throw 'sb!impl::%end-of-the-world t)))
1235 (interrupt-thread-error ()))
1236 ;; Normally this never finishes, as once the main-thread
1237 ;; unwinds we exit with the right code, but if times out
1238 ;; before that happens, we will exit after returning.
1239 (join-thread main :default t :timeout timeout)))))
1241 (defun terminate-session ()
1243 "Kill all threads in session except for this one. Does nothing if current
1244 thread is not the foreground thread."
1245 ;; FIXME: threads created in other threads may escape termination
1247 (with-session-lock (*session*)
1248 (and (eq *current-thread*
1249 (car (session-interactive-threads *session*)))
1250 (session-threads *session*)))))
1251 ;; do the kill after dropping the mutex; unwind forms in dying
1252 ;; threads may want to do session things
1253 (dolist (thread to-kill)
1254 (unless (eq thread *current-thread*)
1255 ;; terminate the thread but don't be surprised if it has
1256 ;; exited in the meantime
1257 (handler-case (terminate-thread thread)
1258 (interrupt-thread-error ()))))))
1260 ;;; called from top of invoke-debugger
1261 (defun debugger-wait-until-foreground-thread (stream)
1262 "Returns T if thread had been running in background, NIL if it was
1264 (declare (ignore stream))
1268 (with-session-lock (*session*)
1269 (not (member *current-thread*
1270 (session-interactive-threads *session*))))
1273 (defun get-foreground ()
1276 (let ((was-foreground t))
1278 (/show0 "Looping in GET-FOREGROUND")
1279 (with-session-lock (*session*)
1280 (let ((int-t (session-interactive-threads *session*)))
1281 (when (eq (car int-t) *current-thread*)
1282 (unless was-foreground
1283 (format *query-io* "Resuming thread ~A~%" *current-thread*))
1284 (return-from get-foreground t))
1285 (setf was-foreground nil)
1286 (unless (member *current-thread* int-t)
1287 (setf (cdr (last int-t))
1288 (list *current-thread*)))
1290 (session-interactive-threads-queue *session*)
1291 (session-lock *session*)))))))
1293 (defun release-foreground (&optional next)
1295 "Background this thread. If NEXT is supplied, arrange for it to
1296 have the foreground next."
1297 #!-sb-thread (declare (ignore next))
1300 (with-session-lock (*session*)
1301 (when (rest (session-interactive-threads *session*))
1302 (setf (session-interactive-threads *session*)
1303 (delete *current-thread* (session-interactive-threads *session*))))
1305 (setf (session-interactive-threads *session*)
1307 (delete next (session-interactive-threads *session*)))))
1308 (condition-broadcast (session-interactive-threads-queue *session*))))
1310 (defun foreground-thread ()
1311 (car (session-interactive-threads *session*)))
1313 (defun make-listener-thread (tty-name)
1314 (assert (probe-file tty-name))
1315 (let* ((in (sb!unix:unix-open (namestring tty-name) sb!unix:o_rdwr #o666))
1316 (out (sb!unix:unix-dup in))
1317 (err (sb!unix:unix-dup in)))
1318 (labels ((thread-repl ()
1319 (sb!unix::unix-setsid)
1320 (let* ((sb!impl::*stdin*
1321 (make-fd-stream in :input t :buffering :line
1324 (make-fd-stream out :output t :buffering :line
1327 (make-fd-stream err :output t :buffering :line
1330 (make-fd-stream err :input t :output t
1333 (sb!impl::*descriptor-handlers* nil))
1334 (with-new-session ()
1336 (sb!impl::toplevel-repl nil)
1337 (sb!int:flush-standard-output-streams))))))
1338 (make-thread #'thread-repl))))
1343 (defun make-thread (function &key name arguments)
1345 "Create a new thread of NAME that runs FUNCTION with the argument
1346 list designator provided (defaults to no argument). Thread exits when
1347 the function returns. The return values of FUNCTION are kept around
1348 and can be retrieved by JOIN-THREAD.
1350 Invoking the initial ABORT restart estabilished by MAKE-THREAD
1351 terminates the thread.
1353 See also: RETURN-FROM-THREAD, ABORT-THREAD."
1354 #!-sb-thread (declare (ignore function name arguments))
1355 #!-sb-thread (error "Not supported in unithread builds.")
1356 #!+sb-thread (assert (or (atom arguments)
1357 (null (cdr (last arguments))))
1359 "Argument passed to ~S, ~S, is an improper list."
1360 'make-thread arguments)
1363 (with-mutex (*make-thread-lock*)
1364 (let* ((thread (%make-thread :name name))
1365 (setup-sem (make-semaphore :name "Thread setup semaphore"))
1366 (real-function (coerce function 'function))
1367 (arguments (if (listp arguments)
1371 (named-lambda initial-thread-function ()
1372 ;; In time we'll move some of the binding presently done in C
1375 ;; KLUDGE: Here we have a magic list of variables that are
1376 ;; not thread-safe for one reason or another. As people
1377 ;; report problems with the thread safety of certain
1378 ;; variables, (e.g. "*print-case* in multiple threads
1379 ;; broken", sbcl-devel 2006-07-14), we add a few more
1380 ;; bindings here. The Right Thing is probably some variant
1381 ;; of Allegro's *cl-default-special-bindings*, as that is at
1382 ;; least accessible to users to secure their own libraries.
1383 ;; --njf, 2006-07-15
1385 ;; As it is, this lambda must not cons until we are ready
1386 ;; to run GC. Be very careful.
1387 (let* ((*current-thread* thread)
1388 (*restart-clusters* nil)
1389 (*handler-clusters* (sb!kernel::initial-handler-clusters))
1390 (*condition-restarts* nil)
1391 (*exit-in-process* nil)
1392 (sb!impl::*deadline* nil)
1393 (sb!impl::*deadline-seconds* nil)
1394 (sb!impl::*step-out* nil)
1395 ;; internal printer variables
1396 (sb!impl::*previous-case* nil)
1397 (sb!impl::*previous-readtable-case* nil)
1398 (sb!impl::*internal-symbol-output-fun* nil)
1399 (sb!impl::*descriptor-handlers* nil)) ; serve-event
1401 (setf sb!vm:*alloc-signal* *default-alloc-signal*)
1402 (setf (thread-os-thread thread) (current-thread-os-thread))
1403 (with-mutex ((thread-result-lock thread))
1404 (with-all-threads-lock
1405 (push thread *all-threads*))
1406 (with-session-lock (*session*)
1407 (push thread (session-threads *session*)))
1408 (setf (thread-%alive-p thread) t)
1409 (signal-semaphore setup-sem)
1410 ;; Using handling-end-of-the-world would be a bit tricky
1411 ;; due to other catches and interrupts, so we essentially
1412 ;; re-implement it here. Once and only once more.
1413 (catch 'sb!impl::toplevel-catcher
1414 (catch 'sb!impl::%end-of-the-world
1415 (catch '%abort-thread
1416 (with-simple-restart
1417 (abort "~@<Abort thread (~A)~@:>" *current-thread*)
1420 (with-local-interrupts
1421 (sb!unix::unblock-deferrable-signals)
1422 (setf (thread-result thread)
1424 (multiple-value-list
1426 (catch '%return-from-thread
1427 (apply real-function arguments))
1428 (when *exit-in-process*
1429 (sb!impl::call-exit-hooks)))))))
1430 ;; We're going down, can't handle interrupts
1431 ;; sanely anymore. GC remains enabled.
1432 (block-deferrable-signals)
1433 ;; We don't want to run interrupts in a dead
1434 ;; thread when we leave WITHOUT-INTERRUPTS.
1435 ;; This potentially causes important
1436 ;; interupts to be lost: SIGINT comes to
1438 (setq *interrupt-pending* nil)
1439 (handle-thread-exit thread)))))))))
1441 ;; If the starting thread is stopped for gc before it signals the
1442 ;; semaphore then we'd be stuck.
1443 (assert (not *gc-inhibit*))
1444 ;; Keep INITIAL-FUNCTION pinned until the child thread is
1445 ;; initialized properly. Wrap the whole thing in
1446 ;; WITHOUT-INTERRUPTS because we pass INITIAL-FUNCTION to another
1449 (with-pinned-objects (initial-function)
1452 (get-lisp-obj-address initial-function))))
1453 (when (zerop os-thread)
1455 (wait-on-semaphore setup-sem)
1456 (return-from make-thread thread))))))
1458 (error "Could not create a new thread.")))
1460 (defun join-thread (thread &key (default nil defaultp) timeout)
1462 "Suspend current thread until THREAD exits. Return the result values
1463 of the thread function.
1465 If the thread does not exit normally within TIMEOUT seconds return
1466 DEFAULT if given, or else signal JOIN-THREAD-ERROR.
1468 Trying to join the main thread will cause JOIN-THREAD to block until
1469 TIMEOUT occurs or the process exits: when main thread exits, the
1470 entire process exits.
1472 NOTE: Return convention in case of a timeout is exprimental and
1474 (let ((lock (thread-result-lock thread))
1480 (allow-with-interrupts
1481 ;; Don't use the timeout if the thread is not alive anymore.
1482 (grab-mutex lock :timeout (and (thread-alive-p thread) timeout))))
1483 (cond ((car (thread-result thread))
1484 (return-from join-thread
1485 (values-list (cdr (thread-result thread)))))
1487 (return-from join-thread default))
1489 (setf problem :abort)))
1491 (return-from join-thread default)))
1493 (release-mutex lock))))
1494 (error 'join-thread-error :thread thread :problem problem)))
1496 (defun destroy-thread (thread)
1498 "Deprecated. Same as TERMINATE-THREAD."
1499 (terminate-thread thread))
1501 (defmacro with-interruptions-lock ((thread) &body body)
1502 `(with-system-mutex ((thread-interruptions-lock ,thread))
1505 ;;; Called from the signal handler.
1507 (defun run-interruption ()
1508 (let ((interruption (with-interruptions-lock (*current-thread*)
1509 (pop (thread-interruptions *current-thread*)))))
1510 ;; If there is more to do, then resignal and let the normal
1511 ;; interrupt deferral mechanism take care of the rest. From the
1512 ;; OS's point of view the signal we are in the handler for is no
1513 ;; longer pending, so the signal will not be lost.
1514 (when (thread-interruptions *current-thread*)
1515 (kill-safely (thread-os-thread *current-thread*) sb!unix:sigpipe))
1517 (funcall interruption))))
1519 (defun interrupt-thread (thread function)
1521 "Interrupt THREAD and make it run FUNCTION.
1523 The interrupt is asynchronous, and can occur anywhere with the exception of
1524 sections protected using SB-SYS:WITHOUT-INTERRUPTS.
1526 FUNCTION is called with interrupts disabled, under
1527 SB-SYS:ALLOW-WITH-INTERRUPTS. Since functions such as GRAB-MUTEX may try to
1528 enable interrupts internally, in most cases FUNCTION should either enter
1529 SB-SYS:WITH-INTERRUPTS to allow nested interrupts, or
1530 SB-SYS:WITHOUT-INTERRUPTS to prevent them completely.
1532 When a thread receives multiple interrupts, they are executed in the order
1533 they were sent -- first in, first out.
1535 This means that a great degree of care is required to use INTERRUPT-THREAD
1536 safely and sanely in a production environment. The general recommendation is
1537 to limit uses of INTERRUPT-THREAD for interactive debugging, banning it
1538 entirely from production environments -- it is simply exceedingly hard to use
1541 With those caveats in mind, what you need to know when using it:
1543 * If calling FUNCTION causes a non-local transfer of control (ie. an
1544 unwind), all normal cleanup forms will be executed.
1546 However, if the interrupt occurs during cleanup forms of an UNWIND-PROTECT,
1547 it is just as if that had happened due to a regular GO, THROW, or
1548 RETURN-FROM: the interrupted cleanup form and those following it in the
1549 same UNWIND-PROTECT do not get executed.
1551 SBCL tries to keep its own internals asynch-unwind-safe, but this is
1552 frankly an unreasonable expectation for third party libraries, especially
1553 given that asynch-unwind-safety does not compose: a function calling
1554 only asynch-unwind-safe function isn't automatically asynch-unwind-safe.
1556 This means that in order for an asych unwind to be safe, the entire
1557 callstack at the point of interruption needs to be asynch-unwind-safe.
1559 * In addition to asynch-unwind-safety you must consider the issue of
1560 re-entrancy. INTERRUPT-THREAD can cause function that are never normally
1561 called recursively to be re-entered during their dynamic contour,
1562 which may cause them to misbehave. (Consider binding of special variables,
1563 values of global variables, etc.)
1565 Take togather, these two restrict the \"safe\" things to do using
1566 INTERRUPT-THREAD to a fairly minimal set. One useful one -- exclusively for
1567 interactive development use is using it to force entry to debugger to inspect
1568 the state of a thread:
1570 (interrupt-thread thread #'break)
1572 Short version: be careful out there."
1574 (declare (ignore thread))
1576 (with-interrupt-bindings
1577 (with-interrupts (funcall function)))
1579 (let ((os-thread (thread-os-thread thread)))
1580 (cond ((not os-thread)
1581 (error 'interrupt-thread-error :thread thread))
1583 (with-interruptions-lock (thread)
1584 ;; Append to the end of the interruptions queue. It's
1585 ;; O(N), but it does not hurt to slow interruptors down a
1586 ;; bit when the queue gets long.
1587 (setf (thread-interruptions thread)
1588 (append (thread-interruptions thread)
1591 (allow-with-interrupts
1592 (funcall function))))))))
1593 (when (minusp (kill-safely os-thread sb!unix:sigpipe))
1594 (error 'interrupt-thread-error :thread thread))))))
1596 (defun terminate-thread (thread)
1598 "Terminate the thread identified by THREAD, by interrupting it and
1599 causing it to call SB-EXT:ABORT-THREAD with :ALLOW-EXIT T.
1601 The unwind caused by TERMINATE-THREAD is asynchronous, meaning that
1602 eg. thread executing
1607 (setf foo (get-foo))
1610 ;; An interrupt occurring inside the cleanup clause
1611 ;; will cause cleanups from the current UNWIND-PROTECT
1613 (release-foo foo))))
1615 might miss calling RELEASE-FOO despite GET-FOO having returned true if
1616 the interrupt occurs inside the cleanup clause, eg. during execution
1619 Thus, in order to write an asynch unwind safe UNWIND-PROTECT you need
1620 to use WITHOUT-INTERRUPTS:
1623 (sb-sys:without-interrupts
1626 (setf foo (sb-sys:allow-with-interrupts
1628 (sb-sys:with-local-interrupts
1631 (release-foo foo)))))
1633 Since most libraries using UNWIND-PROTECT do not do this, you should never
1634 assume that unknown code can safely be terminated using TERMINATE-THREAD."
1635 (interrupt-thread thread (lambda () (abort-thread :allow-exit t))))
1637 (define-alien-routine "thread_yield" int)
1640 (setf (fdocumentation 'thread-yield 'function)
1641 "Yield the processor to other threads.")
1643 ;;; internal use only. If you think you need to use these, either you
1644 ;;; are an SBCL developer, are doing something that you should discuss
1645 ;;; with an SBCL developer first, or are doing something that you
1646 ;;; should probably discuss with a professional psychiatrist first
1649 (defun %thread-sap (thread)
1650 (let ((thread-sap (alien-sap (extern-alien "all_threads" (* t))))
1651 (target (thread-os-thread thread)))
1653 (when (sap= thread-sap (int-sap 0)) (return nil))
1654 (let ((os-thread (sap-ref-word thread-sap
1655 (* sb!vm:n-word-bytes
1656 sb!vm::thread-os-thread-slot))))
1657 (when (= os-thread target) (return thread-sap))
1659 (sap-ref-sap thread-sap (* sb!vm:n-word-bytes
1660 sb!vm::thread-next-slot)))))))
1662 (defun %symbol-value-in-thread (symbol thread)
1663 ;; Prevent the thread from dying completely while we look for the TLS
1665 (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 (return (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 (return (values nil :unbound-in-thread)))
1678 (return (values obj :ok)))))
1679 (return (values nil :thread-dead))))))
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
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))
1692 (setf (sap-ref-lispobj (%thread-sap thread) offset)
1694 (values value :ok))))
1695 (values nil :thread-dead)))))
1697 (define-alien-variable tls-index-start unsigned-int)
1699 ;; Get values from the TLS area of the current thread.
1700 (defun %thread-local-references ()
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))
1708 (typep obj '(or fixnum character))
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))))))
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
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.
1725 Can also be used with SETF to change the thread-local value of SYMBOL.
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))
1731 (multiple-value-bind (res status) (%symbol-value-in-thread symbol thread)
1735 (error 'symbol-value-in-thread-error
1738 :info (list :read status))
1742 (values (symbol-value symbol) t)
1744 (error 'symbol-value-in-thread-error
1747 :info (list :read :unbound-in-thread))
1750 (defun (setf symbol-value-in-thread) (value symbol thread &optional (errorp t))
1751 (declare (symbol symbol) (thread thread))
1753 (multiple-value-bind (res status) (%set-symbol-value-in-thread symbol thread value)
1757 (error 'symbol-value-in-thread-error
1760 :info (list :write status))
1764 (values (setf (symbol-value symbol) value) t)
1766 (error 'symbol-value-in-thread-error
1769 :info (list :write :unbound-in-thread))
1772 (defun sb!vm::locked-symbol-global-value-add (symbol-name delta)
1773 (sb!vm::locked-symbol-global-value-add symbol-name delta))
1778 (defun thread-stepping ()
1780 (sap-ref-word (current-thread-sap)
1781 (* sb!vm::thread-stepping-slot sb!vm:n-word-bytes))))
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)))