blob: 0cf82f3b241959007395a95106d7940d4a9f769b (
plain)
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
|
{-# OPTIONS_GHC -cpp #-}
{-# LANGUAGE ForeignFunctionInterface #-}
module Main where
import Control.Concurrent
haskellFun :: Int -> IO ()
haskellFun c = putStrLn ("Haskell: " ++ show c)
foreign export ccall "hFun" haskellFun :: Int -> IO ()
foreign import ccall safe "hFun" hFun :: Int -> IO ()
#ifdef mingw32_HOST_OS
foreign import stdcall safe "Sleep" _sleepBlock :: Int -> IO ()
sleepBlock n = _sleepBlock (n*1000)
#else
foreign import ccall safe "sleep" sleepBlock :: Int -> IO ()
#endif
main :: IO ()
main = do
th <- newEmptyMVar
forkIO $ do
putStrLn "newThread started"
sleepBlock 1
putStrLn "newThread back again"
putMVar th "1 sec later"
threadDelay 500000 >> putStrLn "mainThread"
-- this will not be blocked in the threaded RTS
forkIO $ (hFun 2)
-- neither will this
x <- takeMVar th
putStrLn x
putStrLn "\nshutting down"
|