-
Notifications
You must be signed in to change notification settings - Fork 0
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
Showing
13 changed files
with
224 additions
and
43 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 |
---|---|---|
@@ -1,2 +1,3 @@ | ||
dist-newstyle/ | ||
.envrc | ||
niio |
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,4 @@ | ||
packages: . | ||
|
||
package debuggable | ||
tests: True |
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,5 @@ | ||
packages: . | ||
|
||
package debuggable | ||
tests: True | ||
ghc-options: -Werror |
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,78 @@ | ||
module Cmdline ( | ||
Cmdline(..) | ||
, Demo(..) | ||
, UseDebuggable(..) | ||
, getCmdline | ||
) where | ||
|
||
import Options.Applicative | ||
|
||
{------------------------------------------------------------------------------- | ||
Top-level | ||
-------------------------------------------------------------------------------} | ||
|
||
data Cmdline = Cmdline { | ||
cmdDemo :: Maybe Demo | ||
} | ||
deriving stock (Show) | ||
|
||
data Demo = | ||
DemoNIIO UseDebuggable | ||
| DemoCallSite UseDebuggable | ||
deriving stock (Show) | ||
|
||
data UseDebuggable = | ||
UseDebuggable | ||
| WithoutDebuggable | ||
deriving stock (Show) | ||
|
||
getCmdline :: IO Cmdline | ||
getCmdline = execParser opts | ||
where | ||
opts :: ParserInfo Cmdline | ||
opts = info (parseCmdline <**> helper) $ mconcat [ | ||
fullDesc | ||
, header "Demo of the debuggable package" | ||
] | ||
|
||
{------------------------------------------------------------------------------- | ||
Parser | ||
-------------------------------------------------------------------------------} | ||
|
||
parseCmdline :: Parser Cmdline | ||
parseCmdline = | ||
Cmdline | ||
<$> optional parseDemo | ||
|
||
parseDemo :: Parser Demo | ||
parseDemo = subparser $ mconcat [ | ||
command' | ||
"niio" | ||
(DemoNIIO <$> parseUseDebuggable) | ||
"Demo Debug.NonInterleavedIO" | ||
, command' | ||
"provenance" | ||
(DemoCallSite <$> parseUseDebuggable) | ||
"Demo CallSite from Debug.Provenance" | ||
] | ||
|
||
parseUseDebuggable :: Parser UseDebuggable | ||
parseUseDebuggable = asum [ | ||
flag' UseDebuggable $ mconcat [ | ||
long "use-debuggable" | ||
, help "Use the debuggable library" | ||
] | ||
, flag' WithoutDebuggable $ mconcat [ | ||
long "without-debuggable" | ||
, help "Show what happens if we don't have the library support" | ||
] | ||
] | ||
|
||
{------------------------------------------------------------------------------- | ||
Auxiliary optparse-applicative | ||
-------------------------------------------------------------------------------} | ||
|
||
command' :: String -> Parser a -> String -> Mod CommandFields a | ||
command' cmd parser desc = command cmd $ info parser $ mconcat [ | ||
progDesc desc | ||
] |
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,19 @@ | ||
module Demo (main) where | ||
|
||
import Cmdline | ||
|
||
import Demo.NIIO qualified as NIIO | ||
import Demo.CallSite qualified as CallSite | ||
|
||
main :: IO () | ||
main = do | ||
Cmdline{cmdDemo} <- getCmdline | ||
case cmdDemo of | ||
Nothing -> putStrLn "Please select a demo (see --help)" | ||
Just demo -> | ||
case demo of | ||
DemoNIIO WithoutDebuggable -> NIIO.withoutDebuggable | ||
DemoNIIO UseDebuggable -> NIIO.useDebuggable | ||
DemoCallSite WithoutDebuggable -> CallSite.withoutDebuggable | ||
DemoCallSite UseDebuggable -> CallSite.useDebuggable | ||
|
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,42 @@ | ||
module Demo.NIIO (withoutDebuggable, useDebuggable) where | ||
|
||
import Control.Concurrent | ||
import Control.Concurrent.Async | ||
import Control.Monad | ||
import System.IO | ||
|
||
import Debug.NonInterleavedIO qualified as NIIO | ||
|
||
{------------------------------------------------------------------------------- | ||
Without the library | ||
-------------------------------------------------------------------------------} | ||
|
||
withoutDebuggable :: IO () | ||
withoutDebuggable = do | ||
hSetBuffering stdout NoBuffering | ||
|
||
concurrently_ | ||
( replicateM_ 10 $ do | ||
putStrLn "This is a message from the first thread" | ||
threadDelay 100_000 | ||
) | ||
( replicateM_ 10 $ do | ||
putStrLn "And this is a message from the second thread" | ||
threadDelay 100_000 | ||
) | ||
|
||
{------------------------------------------------------------------------------- | ||
Using the library | ||
-------------------------------------------------------------------------------} | ||
|
||
useDebuggable :: IO () | ||
useDebuggable = do | ||
concurrently_ | ||
( replicateM_ 10 $ do | ||
NIIO.putStrLn "This is a message from the first thread" | ||
threadDelay 100_000 | ||
) | ||
( replicateM_ 10 $ do | ||
NIIO.putStrLn "And this is a message from the second thread" | ||
threadDelay 100_000 | ||
) |
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
Oops, something went wrong.