Skip to content

Commit

Permalink
io-sim-classes: Eq instances for stm's mutable varibles
Browse files Browse the repository at this point in the history
* TVar
* TMVar
* TQueue
* TBQueue

Expose 'eqTVar' which only requires `MonadSTM m` constraint and 'EqTVar'
type alias which allows to use `==` directly.  The complications comes
from rough edges of QuantifiedConstraints and TypeFamily extensions
(https://gitlab.haskell.org/ghc/ghc/-/issues/14860)

Includes changes in `io-sim-classes` and `io-sim`.
  • Loading branch information
coot committed Apr 15, 2021
1 parent 5729847 commit e3aef29
Show file tree
Hide file tree
Showing 4 changed files with 305 additions and 84 deletions.
15 changes: 13 additions & 2 deletions io-sim-classes/src/Control/Monad/Class/MonadAsync.hs
Expand Up @@ -33,6 +33,10 @@ import Control.Monad.Class.MonadThrow

import Control.Concurrent.Async (AsyncCancelled (..))
import qualified Control.Concurrent.Async as Async
import qualified Control.Concurrent.STM.TVar as STM
import qualified Control.Concurrent.STM.TMVar as STM
import qualified Control.Concurrent.STM.TQueue as STM
import qualified Control.Concurrent.STM.TBQueue as STM
import qualified Control.Exception as E
import Control.Monad.Reader
import qualified Control.Monad.STM as STM
Expand All @@ -41,7 +45,10 @@ import Data.Foldable (fold)
import Data.Kind (Type)
import Data.Proxy

class (Functor async, MonadSTMTx stm) => MonadAsyncSTM async stm where
class ( Functor async
, MonadSTMTx stm tvar tmvar tqueue tbqueue
)
=> MonadAsyncSTM async stm tvar tmvar tqueue tbqueue where
{-# MINIMAL waitCatchSTM, pollSTM #-}

waitSTM :: async a -> stm a
Expand Down Expand Up @@ -98,6 +105,8 @@ class (Functor async, MonadSTMTx stm) => MonadAsyncSTM async stm where
class ( MonadSTM m
, MonadThread m
, MonadAsyncSTM (Async m) (STM m)
(TVar m) (TMVar m)
(TQueue m) (TBQueue m)
) => MonadAsync m where

{-# MINIMAL async, asyncThreadId, cancel, cancelWith, asyncWithUnmask #-}
Expand Down Expand Up @@ -258,7 +267,9 @@ replicateConcurrently_ cnt = runConcurrently . fold . replicate cnt . Concurrent
-- Instance for IO uses the existing async library implementations
--

instance MonadAsyncSTM Async.Async STM.STM where
instance MonadAsyncSTM Async.Async STM.STM
STM.TVar STM.TMVar
STM.TQueue STM.TBQueue where
waitSTM = Async.waitSTM
pollSTM = Async.pollSTM
waitCatchSTM = Async.waitCatchSTM
Expand Down

0 comments on commit e3aef29

Please sign in to comment.