Skip to content
This repository was archived by the owner on Apr 25, 2020. It is now read-only.

Commit 460f3cd

Browse files
committed
Reorganize shared modules
1 parent 6216cbd commit 460f3cd

File tree

5 files changed

+61
-4
lines changed

5 files changed

+61
-4
lines changed
File renamed without changes.

ghc-mod.cabal

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ Library
109109
Default-Extensions: ScopedTypeVariables, RecordWildCards, NamedFieldPuns,
110110
ConstraintKinds, FlexibleContexts,
111111
DataKinds, KindSignatures, TypeOperators, ViewPatterns
112-
HS-Source-Dirs: ., core
112+
HS-Source-Dirs: ., core, shared
113113
Exposed-Modules:
114114
GhcMod
115115
GhcModExe.Boot
@@ -217,7 +217,7 @@ Executable ghc-mod
217217
Default-Language: Haskell2010
218218
Main-Is: GHCModWrapper.hs
219219
Other-Modules: Paths_ghc_mod
220-
HS-Source-Dirs: src, .
220+
HS-Source-Dirs: ., src, shared
221221
GHC-Options: -Wall
222222
Build-Depends: base < 5 && >= 4.0
223223
, directory < 1.4
@@ -239,7 +239,7 @@ Executable ghc-mod-real
239239
, GHCMod.Options.ShellParse
240240
GHC-Options: -Wall -fno-warn-deprecations -threaded
241241
Default-Extensions: ConstraintKinds, FlexibleContexts
242-
HS-Source-Dirs: src
242+
HS-Source-Dirs: src, shared
243243
X-Internal: True
244244
Build-Depends:
245245
base
@@ -268,7 +268,7 @@ Executable ghc-modi
268268
if os(windows)
269269
Cpp-Options: -DWINDOWS
270270
Default-Extensions: ConstraintKinds, FlexibleContexts
271-
HS-Source-Dirs: src, .
271+
HS-Source-Dirs: ., src, shared
272272
Build-Depends:
273273
-- See Note [GHC Boot libraries]
274274
base
File renamed without changes.
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
module System.Process.Concurrent where
2+
3+
import Control.Concurrent.MVar
4+
import System.Process
5+
import System.Exit (ExitCode)
6+
import System.IO (Handle)
7+
8+
newtype CProcessHandle = CProcessHandle (MVar ProcessHandleState)
9+
10+
data ProcessHandleState = OpenHandle ProcessHandle
11+
| WaitingOn ProcessHandle (MVar ExitCode)
12+
| ClosedHandle ExitCode
13+
14+
createCProcess :: CreateProcess
15+
-> IO ( Maybe Handle
16+
, Maybe Handle
17+
, Maybe Handle
18+
, CProcessHandle
19+
)
20+
createCProcess p = do
21+
(i, o, e, h) <- createProcess p
22+
ch <- mkCProcessHandle h
23+
return (i, o, e, ch)
24+
25+
mkCProcessHandle :: ProcessHandle -> IO CProcessHandle
26+
mkCProcessHandle handle =
27+
CProcessHandle <$> newMVar (OpenHandle handle)
28+
29+
waitForCProcess :: CProcessHandle -> IO ExitCode
30+
waitForCProcess (CProcessHandle mv) = do
31+
phs <- takeMVar mv
32+
-- TODO: What happens when an exception occurs in here?
33+
case phs of
34+
OpenHandle handle -> do
35+
emv <- newEmptyMVar
36+
putMVar mv $ WaitingOn handle emv
37+
rv <- waitForProcess handle
38+
putMVar emv rv
39+
return rv
40+
WaitingOn _handle emv -> do
41+
putMVar mv phs
42+
takeMVar emv
43+
ClosedHandle rv -> do
44+
putMVar mv phs
45+
return rv
46+
47+
terminateCProcess :: CProcessHandle -> IO ()
48+
terminateCProcess (CProcessHandle mv) = do
49+
phs <- takeMVar mv
50+
case phs of
51+
OpenHandle handle -> do
52+
terminateProcess handle
53+
WaitingOn handle _ -> do
54+
terminateProcess handle
55+
_ -> return ()
56+
57+
putMVar mv phs
File renamed without changes.

0 commit comments

Comments
 (0)