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