multithreadingcommon-lispsbcl

Atomic Operations for Multithreading in SBCL


I'm getting an error when loading a function that contains an atomic operation. As a simple example, the file test.lisp contains:

(defparameter *count* 0)
(defun test ()
  (sb-ext:atomic-incf *count*))

which generates the following error:

* (load "d:\\test.lisp")

; file: d:/test.lisp
; in: DEFUN TEST
;     (ATOMIC-INCF *COUNT*)
;
; caught ERROR:
;   during macroexpansion of (ATOMIC-INCF *COUNT*). Use *BREAK-ON-SIGNALS* to
;   intercept.
;
;    Invalid first argument to ATOMIC-INCF: *COUNT*
;
; compilation unit finished
;   caught 1 ERROR condition
T
*

Why is *count* invalid?


Solution

  • From the docstring:

    PLACE must access one of the following:
     - a DEFSTRUCT slot with declared type (UNSIGNED-BYTE 64)
       or AREF of a (SIMPLE-ARRAY (UNSIGNED-BYTE 64) (*))
       The type SB-EXT:WORD can be used for these purposes.
     - CAR or CDR (respectively FIRST or REST) of a CONS.
     - a variable defined using DEFGLOBAL with a proclaimed type of FIXNUM.
    Macroexpansion is performed on PLACE before expanding ATOMIC-INCF.
    

    I suspect these are in place to avoid runtime checks when doing compare-and-swap.