'cas' returns 'False' on success
- Dominant language
- Haskell
- Stars
- 49
- Forks
- 7
- PR merge metrics
- No merged PRs in 30d
Description
The [`cas`](https://github.com/ekmett/structs/blob/master/src/Data/Struct/Internal.hs#L196) operation returns `False` on a successful compare-and-swap, and `True` on failure. It's not a bug per-se, but... a little surprising. I also note that this is just a thin wrapper around `casSmallArray#`, so if this is undesired behaviour, arguably it should be fixed upstream. The best course is probably to just update the documentation?
ghc version: 8.4.4 (lts-12.0), 8.6.2 (nightly-2018-11-27)
structs version: 0.1.1, fa86a1acab401e8421c1547d3d6bbc38b4c5a3e2
# Example
```haskell
{-# LANGUAGE RoleAnnotations #-}
{-# LANGUAGE TemplateHaskell #-}
import Data.Struct
import Data.Struct.Internal
import Data.Struct.TH
import Text.Printf
makeStruct [d|
data LinkedList a s = LinkedList
{ val :: a
, next :: !(LinkedList a s)
}
|]
test :: IO ()
test = do
n0 <- newLinkedList (0 :: Int) Nil
n1 <- newLinkedList (1 :: Int) Nil
n2 <- newLinkedList (2 :: Int) Nil
a <- get next n0
printf "n0.next == Nil: %s\n" (show (isNil a))
printf "try to swing n0.next to n1 (this should succeed)\n"
(status1, rv1) <- cas next n0 Nil n1
printf "cas returned status=%s, isNil returned_val=%s\n" (show status1) (show (isNil rv1))
printf "returned_val == n1: %s\n" (show (eqStruct rv1 n1))
b <- get next n0
printf "n0.next == Nil: %s\n" (show (isNil b))
printf "n0.next == n1: %s\n" (show (eqStruct b n1))
printf "\n"
printf "try to swing n0.next to n2 (this should fail)\n"
(status2, rv2) <- cas next n0 Nil n2
printf "cas returned status=%s, isNil returned_val=%s\n" (show status2) (show (isNil rv2))
printf "returned_val == n1: %s\n" (show (eqStruct rv2 n1))
printf "returned_val == n2: %s\n" (show (eqStruct rv2 n2))
```
# Output
```
n0.next == Nil: True
try to swing n0.next to n1 (this should succeed)
cas returned status=False, isNil returned_val=False
returned_val == n1: True
n0.next == Nil: False
n0.next == n1: True
try to swing n0.next to n2 (this should fail)
cas returned status=True, isNil returned_val=False
returned_val == n1: True
returned_val == n2: False
```
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.