haskellconcurrencyprocessghc

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


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?


Solution

  • There is the rtsSupportsBoundThreads value value in Control.Concurrent for this. For example:

    module Main (main) where
    
    import Control.Concurrent
    
    main :: IO ()
    main = print rtsSupportsBoundThreads
    

    And during 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
    

    Here is its C-part source code:

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