RE: Native Threads in the RTS

2002-11-28 Thread Simon Peyton-Jones
Improving.  Want to put it in CVS?  Simon M can suggest where.

Simon


| Definitions
| ~~~
| A native thread (aka OS thread) is a thread as defined by the
operating
| system.
| 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
completes.  

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
thread(s).

| 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
| executing.
| *) The native thread is executing foreign code.
| *) The native thread and all Haskell threads associated with it are
| blocked.

You don't say (but you do mean)

A bound Haskell thread can be executed only by its associated
native thread

You don't say (and I'm not sure if you mean)

If a bound native thread blocks, all of its associated Haskell 
threads are blocked too

If a bound Haskell thread blocks, its associate native thread
and all its 
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
inviolate.

| 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
implementation
| 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 
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.


___
Glasgow-haskell-users mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users



RE: Native Threads in the RTS

2002-11-28 Thread Simon Marlow
Simon P.J. writes:
 Can you give an example of when a native thread is associated 
 with more than one Haskell thread?

I gave an example in my previous message.  It's when a bound Haskell
thread makes a foreign call which re-enters Haskell via a bound foreign
export.

 You don't say (and I'm not sure if you mean)
 
   If a bound native thread blocks, all of its associated Haskell 
   threads are blocked too

When Bagpuss goes to sleep, all his friends go to sleep too. :-)
(apologies to those who never watched British childrens TV in the 70s).

Actually, for any given native thread, only one of its bound Haskell
threads can be runnable, the others must all be blocked waiting on the
result of a foreign call.  So you don't really have to worry about a
native thread being bound to multiple Haskell threads; this would be
quite tricky to implement in the scheduler anyway.

Cheers,
Simon
___
Glasgow-haskell-users mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users



Re: Native Threads in the RTS

2002-11-28 Thread Wolfgang Thaller
I'll write up a new version of the proposal tomorrow. For now, here are 
some answers and [at the end] a question about the current FFI 
specification.

Simon Peyton Jones wrote:
You don't say (but you do mean)

	A bound Haskell thread can be executed only by its associated
native thread


No I don't mean that, and I've been avoiding to say that. This is how 
the GHC implementation will probably handle it, but I do not want to 
put that in the general specification. Alastair remarked a while ago:

For that matter, I'd like it to be possible to implement this spec in
Hugs.  Hugs is internally single-threaded but this spec is concerned
with what happens when Haskell calls out to C and we could arrange to
switch into the appropriate native thread when Hugs calls out to C.


After all, the only thing which needs to be guaranteed is that foreign 
functions called by the Haskell thread are executed in the associated 
native thread.

You don't say (and I'm not sure if you mean)

	If a bound native thread blocks, all of its associated Haskell
	threads are blocked too

	If a bound Haskell thread blocks, its associate native thread
and all its
	associated Haskell threads also block.


Does this sound clearer:

*) Exactly one Haskell thread associated with the native thread is 
executing. All other associated Haskell threads are blocked. No foreign 
code is being executed by the native thread.
*) The native thread is executing foreign code. No Haskell code is 
executing in any of the associated Haskell threads.
*) The native thread and all Haskell threads associated with it are 
blocked.

| 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
inviolate.

[...]
Again, it must not be a bound native thread.

Good point, I had overlooked that.


If a bound Haskell thread
	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.


Good question. I reread Section 3.3 of the FFI document (RC7), and now 
I think I cannot clarify my specification in this respect without first 
asking others to clarify the current specs - can someone explain the 
distinction between unsafe, safe and threadsafe in the current FFI to 
me? I think I know what it does in GHC, but what's the general 
definition? I've read the description in the FFI document, but it's not 
clear to me. Is there any reason why safe is the default and not 
threadsafe? After all, safe is less safe (it might cause the whole 
program to block). To me, safe seems to be an odd middle ground 
between speed and safety. What is safe guaranteed/allowed to do? Is 
it _guaranteed_ to block other Haskell threads under certain 
conditions? Or is that only an artifact of current implementations? Why 
are implementations allowed to _silently_ fall back to safe when 
threadsafe is not supported? Isn't that dangerous?

If I'm not mistaken, threadsafe calls from bound Haskell threads 
would have exactly the same overhead as safe calls. Should we make 
sure that safe calls somehow block other threads? If so, why?


Thats all for now

Wolfgang

___
Glasgow-haskell-users mailing list
[EMAIL PROTECTED]
http://www.haskell.org/mailman/listinfo/glasgow-haskell-users