Add atomic stuff
- Dominant language
- Haskell
- Stars
- 123
- Forks
- 60
- PR merge metrics
- No merged PRs in 30d
Description
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:
```haskell
atomicModifyArray
:: MutableArray RealWorld a
-> Int
-> (a -> (a, b))
-> IO (a, b)
atomicModifyArray mary i f = do
old_ref <- newIORef undefined
let
res_thunk = unsafeDupablePerformIO $
f <$> readIORef old_ref
{-# NOINLINE res_thunk #-}
(new_thunk, _) = res_thunk
{-# NOINLINE new_thunk #-}
loop = do
old_val <- readArray mary i
writeIORef old_ref old_val
succeeded <-
-- Perform a CAS expecting old_val and
-- attempting to install new_thunk
if succeeded
then do
-- The evaluate is optional, but advisable.
-- The alternative is to return `lazy res_thunk`
ret <- evaluate res_thunk
pure ret
else loop
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.