From 58df54e8137ec4a740dbe5ef7a7b3f92eb3743a6 Mon Sep 17 00:00:00 2001 From: Anton-Latukha Date: Tue, 9 Feb 2021 02:31:37 +0200 Subject: [PATCH] Control.Monad.Ref: add instance MonadAtomicRef (ST a) This is code by John Wiegley. --- Control/Monad/Ref.hs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Control/Monad/Ref.hs b/Control/Monad/Ref.hs index 754f4ba..98ac277 100644 --- a/Control/Monad/Ref.hs +++ b/Control/Monad/Ref.hs @@ -225,6 +225,19 @@ instance MonadAtomicRef IO where atomicModifyRef' = atomicModifyIORef' #endif /* MIN_VERSION_base(4,6,0) */ +-- Since there's no forking, it's automatically atomic. +instance MonadAtomicRef (ST s) where + atomicModifyRef r f = do + v <- readRef r + let (a, b) = f v + writeRef r a + return b + atomicModifyRef' r f = do + v <- readRef r + let (a, b) = f v + writeRef r $! a + return b + instance MonadAtomicRef STM where atomicModifyRef r f = do x <- readRef r let (x', y) = f x