Investigate adding fast compare-and-swap Int type/primops
|Reported by:||tibbe||Owned by:||tibbe|
|Type of failure:||None/Unknown||Test Case:|
|Related Tickets:||#8157, #7883||Differential Rev(s):|
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
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.