Releasing resources associated with a RTS worker thread?

Joe Hendrix jhendrix at
Mon Aug 24 06:23:40 UTC 2015

I am working on FFI bindings to C++ code that associates several memory pools to each thread using the library.  When a thread is done using the library, it can call a C function to release the memory pool objects.

In the Haskell bindings, I’d like to be able to attach a finalizer that called this cleanup code whenever an RTS OS thread is shutdown.  It appeared that the main place that happens is here:

I did not see a way to register a finalizer that is run before the call to shutdownThread.  Could this be something worth adding to the RTS?

It seems like the most straightforward way would be to add a finalizer list to each task object, and expose a C function for adding callbacks to it.

As a fallback, it should be possible to modify the memory pools in the C++ code so that they can be disabled.   According to the author of the C++ code, this should have about a 10% slowdown on execution, so it would be nice if I could keep them enabled without a risk of memory leaks.


More information about the ghc-devs mailing list