Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 536
Petr Avatar asked Apr 03 '14 13:04

Petr


People also ask

How can we determine at runtime when the code is executed If we are on a 32 bit or 64 bit architecture?

To see whether your process is 64-bit or 32-bit you can simply check the IntPtr. Size or any other pointer type. If you get 4 then you are running on 32-bit. If you get 8 then you are running on 64-bit.

What happens when code gets compiled?

A compiler takes the program code (source code) and converts the source code to a machine language module (called an object file). Another specialized program, called a linker, combines this object file with other previously compiled object files (in particular run-time modules) to create an executable file.

Where to put makefile?

some projects put their makefile in src/ subdirectory of the root directories of the projects, some projects put their makefiles in the root directory of the project.


2 Answers

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
}
like image 130
Fedor Gogolev Avatar answered Oct 25 '22 05:10

Fedor Gogolev


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.

like image 22
Samvel Truzyan Avatar answered Oct 25 '22 06:10

Samvel Truzyan