anchor.diff:        (panic "Stopping world while pseudo-atomic!"))
       (ensure-interrupts-enabled)
       (inhibit-thread-pool-blocking-hijack
  -      (with-world-stop-lock ()
  -        ;; First, try to position ourselves as the next thread to stop the world.
  -        ;; This prevents any more threads from becoming PA.
  -        (loop
  -           (when (null *world-stop-pending*)
  -             (setf *world-stop-pending* self)
  -             (return))
  -           ;; Wait for the world to unstop.
  -           ;; FIXME: This can still cause deadlocks when reacquiring the lock.
  -           (condition-wait *world-stop-cvar* *world-stop-lock*))
  -        ;; Now wait for any PA threads to finish.
  -        (loop
  -           (when (zerop *pseudo-atomic-thread-count*)
  -             (return))
  -           ;; FIXME: Same here.
  -           (condition-wait *world-stop-cvar* *world-stop-lock*))
  -        (safe-without-interrupts (self)
  -                                 (acquire-global-thread-lock)
  -                                 (setf *world-stopper* self
  -                                       *world-stop-pending* nil)
  -                                 (release-global-thread-lock))
  -        (quiesce-cpus-for-world-stop))
  -      (unwind-protect
  -           (funcall thunk)
  -        (with-world-stop-lock ()
  -          ;; Release the dogs!
  -          (safe-without-interrupts ()
  -                                   (acquire-global-thread-lock)
  -                                   (setf *world-stopper* nil)
  -                                   (release-global-thread-lock))
  -          (condition-notify *world-stop-cvar* t)
  -          (broadcast-wakeup-ipi))))))
  +     (%call-on-wired-stack-without-interrupts #'%stop-the-world nil)
  +     (unwind-protect
  +          (progn
  +            (quiesce-cpus-for-world-stop)
  +            (funcall thunk))
  +       (%call-on-wired-stack-without-interrupts #'%resume-the-world nil)
  +       (broadcast-wakeup-ipi)))))
   
   (defmacro with-world-stopped (&body body)
     `(call-with-world-stopped (dx-lambda () ,@body)))
  
positive.code:     ;; Default timeslice is 10ms/0.01s.
      ;; Work at read-time to avoid floats at runtime.
      (setf *timeslice-length* #.(truncate (* 0.01 internal-time-units-per-second)))
      (setf *world-stop-lock* (make-mutex "World stop lock")
            *world-stop-cvar* (make-condition-variable "World stop cvar")
            *world-stop-pending* nil
            *pseudo-atomic-thread-count* 0)
      (setf *all-threads* sys.int::*snapshot-thread*
            (thread-global-next sys.int::*snapshot-thread*) sys.int::*pager-thread*
            (thread-global-prev sys.int::*snapshot-thread*) nil
  
negative.code: (defun world-stopped-p ()
    "Returns true if the world is stopped."
    *world-stopper*)
  
  (defun %enter-pseudo-atomic-unsleep (arg)
    (declare (ignore arg))
    (%call-on-wired-stack-without-interrupts #'%enter-pseudo-atomic nil))
  
  (defun %enter-pseudo-atomic (sp fp)
    (acquire-stw-locks)
    (let ((self (current-thread)))
      (when (and (wait-queue-empty-p *pending-world-stoppers*)
                 ;; This might be possible? If the world is stopped on
                 ;; another CPU this might slip in between the other CPU
                 ;; releasing locks & quiescing CPUs?
                 (not *world-stopper*))
        (incf *pseudo-atomic-thread-count*)
        (release-stw-locks)
        (return-from %enter-pseudo-atomic))
      (push-wait-queue self *pending-pseudo-atomics*)
      ;; Leave the thread-lock locked, going to sleep.
      (release-stw-locks t)
      (setf (thread-wait-item self) *pending-pseudo-atomics*
            (thread-state self) :sleeping
            (thread-unsleep-helper self) #'%enter-pseudo-atomic-unsleep
            (thread-unsleep-helper-argument self) nil)
      (%reschedule-via-wired-stack sp fp)))
  
  (defun %leave-pseudo-atomic (sp fp)
    (declare (ignore sp fp))
    (acquire-stw-locks)
    (decf *pseudo-atomic-thread-count*)
    (when (and (eql *pseudo-atomic-thread-count* 0)
               (not (wait-queue-empty-p *pending-world-stoppers*)))
      (let ((thread (pop-wait-queue *pending-world-stoppers*)))
        (setf *world-stopper* thread)
        (wake-thread-1 thread)))
    (release-stw-locks))
  
  (defun call-with-pseudo-atomic (thunk)
    (when (eql *world-stopper* (current-thread))
      (panic "Going PA with world stopped!"))
    (ensure-interrupts-enabled)
    (inhibit-thread-pool-blocking-hijack
     (%call-on-wired-stack-without-interrupts #'%enter-pseudo-atomic nil)
     (unwind-protect
          (let ((*pseudo-atomic* t))
            (funcall thunk))
       (%call-on-wired-stack-without-interrupts #'%leave-pseudo-atomic nil))))
  
  (defmacro with-pseudo-atomic (&body body)
    `(call-with-pseudo-atomic (dx-lambda () ,@body)))
  