How to detect if a program has been compiled using -threaded?

寵の児 提交于 2020-01-11 04:32:05

问题


I'm working on a Haskell daemon that uses POSIX fork/exec together with file locking mechanism. My experiments show that file locks aren't inherited during executeFile with -threaded runtime (see also this thread), no matter if I use +RTS -N or not. So I'd like to add a check to be sure that the daemon ins't compiled with -threaded. Is there a portable way to detect it?


回答1:


There is a value in Control.Concurrent for this, for example:

module Main (main) where

import Control.Concurrent

main :: IO ()
main = print rtsSupportsBoundThreads

And 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

And it's C-part source code:

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



回答2:


This is a dirty hack and might be not portable but I can confirm it works for ghc-7.6.3 on 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

See BlockedOnOther docstring for details.



来源:https://stackoverflow.com/questions/22839824/how-to-detect-if-a-program-has-been-compiled-using-threaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!