X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=tests%2Fcompare-and-swap.impure.lisp;h=accb74f23090c6d24bdcdcb546102ce69aa10ff1;hb=83659744f9caa97aa83eb562d872b1c0127403c0;hp=8a034adf3c38a659d7e71d407cef4990d62d48d9;hpb=db9c81e7b9a67ebd26b87bf2c1686d1b4968f097;p=sbcl.git diff --git a/tests/compare-and-swap.impure.lisp b/tests/compare-and-swap.impure.lisp index 8a034ad..accb74f 100644 --- a/tests/compare-and-swap.impure.lisp +++ b/tests/compare-and-swap.impure.lisp @@ -104,3 +104,46 @@ (handler-case (sb-ext:compare-and-swap (symbol-value name) t 42) (error () :error))))) + +;;;; ATOMIC-INCF and ATOMIC-DECF (we should probably rename this file atomic-ops...) + +(defstruct box + (word 0 :type sb-vm:word)) + +(defun inc-box (box n) + (declare (fixnum n) (box box)) + (loop repeat n + do (sb-ext:atomic-incf (box-word box)))) + +(defun dec-box (box n) + (declare (fixnum n) (box box)) + (loop repeat n + do (sb-ext:atomic-decf (box-word box)))) + +(let ((box (make-box))) + (inc-box box 10000) + (assert (= 10000 (box-word box))) + (dec-box box 10000) + (assert (= 0 (box-word box)))) + +(with-test (:name :atomic-incf-wraparound) + (let ((box (make-box :word (1- (ash 1 sb-vm:n-word-bits))))) + (sb-ext:atomic-incf (box-word box) 2) + (assert (= 1 (box-word box))))) + +(with-test (:name :atomic-decf-wraparound) + (let ((box (make-box :word 0))) + (sb-ext:atomic-decf (box-word box) 2) + (assert (= (- (ash 1 sb-vm:n-word-bits) 2) (box-word box))))) + +#+sb-thread +(let* ((box (make-box)) + (threads (loop repeat 64 + collect (sb-thread:make-thread (lambda () + (inc-box box 1000) + (dec-box box 10000) + (inc-box box 10000) + (dec-box box 1000)) + :name "inc/dec thread")))) + (mapc #'sb-thread:join-thread threads) + (assert (= 0 (box-word box))))