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

Commit

Permalink
Reorganize shared modules
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielG committed Mar 1, 2017
1 parent 6216cbd commit 460f3cd
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 4 deletions.
File renamed without changes.
8 changes: 4 additions & 4 deletions ghc-mod.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ Library
Default-Extensions: ScopedTypeVariables, RecordWildCards, NamedFieldPuns,
ConstraintKinds, FlexibleContexts,
DataKinds, KindSignatures, TypeOperators, ViewPatterns
HS-Source-Dirs: ., core
HS-Source-Dirs: ., core, shared
Exposed-Modules:
GhcMod
GhcModExe.Boot
Expand Down Expand Up @@ -217,7 +217,7 @@ Executable ghc-mod
Default-Language: Haskell2010
Main-Is: GHCModWrapper.hs
Other-Modules: Paths_ghc_mod
HS-Source-Dirs: src, .
HS-Source-Dirs: ., src, shared
GHC-Options: -Wall
Build-Depends: base < 5 && >= 4.0
, directory < 1.4
Expand All @@ -239,7 +239,7 @@ Executable ghc-mod-real
, GHCMod.Options.ShellParse
GHC-Options: -Wall -fno-warn-deprecations -threaded
Default-Extensions: ConstraintKinds, FlexibleContexts
HS-Source-Dirs: src
HS-Source-Dirs: src, shared
X-Internal: True
Build-Depends:
base
Expand Down Expand Up @@ -268,7 +268,7 @@ Executable ghc-modi
if os(windows)
Cpp-Options: -DWINDOWS
Default-Extensions: ConstraintKinds, FlexibleContexts
HS-Source-Dirs: src, .
HS-Source-Dirs: ., src, shared
Build-Depends:
-- See Note [GHC Boot libraries]
base
Expand Down
File renamed without changes.
57 changes: 57 additions & 0 deletions shared/System/Process/Concurrent.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
module System.Process.Concurrent where

import Control.Concurrent.MVar
import System.Process
import System.Exit (ExitCode)
import System.IO (Handle)

newtype CProcessHandle = CProcessHandle (MVar ProcessHandleState)

data ProcessHandleState = OpenHandle ProcessHandle
| WaitingOn ProcessHandle (MVar ExitCode)
| ClosedHandle ExitCode

createCProcess :: CreateProcess
-> IO ( Maybe Handle
, Maybe Handle
, Maybe Handle
, CProcessHandle
)
createCProcess p = do
(i, o, e, h) <- createProcess p
ch <- mkCProcessHandle h
return (i, o, e, ch)

mkCProcessHandle :: ProcessHandle -> IO CProcessHandle
mkCProcessHandle handle =
CProcessHandle <$> newMVar (OpenHandle handle)

waitForCProcess :: CProcessHandle -> IO ExitCode
waitForCProcess (CProcessHandle mv) = do
phs <- takeMVar mv
-- TODO: What happens when an exception occurs in here?
case phs of
OpenHandle handle -> do
emv <- newEmptyMVar
putMVar mv $ WaitingOn handle emv
rv <- waitForProcess handle
putMVar emv rv
return rv
WaitingOn _handle emv -> do
putMVar mv phs
takeMVar emv
ClosedHandle rv -> do
putMVar mv phs
return rv

terminateCProcess :: CProcessHandle -> IO ()
terminateCProcess (CProcessHandle mv) = do
phs <- takeMVar mv
case phs of
OpenHandle handle -> do
terminateProcess handle
WaitingOn handle _ -> do
terminateProcess handle
_ -> return ()

putMVar mv phs
File renamed without changes.

0 comments on commit 460f3cd

Please sign in to comment.