rrnewton / haskell-lockfree

A collection of different packages for CAS based data structures.
106 stars 25 forks source link

Provide loop wrapper for casArrayElem? #64

Open winterland1989 opened 7 years ago

winterland1989 commented 7 years ago

The casArrayElem provide a low-level atomic operations based on Ticket, Is there any possibilities to provide a loop wrapper similar to atomicModifyIORef? 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.

treeowl commented 6 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.

treeowl commented 1 year ago

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.