Create volatile box for managing state



Clojure needs a faster variant of Atom for managing state inside transducers. That is, Atoms do the job, but they provide a little too much capability for the purposes of transducers. Specifically the compare and swap semantics of Atoms add too much overhead. Therefore, it was determined that a simple volatile ref type would work to ensure basic propagation of its value to other threads and reads of the latest write from any other thread. While updates are subject to race conditions, access is controlled by JVM guarantees.

Solution overview: Create a concrete type in Java, akin to clojure.lang.Box, but volatile inside supports IDeref, but not watches etc.


Patch: volatile3.diff

Screened by: fogus




Alex Miller
August 29, 2014, 10:31 PM

Updated to volatile3.diff to address offline comment from Rich.

August 29, 2014, 5:01 PM

I downloaded the patch and applied to latest master. I ran the isolated tests and the full test suite and also ensured that the patch didn't add any reflection warnings. I then modified the ticket description to add a little more context and motivation (for future readers). The code is straight-forward and clean.

Alex Miller
August 28, 2014, 3:05 PM

Made changes as requested.

Rich Hickey
August 27, 2014, 12:37 PM

Can you please lift the with-meta stuff out of the syntax-quote?
Actually, if volatile! ctor returned a type-hinted value that extra hinting might not even be needed. Let's do both for now.

Also the type hint on the volatile? arg makes no sense - it's a predicate asking if something is a volatile.

Jozef Wagner
August 27, 2014, 9:50 AM

why the bang in the name of volatile! function? If the reason is to warn users that this is an 'expert only' stuff, I suggest to use a verbose name instead, e.g. volatile-reference. (This will also be consistent with approach chosen in the names of volatile-mutable and unsynchronized-mutable hints.)

Your pinned fields
Click on the next to a field label to start pinning.


Rich Hickey


Rich Hickey





Code and Test



Affects versions

Fix versions