Hello everyone,
I'm currently calling Haskell code from C. My goal is to apply a Haskell function to each element of some dataset that I get from outside Haskell-land. For now, before I make this fancier and more efficient, the plan is to just bring the RTS up with hs_init, call my Haskell function (exporter to C with 'foreign export ccall [...]') on the element and finally shut the RTS down, doing this for every element.
When running this, I realized the RTS was running into a segfault when calling the Haskell function on the second element. This led me to believe it wasn't possible to call hs_init()/hs_exit() multiple times in the same program. But then I checked the Haskell 2010 report, FFI section [1], and it says:
The function hs_init() initialises the Haskell system and
provides it with the available command line arguments. Upon return, the arguments solely intended for the Haskell
runtime system are removed (i.e., the values that argc and argv point to may have changed). This function must be
called during program startup before any Haskell function is invoked; otherwise, the system behaviour is undefined.
Conversely, the Haskell system is deinitialised by a call to hs_exit(). Multiple invocations of hs_init() are
permitted, provided that they are followed by an equal number of calls to hs_exit() and that the first call to
hs_exit() is after the last call to hs_init(). In addition to nested calls to hs_init(), the Haskell
system may be de-initialised with hs_exit() and be re-initialised with hs_init() at a later point in
time. This ensures that repeated initialisation due to multiple libraries being implemented in Haskell is
covered.
Which means, if I understand correctly, that what I want, while very inefficient, should work fine.