-
Notifications
You must be signed in to change notification settings - Fork 42
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
1fc9dba
commit 63a560f
Showing
7 changed files
with
154 additions
and
13 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
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
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,18 @@ | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
|
||
module Lang.Crucible.Utils.Seconds | ||
( Seconds | ||
, secondsToInt | ||
, secondsFromInt | ||
, secondsToMicroseconds | ||
) where | ||
|
||
newtype Seconds = Seconds { secondsToInt :: Int } | ||
deriving (Eq, Num, Ord, Show) | ||
|
||
-- | Inverse of 'secondsToInt' | ||
secondsFromInt :: Int -> Seconds | ||
secondsFromInt = Seconds | ||
|
||
secondsToMicroseconds :: Seconds -> Int | ||
secondsToMicroseconds = (* 1000000) . secondsToInt |
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,58 @@ | ||
module Lang.Crucible.Utils.Timeout | ||
( Timeout(..) | ||
, TimeoutError(..) | ||
, withTimeout | ||
) where | ||
|
||
import qualified Control.Concurrent as CC | ||
import qualified Control.Concurrent.Async as CCA | ||
import Control.Exception.Base (SomeException) | ||
|
||
import qualified Lang.Crucible.Utils.Seconds as Secs | ||
|
||
-- | A timeout, in seconds. | ||
newtype Timeout = Timeout { getTimeout :: Secs.Seconds } | ||
deriving (Eq, Ord, Show) | ||
|
||
-- Private, not exported | ||
timeoutToMicros :: Timeout -> Int | ||
timeoutToMicros = Secs.secondsToMicroseconds . getTimeout | ||
|
||
-- Private, not exported | ||
data DidTimeOut = DidTimeOut | ||
|
||
-- | An error resulting from 'withTimeout'. | ||
data TimeoutError | ||
= -- | The task timed out | ||
TimedOut | ||
-- | Some other exception ocurred | ||
| Exception SomeException | ||
deriving Show | ||
|
||
-- | Execute a task with a timeout. | ||
-- | ||
-- Catches any exceptions that occur during the task, returning them as | ||
-- @'Left' ('Exception' exn)@. | ||
withTimeout :: | ||
-- | Timeout duration (seconds) | ||
Timeout -> | ||
-- | Task to attempt | ||
IO a -> | ||
IO (Either TimeoutError a) | ||
withTimeout to task = do | ||
worker <- CCA.async task | ||
timeout <- CCA.async $ do | ||
CC.threadDelay (timeoutToMicros to) | ||
CCA.cancel worker | ||
return DidTimeOut | ||
res <- CCA.waitEitherCatch timeout worker | ||
case res of | ||
Left (Right DidTimeOut) -> do | ||
return (Left TimedOut) | ||
Left (Left exn) -> do | ||
return (Left (Exception exn)) | ||
Right (Left exn) -> do | ||
return (Left (Exception exn)) | ||
Right (Right val) -> | ||
return (Right val) | ||
|