
***************** Native Threads Proposal, version 2
Some "foreign" libraries (for example OpenGL) rely on a mechanism called thread-local storage. The meaning of an OpenGL call therefore usually depends on which OS thread it is called from. Therefore, some kind of direct mapping from Haskell threads to OS threads is necessary in order to use the affected foreign libraries. Executing every haskell thread in its own OS thread is not feasible for performance reasons. However, perfomance of native OS threads is not too bad as long as there aren't too many, so I propose that some threads get their own OS threads, and some don't:
Every Haskell Thread can be either a "green" thread or a "native" thread. For each "native" thread, there is exactly one OS thread created by the RTS.
Is there an invariant that there is only one Haskell thread for each native thread?
For a green thread, it is unspecified which OS thread it is executed in. The main program and all haskell threads forked using forkIO are green threads. Threads forked using forkNativeThread :: IO () -> IO () are native threads. (Note: The type of the current haskell thread does _not_ matter when forking new threads)
Execution of a green thread might move from one OS thread to another at any time. A "green" thread is never executed in an OS thread that is reserved for a "native" thread. A "native" haskell thread and all foreign imported functions that it calls are executed in its associated OS thread. A foreign exported callback that is called from C code executing in that OS thread is executed in the native haskell thread. A foreign exported callback that is called from C code executing in an OS thread that is not associated with a "native" haskell thread is executed in a new green haskell thread.
Only one OS thread can execute Haskell code at any given time.
If a "native" haskell thread enters a foreign imported function that is marked as "safe" or "threadsafe", all other Haskell threads keep running. If the imported function is marked as "unsafe", no other threads are executed until the call finishes.
I'd change this paragraph as follows: If a "native" haskell thread enters a foreign imported function that is marked as "threadsafe", all other Haskell threads keep running. If the imported function is marked as "safe" or "unsafe", no other threads are executed until the call finishes. in other words, foreign imports for native threads behave just like they do for green threads. As Simon P.J. pointed out, we don't do any OS-thread synchronisation on a "safe" foreign import.
Other things I'm not sure about: What should we do get if a foreign function spawns a new OS thread and executes a haskell callback in that OS thread? Should a new native haskell thread that executes in the OS thread be created? Should the new OS thread be blocked and the callback executed in a green thread? What does the current threaded RTS do? (I assume the non-threaded RTS will just crash?)
The Haskell callback will execute in a green thread, according to the proposal above, and I think that's the right thing to do. It's possible to get into a situation in which several Haskell threads in the system are associated with the same OS thread: - Haskell native thread calls a foreign import, which - invokes a Haskell callback, which - calls a foreign import, which - invokes another Haskell callback at this point the two Haskell threads created by the callbacks are both attached to the same native thread. One of them is blocked waiting for its foreign import to return, however. I'm not sure if this is important. Cheers, Simon
participants (1)
-
Simon Marlow