-
Notifications
You must be signed in to change notification settings - Fork 20
/
EarlyExit.hs
307 lines (257 loc) · 11.7 KB
/
EarlyExit.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE MultiParamTypeClasses #-}
{-# LANGUAGE QuantifiedConstraints #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE UndecidableInstances #-}
module Ouroboros.Consensus.Util.EarlyExit (
exitEarly
, withEarlyExit
, withEarlyExit_
-- * Re-exports
, lift
-- * opaque
, WithEarlyExit
) where
import Control.Applicative
import Control.Concurrent.Class.MonadMVar
import Control.Monad
import Control.Monad.Class.MonadAsync
import Control.Monad.Class.MonadEventlog
import Control.Monad.Class.MonadFork
import Control.Monad.Class.MonadST
import Control.Monad.Class.MonadSTM.Internal
import Control.Monad.Class.MonadThrow
import Control.Monad.Class.MonadTime
import Control.Monad.Class.MonadTime.SI
import Control.Monad.Class.MonadTimer
import qualified Control.Monad.Class.MonadTimer.SI as TimerSI
import Control.Monad.Trans.Class
import Control.Monad.Trans.Maybe
import Data.Function (on)
import Data.Proxy
import NoThunks.Class (NoThunks (..))
import Ouroboros.Consensus.Util ((.:))
import Ouroboros.Consensus.Util.IOLike (IOLike (..), PrimMonad (..),
StrictSVar, StrictTVar, castStrictSVar, castStrictTVar)
import Ouroboros.Consensus.Util.NormalForm.StrictMVar (StrictMVar)
{-------------------------------------------------------------------------------
Basic definitions
-------------------------------------------------------------------------------}
newtype WithEarlyExit m a = WithEarlyExit {
unWithEarlyExit :: MaybeT m a
}
deriving ( Functor
, Applicative
, Alternative
, Monad
, MonadTrans
, MonadPlus
)
instance NoThunks (StrictTVar m a)
=> NoThunks (StrictTVar (WithEarlyExit m) a) where
showTypeOf _ = "StrictTVar (WithEarlyExit m)"
wNoThunks ctxt tv = do
wNoThunks ctxt (castStrictTVar tv :: StrictTVar m a)
instance NoThunks (StrictSVar m a)
=> NoThunks (StrictSVar (WithEarlyExit m) a) where
showTypeOf _ = "StrictSVar (WithEarlyExit m)"
wNoThunks ctxt tv = do
wNoThunks ctxt (castStrictSVar tv :: StrictSVar m a)
-- | Internal only
earlyExit :: m (Maybe a) -> WithEarlyExit m a
earlyExit = WithEarlyExit . MaybeT
withEarlyExit :: WithEarlyExit m a -> m (Maybe a)
withEarlyExit = runMaybeT . unWithEarlyExit
withEarlyExit_ :: Functor m => WithEarlyExit m () -> m ()
withEarlyExit_ = fmap collapse . withEarlyExit
collapse :: Maybe () -> ()
collapse Nothing = ()
collapse (Just ()) = ()
exitEarly :: Applicative m => WithEarlyExit m a
exitEarly = earlyExit $ pure Nothing
instance (forall a'. NoThunks (m a'))
=> NoThunks (WithEarlyExit m a) where
showTypeOf _p = "WithEarlyExit " ++ showTypeOf (Proxy @(m a))
wNoThunks ctxt = wNoThunks ctxt . withEarlyExit
{-------------------------------------------------------------------------------
Instances for io-classes
-------------------------------------------------------------------------------}
instance MonadSTM m => MonadSTM (WithEarlyExit m) where
type STM (WithEarlyExit m) = WithEarlyExit (STM m)
atomically = earlyExit . atomically . withEarlyExit
type TVar (WithEarlyExit m) = TVar m
type TMVar (WithEarlyExit m) = TMVar m
type TQueue (WithEarlyExit m) = TQueue m
type TBQueue (WithEarlyExit m) = TBQueue m
type TArray (WithEarlyExit m) = TArray m
type TSem (WithEarlyExit m) = TSem m
type TChan (WithEarlyExit m) = TChan m
newTVar = lift . newTVar
readTVar = lift . readTVar
writeTVar = lift .: writeTVar
retry = lift retry
orElse = (earlyExit .: orElse) `on` withEarlyExit
newTMVar = lift . newTMVar
newEmptyTMVar = lift newEmptyTMVar
takeTMVar = lift . takeTMVar
tryTakeTMVar = lift . tryTakeTMVar
putTMVar = lift .: putTMVar
tryPutTMVar = lift .: tryPutTMVar
readTMVar = lift . readTMVar
tryReadTMVar = lift . tryReadTMVar
swapTMVar = lift .: swapTMVar
isEmptyTMVar = lift . isEmptyTMVar
newTQueue = lift newTQueue
readTQueue = lift . readTQueue
tryReadTQueue = lift . tryReadTQueue
peekTQueue = lift . peekTQueue
tryPeekTQueue = lift . tryPeekTQueue
flushTQueue = lift . flushTQueue
writeTQueue = lift .: writeTQueue
isEmptyTQueue = lift . isEmptyTQueue
unGetTQueue = lift .: unGetTQueue
newTBQueue = lift . newTBQueue
readTBQueue = lift . readTBQueue
tryReadTBQueue = lift . tryReadTBQueue
peekTBQueue = lift . peekTBQueue
tryPeekTBQueue = lift . tryPeekTBQueue
flushTBQueue = lift . flushTBQueue
writeTBQueue = lift .: writeTBQueue
lengthTBQueue = lift . lengthTBQueue
isEmptyTBQueue = lift . isEmptyTBQueue
isFullTBQueue = lift . isFullTBQueue
unGetTBQueue = lift .: unGetTBQueue
newTSem = lift . newTSem
waitTSem = lift . waitTSem
signalTSem = lift . signalTSem
signalTSemN = lift .: signalTSemN
newTChan = lift newTChan
newBroadcastTChan = lift newBroadcastTChan
dupTChan = lift . dupTChan
cloneTChan = lift . cloneTChan
readTChan = lift . readTChan
tryReadTChan = lift . tryReadTChan
peekTChan = lift . peekTChan
tryPeekTChan = lift . tryPeekTChan
writeTChan = lift .: writeTChan
unGetTChan = lift .: unGetTChan
isEmptyTChan = lift . isEmptyTChan
newTMVarIO = lift . newTMVarIO
newEmptyTMVarIO = lift newEmptyTMVarIO
instance (MonadMVar m, MonadMask m, MonadEvaluate m)
=> MonadMVar (WithEarlyExit m) where
type MVar (WithEarlyExit m) = MVar m
newEmptyMVar = lift newEmptyMVar
takeMVar = lift . takeMVar
putMVar = lift .: putMVar
tryTakeMVar = lift . tryTakeMVar
tryPutMVar = lift .: tryPutMVar
tryReadMVar = lift . tryReadMVar
isEmptyMVar = lift . isEmptyMVar
newMVar = lift . newMVar
readMVar = lift . readMVar
swapMVar = lift .: swapMVar
instance MonadCatch m => MonadThrow (WithEarlyExit m) where
throwIO = lift . throwIO
instance MonadCatch m => MonadCatch (WithEarlyExit m) where
catch act handler = earlyExit $
catch (withEarlyExit act) (withEarlyExit . handler)
generalBracket acquire release use = earlyExit $ do
-- This is modelled on the case for ErrorT, except that we don't have
-- to worry about reporting the right error, since we only have @Nothing@
(mb, mc) <- generalBracket
(withEarlyExit acquire)
(\mResource exitCase ->
case (mResource, exitCase) of
(Nothing, _) ->
-- resource not acquired
return Nothing
(Just resource, ExitCaseSuccess (Just b)) ->
withEarlyExit $ release resource (ExitCaseSuccess b)
(Just resource, ExitCaseException e) ->
withEarlyExit $ release resource (ExitCaseException e)
(Just resource, _otherwise) ->
withEarlyExit $ release resource ExitCaseAbort
)
(maybe (return Nothing) (withEarlyExit . use))
return $ (,) <$> mb <*> mc
instance MonadMask m => MonadMask (WithEarlyExit m) where
mask f = earlyExit $
mask $ \unmask ->
withEarlyExit (f (earlyExit . unmask . withEarlyExit))
uninterruptibleMask f = earlyExit $
uninterruptibleMask $ \unmask ->
let unmask' :: forall a. WithEarlyExit m a -> WithEarlyExit m a
unmask' = earlyExit . unmask . withEarlyExit
in withEarlyExit (f unmask')
instance MonadThread m => MonadThread (WithEarlyExit m) where
type ThreadId (WithEarlyExit m) = ThreadId m
myThreadId = lift myThreadId
labelThread = lift .: labelThread
instance (MonadMask m, MonadAsync m, MonadCatch (STM m))
=> MonadAsync (WithEarlyExit m) where
type Async (WithEarlyExit m) = WithEarlyExit (Async m)
async = lift . (fmap earlyExit . async) . withEarlyExit
asyncBound = lift . (fmap earlyExit . async) . withEarlyExit
asyncOn n = lift . (fmap earlyExit . asyncOn n) . withEarlyExit
asyncThreadId = asyncThreadId
cancel a = lift $ cancel (withEarlyExit a)
cancelWith a = lift . cancelWith (withEarlyExit a)
waitCatchSTM a = earlyExit (commute <$> waitCatchSTM (withEarlyExit a))
pollSTM a = earlyExit (fmap commute <$> pollSTM (withEarlyExit a))
asyncWithUnmask f = earlyExit $ fmap (Just . earlyExit) $
asyncWithUnmask $ \unmask ->
withEarlyExit (f (earlyExit . unmask . withEarlyExit))
asyncOnWithUnmask n f = earlyExit $ fmap (Just . earlyExit) $
asyncOnWithUnmask n $ \unmask ->
withEarlyExit (f (earlyExit . unmask . withEarlyExit))
commute :: Either SomeException (Maybe a) -> Maybe (Either SomeException a)
commute (Left e) = Just (Left e)
commute (Right Nothing) = Nothing
commute (Right (Just a)) = Just (Right a)
instance MonadFork m => MonadFork (WithEarlyExit m) where
forkIO f = lift $ forkIO (collapse <$> withEarlyExit f)
forkOn n f = lift $ forkOn n (collapse <$> withEarlyExit f)
forkIOWithUnmask f = lift $ forkIOWithUnmask $ \unmask ->
let unmask' :: forall a. WithEarlyExit m a -> WithEarlyExit m a
unmask' = earlyExit . unmask . withEarlyExit
in collapse <$> withEarlyExit (f unmask')
forkFinally f fin = lift $ forkFinally
(withEarlyExit f)
(withEarlyExit_ . maybe (pure ()) fin . commute)
throwTo = lift .: throwTo
yield = lift yield
instance PrimMonad m => PrimMonad (WithEarlyExit m) where
type PrimState (WithEarlyExit m) = PrimState m
primitive = lift . primitive
{-# INLINE primitive #-}
instance MonadST m => MonadST (WithEarlyExit m) where
stToIO = lift . stToIO
withLiftST k = k stToIO
instance MonadMonotonicTimeNSec m => MonadMonotonicTimeNSec (WithEarlyExit m) where
getMonotonicTimeNSec = lift getMonotonicTimeNSec
instance MonadMonotonicTime m => MonadMonotonicTime (WithEarlyExit m) where
getMonotonicTime = lift getMonotonicTime
instance MonadDelay m => MonadDelay (WithEarlyExit m) where
threadDelay = lift . threadDelay
instance TimerSI.MonadDelay m => TimerSI.MonadDelay (WithEarlyExit m) where
threadDelay = lift . TimerSI.threadDelay
instance (MonadEvaluate m, MonadCatch m) => MonadEvaluate (WithEarlyExit m) where
evaluate = lift . evaluate
instance MonadEventlog m => MonadEventlog (WithEarlyExit m) where
traceEventIO = lift . traceEventIO
traceMarkerIO = lift . traceMarkerIO
{-------------------------------------------------------------------------------
Finally, the consensus IOLike wrapper
-------------------------------------------------------------------------------}
instance ( IOLike m
, forall a. NoThunks (StrictTVar (WithEarlyExit m) a)
, forall a. NoThunks (StrictSVar (WithEarlyExit m) a)
, forall a. NoThunks (StrictMVar (WithEarlyExit m) a)
) => IOLike (WithEarlyExit m) where
forgetSignKeyKES = lift . forgetSignKeyKES