Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Provide loop wrapper for casArrayElem? #64

Open
winterland1989 opened this issue May 24, 2017 · 2 comments
Open

Provide loop wrapper for casArrayElem? #64

winterland1989 opened this issue May 24, 2017 · 2 comments

Comments

@winterland1989
Copy link

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
Copy link

treeowl commented Jun 21, 2018

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 added a commit to treeowl/haskell-lockfree that referenced this issue Jun 21, 2018
treeowl added a commit to treeowl/haskell-lockfree that referenced this issue Jun 21, 2018
treeowl added a commit to treeowl/haskell-lockfree that referenced this issue Jun 21, 2018
treeowl added a commit to treeowl/haskell-lockfree that referenced this issue Jun 21, 2018
treeowl added a commit to treeowl/haskell-lockfree that referenced this issue Jun 21, 2018
treeowl added a commit to treeowl/haskell-lockfree that referenced this issue Jun 21, 2018
treeowl added a commit to treeowl/haskell-lockfree that referenced this issue Jun 21, 2018
@treeowl
Copy link

treeowl commented Mar 3, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants