Thread-Safety through FFI

Simon Marlow simonmar@microsoft.com
Wed, 24 Oct 2001 10:52:26 +0100


> Is there a way of calling exported 'dynamic export' functions in a=20
> thread-safe manner? I have this problem in JVM-Bridge...
>=20
> This is what the main thread does, from Haskell:
>=20
> 1. starts the Java VM, which starts a bunch of other threads,=20
> including=20
> the AWT thread;
> 2. creates a pointer to a some IO function 'paint', using a 'dynamic=20
> export' callback-maker;
> 3. creates a new Java Frame (i.e. a GUI window) with the=20
> function-pointer;
> 4. repeatedly calls the Java Thread.yield function for 20 seconds.
> (the program then ends)
>=20
> Now, once the Java Frame has been created, the AWT thread will=20
> occasionally call the 'paint' method, which ends up calling the=20
> function-pointer. However, it seems GHC 5.02 'dynamic export'=20
> calls are=20
> not thread-safe, and sooner or later it crashes, either with a seg=20
> violation or a complaint about unknown closure types.

If you're talking about OS threads (eg. POSIX threads), then GHC's RTS
certainly isn't thread safe.  It isn't safe to call foreign exported
functions from more that one OS thread simultaneously.  We have some
partially working thread support, but it has been on the back burner for
some time now.

Cheers,
	Simon