X-Git-Url: http://repo.macrolet.net/gitweb/?a=blobdiff_plain;f=tests%2Fcompare-and-swap.impure.lisp;h=e35b56d94ac1cb73daedc546c758b45699b256a6;hb=3fa2feb10ab827fc6cc2a85287e78b6e66b7bf4d;hp=63660431f3494e955bd3536196fe4d9cca2c59db;hpb=bfb19d306581ac86feb4371846c4b9953d692dd8;p=sbcl.git diff --git a/tests/compare-and-swap.impure.lisp b/tests/compare-and-swap.impure.lisp index 6366043..e35b56d 100644 --- a/tests/compare-and-swap.impure.lisp +++ b/tests/compare-and-swap.impure.lisp @@ -75,3 +75,65 @@ (ignore-errors (compare-and-swap (svref "foo" 1) 1 2)) (assert (not res)) (assert (typep err 'type-error))) + +;; Check that we don't modify constants +(defconstant +a-constant+ 42) +(assert + (eq :error + (handler-case + (sb-ext:compare-and-swap (symbol-value '+a-constant+) 42 13) + (error () :error)))) +(let ((name '+a-constant+)) + (assert + (eq :error + (handler-case + (sb-ext:compare-and-swap (symbol-value name) 42 13) + (error () :error))))) + +;; Check that we don't mess declaimed types +(declaim (boolean *a-boolean*)) +(defparameter *a-boolean* t) +(assert + (eq :error + (handler-case + (sb-ext:compare-and-swap (symbol-value '*a-boolean*) t 42) + (error () :error)))) +(let ((name '*a-boolean*)) + (assert + (eq :error + (handler-case + (sb-ext:compare-and-swap (symbol-value name) t 42) + (error () :error))))) + +;;;; ATOMIC-INCF (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-incf (box-word box) -1))) + +(let ((box (make-box))) + (inc-box box 10000) + (assert (= 10000 (box-word box))) + (dec-box box 10000) + (assert (= 0 (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))))