Native Threads in the RTS
Thu, 28 Nov 2002 09:02:15 -0000
Improving. Want to put it in CVS? Simon M can suggest where.
| A native thread (aka OS thread) is a thread as defined by the
| A Haskell thread is [*** FIXME - How shall I put this? ***] the thing
| you see from Haskell land.
A "Haskell thread" encapsulates the execution of a Haskell I/O action.
A Haskell thread is created by forkIO, and dies when the I/O action
A Haskell thread is always executed by a native thread. The Haskell RTS
creates one or more "worker native threads" to execute Haskell threads.
| Haskell threads may be associated at thread creation time with either
| zero or one native threads. Each Native thread is associated with zero
| or more native threads.
zero or more *Haskell* threads.
Can you give an example of when a native thread is associated with more
than one Haskell thread?
To avoid "A Haskell thread associated with a native thread" I'd prefer
to define the term "a bound Haskell thread". I would also like to
describe a "bound native thread" as one that has associated Haskell
| If a native thread is associated with one or more Haskell threads,
| exactly one of the following must be true:
| *) Exactly one Haskell thread associated with the native thread is
| *) The native thread is executing foreign code.
| *) The native thread and all Haskell threads associated with it are
You don't say (but you do mean)
A bound Haskell thread can be executed only by its associated
You don't say (and I'm not sure if you mean)
If a bound native thread blocks, all of its associated Haskell=20
threads are blocked too
If a bound Haskell thread blocks, its associate native thread
and all its=20
associated Haskell threads also block.
| The thread that main runs in, threads created using forkIO and threads
| created for running finalizers or signal handlers are not necessarily
| associated with a native thread. However, an implementation might
| choose to do so.
But the impl may *not* choose a bound native thread. These must be kept
| When a "free" foreign exported function is invoked, the implementation
| may freely choose what kind of Haskell thread the function is executed
| in. It is not specified whether this thread is associated with a
| particular OS thread or not.
Again, it must not be a bound native thread.
| When a foreign imported function is invoked [by Haskell code], the
| foreign code is executed in the native thread associated with the
| current Haskell thread, if an association exists. If the current
| Haskell thread is not associated to a native thread, the
| may freely decide which thread to run the foreign function in.
| The existing distinction between unsafe, safe and threadsafe calls
| remains unchanged.
If a bound Haskell thread=20
calls a foreign import that is not labelled 'threadsafe'
which calls a bound foreign export
does that work? What if the foreign export was not bound?
Similarly, if the foreign import was labelled 'threadsafe', would it
work? It's not obvious to me. Some kind of semantics would be good.