Skip to content

Investigate adding fast compare-and-swap Int type/primops

I've received reports that using IORef Int and atomicModifyIORef to implement an atomic counter in the ekg package has become a bottleneck for some of its users. These users update the counter thousands of times per second, using multiple threads.

I will investigate whether adding a dedicated atomic Int reference type will offer significant speed improvements. Such a type can also be used to implement cheaper locks (by using bits in the int to represent different lock states, such as reader/write locks.)

Lets call this new type AtomicIntRef for now. This new type needs to support at least these functions:

add :: AtomicIntRef -> Int -> IO Int
set :: AtomicIntRef -> Int -> IO ()
get :: AtomicIntRef -> IO Int

add would be implemented using the lock and xaddq instructions. set and get are just simple loads and stores on x86, as these are atomic.

We might also want to consider having other functions, such as a cas. Furthermore, there are subtleties with memory barriers that might motivate having barrier/barrier-less versions of some functions.

Trac metadata
Trac field Value
Version 7.9
Type FeatureRequest
TypeOfFailure OtherFailure
Priority normal
Resolution Unresolved
Component Compiler
Test case
Differential revisions
BlockedBy
Related
Blocking
CC
Operating system
Architecture
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information