anchor.diff:  
   (defmacro with-pseudo-atomic (&body body)
     `(call-with-pseudo-atomic (dx-lambda () ,@body)))
  +
  +;;; Higher-level locks and things.
   
   ;;; Common structure for sleepable things.
   (defstruct (wait-queue
  
positive.code:   (reset-ephemeral-thread sys.int::*disk-io-thread* #'disk-thread :runnable)
    (condition-notify *world-stop-resume-cvar*))
  
  (defun thread-yield ()
    (%call-on-wired-stack-without-interrupts
     (lambda (sp fp)
      (let ((current (current-thread)))
        (%lock-thread current)
        (setf (thread-state current) :runnable)
        (%reschedule-via-wired-stack sp fp)))
     nil))
  
  (defun %update-run-queue ()
    "Possibly return the current thread to the run queue, and
  return the next thread to run.
  Interrupts must be off, the current thread must be locked."
    (let ((current (current-thread)))
      (with-symbol-spinlock (*global-thread-lock*)
        (cond (*world-stopper*
               ;; World is stopped, the only runnable threads are
               ;; the pager, the disk io thread, the idle thread and the world stopper.
               (unless (or (eql current *world-stopper*)
                           (eql current sys.int::*pager-thread*)
                           (eql current sys.int::*disk-io-thread*))
                 (panic "Aiee. %UPDATE-RUN-QUEUE called with bad thread " current))
               (cond ((eql (thread-state sys.int::*pager-thread*) :runnable)
                      ;; Pager is ready to run.
                      sys.int::*pager-thread*)
                     ((eql (thread-state sys.int::*disk-io-thread*) :runnable)
                      ;; Disk IO is ready to run.
                      sys.int::*disk-io-thread*)
                     ((eql (thread-state *world-stopper*) :runnable)
                      ;; The world stopper is ready.
                      *world-stopper*)
                     (t ;; Switch to idle.
                      sys.int::*bsp-idle-thread*)))
              (t ;; Return the current thread to the run queue and fetch the next thread.
               (when (eql current sys.int::*bsp-idle-thread*)
                 (panic "Aiee. Idle thread called %UPDATE-RUN-QUEUE."))
               (when (eql (thread-state current) :runnable)
                 (push-run-queue current))
               (or (when (eql (thread-state sys.int::*pager-thread*) :runnable)
                     ;; Pager is ready to run.
                     sys.int::*pager-thread*)
                   (when (eql (thread-state sys.int::*disk-io-thread*) :runnable)
                     ;; Disk IO is ready to run.
                     sys.int::*disk-io-thread*)
                   ;; Try taking from the run queue.
                   (pop-run-queue)
                   ;; Fall back on idle.
                   sys.int::*bsp-idle-thread*))))))
  
  (defun %reschedule-via-wired-stack (sp fp)
    ;; Interrupts must be off and the current thread's lock must be held.
    ;; Releases the thread lock and reenables interrupts.
    (let ((current (current-thread))
          (next (%update-run-queue)))
      ;; todo: reset preemption timer here.
      (when (eql next current)
        ;; Staying on the same thread, unlock and return.
        (%unlock-thread current)
        (%%return-to-same-thread sp fp)
        (panic "unreachable"))
      (when (<= sys.int::*exception-stack-base*
                (thread-stack-pointer next)
                (1- sys.int::*exception-stack-size*))
        (panic "Other thread " next " stopped on exception stack!!!"))
      (%lock-thread next)
      (setf (thread-state next) :active)
      (%%switch-to-thread-via-wired-stack current sp fp next)))
  
  (sys.int::define-lap-function %%return-to-same-thread ()
    (sys.lap-x86:mov64 :rsp :r8)
    (sys.lap-x86:mov64 :rbp :r9)
    (sys.lap-x86:xor32 :ecx :ecx)
    (sys.lap-x86:mov64 :r8 nil)
    (sys.lap-x86:sti)
    (:gc :no-frame)
    (sys.lap-x86:ret))
  
  (sys.int::define-lap-function %%switch-to-thread-via-wired-stack ()
    ;; Save frame pointer.
    (sys.lap-x86:gs)
    (sys.lap-x86:mov64 (:object nil #.+thread-state-rbp+) :r10)
    ;; Save fpu state.
    (sys.lap-x86:gs)
    (sys.lap-x86:fxsave (:object nil #.+thread-fx-save-area+))
    ;; Save stack pointer.
    (sys.lap-x86:gs)
    (sys.lap-x86:mov64 (:object nil #.+thread-state-rsp+) :r9)
    ;; Only partial state was saved.
    (sys.lap-x86:gs)
    (sys.lap-x86:mov64 (:object nil #.+thread-full-save-p+) nil)
    ;; Jump to common function.
    (sys.lap-x86:mov64 :r9 :r11)
    (sys.lap-x86:mov64 :r13 (:function %%switch-to-thread-common))
    (sys.lap-x86:jmp (:r13 #.(+ (- sys.int::+tag-object+) 8 (* sys.int::+fref-entry-point+ 8)))))
  
  (defun %reschedule ()
    ;; Interrupts must be off and the current thread's lock must be held.
    ;; Releases the thread lock and reenables interrupts.
    (let ((current (current-thread))
          (next (%update-run-queue)))
      ;; todo: reset preemption timer here.
      (when (eql next current)
        ;; Staying on the same thread, unlock and return.
        (%unlock-thread current)
        (sys.int::%sti)
        (return-from %reschedule))
      (when (<= sys.int::*exception-stack-base*
                (thread-stack-pointer next)
                (1- sys.int::*exception-stack-size*))
        (panic "Other thread " next " stopped on exception stack!!!"))
      (%lock-thread next)
      (setf (thread-state next) :active)
      (%%switch-to-thread current next)))
  
  ;;; Switch to a new thread. Takes the current thread and the new thread as arguments.
  ;;; Watch out. The GC grovels around in the stack of not-running threads, if the
  ;;; layout changes, it must be updated.
  (sys.int::define-lap-function %%switch-to-thread ()
    (:gc :no-frame)
    ;; Save frame pointer.
    (sys.lap-x86:gs)
    (sys.lap-x86:mov64 (:object nil #.+thread-state-rbp+) :rbp)
    ;; Save fpu state.
    (sys.lap-x86:gs)
    (sys.lap-x86:fxsave (:object nil #.+thread-fx-save-area+))
    ;; Save stack pointer.
    (sys.lap-x86:gs)
    (sys.lap-x86:mov64 (:object nil #.+thread-state-rsp+) :rsp)
    ;; Only partial state was saved.
    (sys.lap-x86:gs)
    (sys.lap-x86:mov64 (:object nil #.+thread-full-save-p+) nil)
    ;; Jump to common function.
    (sys.lap-x86:mov64 :r13 (:function %%switch-to-thread-common))
    (sys.lap-x86:jmp (:r13 #.(+ (- sys.int::+tag-object+) 8 (* sys.int::+fref-entry-point+ 8)))))
  
  (defun %reschedule-via-interrupt (interrupt-frame)
    ;; Interrupts must be off and the current thread's lock must be held.
    ;; Releases the thread lock and reenables interrupts.
    (let ((current (current-thread))
          (next (%update-run-queue)))
      ;; todo: reset preemption timer here.
      ;; Avoid double-locking the thread when returning to the current thread.
      (when (not (eql next current))
        (%lock-thread next))
      (setf (thread-state next) :active)
      (%%switch-to-thread-via-interrupt current interrupt-frame next)))
  
  (defun wake-thread (thread)
    "Wake a sleeping thread."
    (without-interrupts
  
negative.code: ;; The cold-generator creates an idle thread for the BSP.
  (defvar sys.int::*bsp-idle-thread*)
  
  (deftype thread ()
    `(satisfies threadp))
  
  (defun threadp (object)
    (and (eql (sys.int::%tag-field object) sys.int::+tag-object+)
         (eql (sys.int::%object-tag object) sys.int::+object-tag-thread+)))
  
  ;;; Thread locking.
  ;;;
  ;;; Each thread has a per-thread spinlock (the thread-lock field), and there is the *GLOBAL-THREAD-LOCK*.
  
  (macrolet ((field (name offset &key (type 't) (accessor 'sys.int::%object-ref-t))
               (let ((field-name (intern (format nil "+THREAD-~A+" (symbol-name name))
  