2014-04-03 12 views
13

Sto lavorando a un demone Haskell che utilizza POSIX fork/exec insieme al meccanismo di blocco dei file. I miei esperimenti mostrano che i blocchi di file non vengono ereditati durante il periodo executeFile con il runtime -threaded (vedere anche this thread), non importa se io uso +RTS -N o no. Quindi vorrei aggiungere un assegno per essere sicuro che il demone non sia compilato con -threaded. C'è un modo portatile per rilevarlo?Come rilevare se un programma è stato compilato usando -threaded?

+3

'setNumCapabilities 2' e controlla che 'getNumCapabilities' restituisca 1? – Yuras

risposta

13

C'è un value in Control.Concurrent per questo, ad esempio:

module Main (main) where 

import Control.Concurrent 

main :: IO() 
main = print rtsSupportsBoundThreads 

e test:

$ ghc -fforce-recomp Test.hs; ./Test 
[1 of 1] Compiling Main    (Test.hs, Test.o) 
Linking Test ... 
False 
$ ghc -fforce-recomp -threaded Test.hs; ./Test 
[1 of 1] Compiling Main    (Test.hs, Test.o) 
Linking Test ... 
True 

Ed è C-parte source code:

HsBool 
rtsSupportsBoundThreads(void) 
{ 
#if defined(THREADED_RTS) 
    return HS_BOOL_TRUE; 
#else 
    return HS_BOOL_FALSE; 
#endif 
} 
1

Questo è un hack sporco e potrebbe non essere portatile, ma posso confermare che funziona per GHC-7.6.3 su Linux:

isThreaded :: IO (Maybe Bool) 
isThreaded = do 
    tid <- forkIO $ threadDelay 1000000 
    yield 
    stat <- threadStatus tid 
    killThread tid 
    case stat of 
    ThreadBlocked BlockedOnMVar -> return (Just True) 
    ThreadBlocked BlockedOnOther -> return (Just False) 
    _       -> return Nothing 

Vedi BlockedOnOther docstring per i dettagli.

Problemi correlati