-
Notifications
You must be signed in to change notification settings - Fork 58
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
Add atomic stuff #295
Comments
A while ago, I wrote primitive-atomic, which just has wrapper for the atomic primops. It's not as sophisticated as
Although, as you point out, CAS on lifted objects is useless on types that have no notion of identity. If the elements in the array were |
CAS primop Here is a potential implementation: atomicModifyArray# :: MonadPrim s m => MutableArray s e -> Int -> (e -> (# e, b #)) -> m b
atomicModifyArray# ma@(MutableArray ma#) i@(I# i#) f = do
current0 <- readArray ma i
primitive $
let go expected s =
case f expected of
(# new, artifact #) ->
case casArray# ma# i# expected new s of
(# s', 0#, _ #) -> (# s', artifact #)
(# s', _, actual #) -> go actual s'
in go current0 So, I wouldn't say it is useless. |
CAS on lifted objects is not useless. It can, however, be subtle, as the data Ticket a = Ticket a
peekTicket :: Ticket a -> a
peekTicket (Ticket a) = lazy a
peekTicket# :: Ticket a -> (# a #)
peekTicket# (Ticket a) = (# lazy a #) |
Yes, that's an alternative implementation, similar to one in |
Yeah, I don't think atomic modification is a good use case with high contention, even |
@lehins, what goes wrong with |
@treeowl Remember this SO question, which I think is somehow related to this and also I've done some benchmarks at some point before and I found that with increasing number of threads performance drops pretty significantly. Not sure why that is as I did not investigate it much further. However I know I will come back to this at some point in the future because I have some interesting work-in-progress for an API that amongst other things provides a unified interface for atomic modification of various mutable types. |
For some reason, this package doesn't offer basic access to atomic CAS and such. I see no good reason for this. The
atomic-primops
package has some good ideas for this, but its implementation is flaky under optimization and its author does not seem responsive to suggestions for fixing that.An interesting (?) thought: I think we can write a somewhat-inefficient
atomicModifyIORef
-like function for array elements using the same sort of implementation trickery but in user-land. Very roughly speaking:The text was updated successfully, but these errors were encountered: