-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a5da3ba
commit 30d26f4
Showing
3 changed files
with
57 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
-- | STM minus retry. These STM actions are guaranteed not to block, and thus guaranteed not to be interrupted by an | ||
-- async exception. | ||
module Ki.Internal.NonblockingSTM | ||
( NonblockingSTM, | ||
nonblockingAtomically, | ||
nonblockingThrowSTM, | ||
|
||
-- * TVar | ||
nonblockingReadTVar, | ||
nonblockingWriteTVar', | ||
) | ||
where | ||
|
||
import Control.Exception (Exception) | ||
import Data.Coerce (coerce) | ||
import GHC.Conc (STM, TVar, atomically, readTVar, throwSTM, writeTVar) | ||
|
||
newtype NonblockingSTM a | ||
= NonblockingSTM (STM a) | ||
deriving newtype (Applicative, Functor, Monad) | ||
|
||
nonblockingAtomically :: forall a. NonblockingSTM a -> IO a | ||
nonblockingAtomically = | ||
coerce @(STM a -> IO a) atomically | ||
|
||
nonblockingThrowSTM :: forall e x. (Exception e) => e -> NonblockingSTM x | ||
nonblockingThrowSTM = | ||
coerce @(e -> STM x) throwSTM | ||
|
||
nonblockingReadTVar :: forall a. TVar a -> NonblockingSTM a | ||
nonblockingReadTVar = | ||
coerce @(TVar a -> STM a) readTVar | ||
|
||
nonblockingWriteTVar' :: forall a. TVar a -> a -> NonblockingSTM () | ||
nonblockingWriteTVar' var !x = | ||
NonblockingSTM (writeTVar var x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters