Skip to content

Commit f6d283a

Browse files
committed
use 'atomicModifyIORefCAS' where possible
1 parent cd46bc0 commit f6d283a

File tree

7 files changed

+19
-12
lines changed

7 files changed

+19
-12
lines changed

core/package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ library:
5050
# hackage dependencies
5151
- aeson
5252
- async
53+
- atomic-primops
5354
- lens-family
5455
- monad-logger
5556
- network-bsd

core/src/Temporal/Core/Worker.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ import Control.Exception
4949
import Control.Monad
5050
import Data.Aeson
5151
import Data.Aeson.TH
52+
import Data.Atomics (atomicModifyIORefCAS)
5253
import Data.ByteString (ByteString)
5354
import qualified Data.ByteString.Lazy as BL
5455
import Data.IORef
@@ -258,7 +259,7 @@ instance Exception WorkerAlreadyClosed
258259
-- After calling this, the worker must not be used again.
259260
closeWorker :: Worker ty -> IO ()
260261
closeWorker (Worker w _ _ _) = mask_ $ do
261-
wp <- atomicModifyIORef' w $ \wp -> (throw WorkerAlreadyClosed, wp)
262+
wp <- liftIO $ atomicModifyIORefCAS w $ \wp -> (throw WorkerAlreadyClosed, wp)
262263
raw_closeWorker wp
263264

264265

core/temporal-sdk-core.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ library
8888
build-depends:
8989
aeson
9090
, async
91+
, atomic-primops
9192
, base >=4.14 && <5
9293
, bytestring
9394
, containers

sdk/package.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ dependencies:
1010
- proto-lens-protobuf-types
1111
- temporal-api-protos
1212
- temporal-sdk-core
13+
- atomic-primops
1314
- bytestring
1415
- discover-instances
1516
- mtl

sdk/src/Temporal/Workflow/Internal/Instance.hs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module Temporal.Workflow.Internal.Instance (
1818
) where
1919

2020
import Control.Monad.Reader
21+
import Data.Atomics (atomicModifyIORefCAS)
2122
import Data.ProtoLens
2223
import qualified Data.Text as T
2324
import GHC.Stack
@@ -62,46 +63,46 @@ flushCommands = do
6263
nextExternalCancelSequence :: InstanceM Sequence
6364
nextExternalCancelSequence = do
6465
inst <- ask
65-
atomicModifyIORef' inst.workflowSequences $ \seqs ->
66+
liftIO $ atomicModifyIORefCAS inst.workflowSequences $ \seqs ->
6667
let seq' = externalCancel seqs
6768
in (seqs {externalCancel = succ seq'}, Sequence seq')
6869

6970

7071
nextChildWorkflowSequence :: InstanceM Sequence
7172
nextChildWorkflowSequence = do
7273
inst <- ask
73-
atomicModifyIORef' inst.workflowSequences $ \seqs ->
74+
liftIO $ atomicModifyIORefCAS inst.workflowSequences $ \seqs ->
7475
let seq' = childWorkflow seqs
7576
in (seqs {childWorkflow = succ seq'}, Sequence seq')
7677

7778

7879
nextExternalSignalSequence :: InstanceM Sequence
7980
nextExternalSignalSequence = do
8081
inst <- ask
81-
atomicModifyIORef' inst.workflowSequences $ \seqs ->
82+
liftIO $ atomicModifyIORefCAS inst.workflowSequences $ \seqs ->
8283
let seq' = externalSignal seqs
8384
in (seqs {externalSignal = succ seq'}, Sequence seq')
8485

8586

8687
nextTimerSequence :: InstanceM Sequence
8788
nextTimerSequence = do
8889
inst <- ask
89-
atomicModifyIORef' inst.workflowSequences $ \seqs ->
90+
liftIO $ atomicModifyIORefCAS inst.workflowSequences $ \seqs ->
9091
let seq' = timer seqs
9192
in (seqs {timer = succ seq'}, Sequence seq')
9293

9394

9495
nextActivitySequence :: InstanceM Sequence
9596
nextActivitySequence = do
9697
inst <- ask
97-
atomicModifyIORef' inst.workflowSequences $ \seqs ->
98+
liftIO $ atomicModifyIORefCAS inst.workflowSequences $ \seqs ->
9899
let seq' = activity seqs
99100
in (seqs {activity = succ seq'}, Sequence seq')
100101

101102

102103
nextConditionSequence :: InstanceM Sequence
103104
nextConditionSequence = do
104105
inst <- ask
105-
atomicModifyIORef' inst.workflowSequences $ \seqs ->
106+
liftIO $ atomicModifyIORefCAS inst.workflowSequences $ \seqs ->
106107
let seq' = condition seqs
107108
in (seqs {condition = succ seq'}, Sequence seq')

sdk/src/Temporal/Workflow/Internal/Monad.hs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Control.Monad
88
import qualified Control.Monad.Catch as Catch
99
import Control.Monad.Logger
1010
import Control.Monad.Reader
11+
import Data.Atomics (atomicModifyIORefCAS, atomicModifyIORefCAS_)
1112
import Data.HashMap.Strict (HashMap)
1213
import qualified Data.HashMap.Strict as HashMap
1314
import Data.Kind
@@ -19,7 +20,6 @@ import Data.Time.Clock.System (SystemTime)
1920
import Data.Vault.Strict
2021
import Data.Vector (Vector)
2122
import Data.Word (Word32)
22-
-- import Debug.Trace
2323
import GHC.Stack
2424
import GHC.TypeLits
2525
import Proto.Temporal.Sdk.Core.WorkflowActivation.WorkflowActivation
@@ -343,7 +343,7 @@ instance FrozenGen StdGen Workflow where
343343
{-# INLINE addJob #-}
344344
addJob :: ContinuationEnv -> Workflow b -> IVar b -> IVar a -> InstanceM ()
345345
addJob env !wf !resultIVar IVar {ivarRef = !ref} =
346-
join $ atomicModifyIORef' ref $ \case
346+
join $ liftIO $ atomicModifyIORefCAS ref $ \case
347347
IVarEmpty list -> (IVarEmpty (JobCons env wf resultIVar list), pure ())
348348
full -> (full, modifyIORef' env.runQueueRef (JobCons env wf resultIVar))
349349

@@ -599,7 +599,7 @@ class MonadWriteStateVar m where
599599
instance MonadReadStateVar Condition where
600600
readStateVar var = Condition $ do
601601
touchedVars <- ask
602-
modifyIORef' touchedVars (Set.insert var.stateVarId)
602+
liftIO $ atomicModifyIORefCAS_ touchedVars (Set.insert var.stateVarId)
603603
readIORef var.stateVarRef
604604

605605

@@ -617,7 +617,7 @@ instance MonadWriteStateVar Workflow where
617617
reevaluateDependentConditions var
618618
pure $ Done ()
619619
modifyStateVar var f = Workflow $ \_ -> do
620-
res <- modifyIORef' var.stateVarRef f
620+
res <- liftIO $ atomicModifyIORefCAS_ var.stateVarRef f
621621
reevaluateDependentConditions var
622622
pure $ Done res
623623

@@ -921,6 +921,6 @@ instance Monoid WorkflowOutboundInterceptor where
921921
nextVarIdSequence :: InstanceM Sequence
922922
nextVarIdSequence = do
923923
inst <- ask
924-
atomicModifyIORef' inst.workflowSequences $ \seqs ->
924+
liftIO $ atomicModifyIORefCAS inst.workflowSequences $ \seqs ->
925925
let seq' = varId seqs
926926
in (seqs {varId = succ seq'}, Sequence seq')

sdk/temporal-sdk.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ library
102102
aeson
103103
, annotated-exception
104104
, async
105+
, atomic-primops
105106
, base >=4.7 && <5
106107
, base64
107108
, bytestring
@@ -187,6 +188,7 @@ test-suite temporal-sdk-tests
187188
aeson
188189
, annotated-exception
189190
, async
191+
, atomic-primops
190192
, base
191193
, base64
192194
, bytestring

0 commit comments

Comments
 (0)