Open winterland1989 opened 7 years ago
Unfortunately, this can't be implemented terribly efficiently using the low-level atomic operations. Some CMM code will be needed to do it optimally. Given atomicModifyIORef ref f
, GHC creates a closure (let's call it q
) representing f
applied to the old value and two closures to fetch the first and second components of q
. Writing in Haskell, we'd have to create all three of these closures anew each time through the loop, but the primop implementation can actually just mutate q
to replace the function argument. We could, I believe, simulate something like this by using an extra IORef
, but that's not likely to be terribly efficient.
I just realized you were probably looking for something like atomicModifyIORefCAS
, but for arrays. Silly me. That only works okay for very cheap functions, but it's an option.
The
casArrayElem
provide a low-level atomic operations based onTicket
, Is there any possibilities to provide a loop wrapper similar toatomicModifyIORef
? As an user i only care about swapping a new result into array and get the old value out rather than manually compare pointer and looping.