Unbound threads and FFI

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Unbound threads and FFI

Joel Reymont
How do unbound threads play with FFI? According to Simon PJ, each  
foreign call will get its own OS thread if its blocked.

How does GHC determine if the call is blocked? Does each call get its  
own OS thread from the start? Sometime later? Does this depend on the  
safe/unsafe specs of the foreign call?

Does the above change if a threaded/non-threaded runtime is in use?

        Thanks, Joel

--
http://wagerlabs.com/





_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

RE: Unbound threads and FFI

Simon Marlow
On 14 December 2005 14:37, Joel Reymont wrote:

> How do unbound threads play with FFI? According to Simon PJ, each
> foreign call will get its own OS thread if its blocked.
>
> How does GHC determine if the call is blocked? Does each call get its
> own OS thread from the start? Sometime later?

When a thread exits Haskell to make an FFI call, another OS thread takes
over running the other Haskell threads.  If we don't have another OS
thread available, we start one.
 
> Does this depend on the safe/unsafe specs of the foreign call?

this only happens for safe calls, not unsafe.

> Does the above change if a threaded/non-threaded runtime is in use?

this only happens in the -threaded RTS, in the non-threaded RTS a FFI
call will block all the other Haskell threads.

Cheers,
        Simon
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Unbound threads and FFI

Joel Reymont

On Dec 14, 2005, at 3:21 PM, Simon Marlow wrote:

> this only happens in the -threaded RTS, in the non-threaded RTS a FFI
> call will block all the other Haskell threads.

Just to be absolutely sure... Does the above apply to safe _and_  
unsafe FFI?

I have been using the non-threaded runtime with unsafe FFI calls  
(OpenSSL, Zlib) and I do not believe my runtime was being blocked.  
Maybe I just didn't notice it, though.

--
http://wagerlabs.com/





_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

RE: Unbound threads and FFI

Simon Marlow
In reply to this post by Joel Reymont
On 14 December 2005 15:34, Joel Reymont wrote:

> On Dec 14, 2005, at 3:21 PM, Simon Marlow wrote:
>
>> this only happens in the -threaded RTS, in the non-threaded RTS a FFI
>> call will block all the other Haskell threads.
>
> Just to be absolutely sure... Does the above apply to safe _and_
> unsafe FFI?
>
> I have been using the non-threaded runtime with unsafe FFI calls
> (OpenSSL, Zlib) and I do not believe my runtime was being blocked.
> Maybe I just didn't notice it, though.

Maybe this little table will help:

Do other Haskell threads get blocked during an FFI call?

              | safe    unsafe
--------------+----------------
 -threaded    | NO      YES
 no -threaded | YES     YES

Cheers,
        Simon
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re[2]: Unbound threads and FFI

Bulat Ziganshin
Hello Simon,

Wednesday, December 14, 2005, 7:39:43 PM, you wrote:

SM> Do other Haskell threads get blocked during an FFI call?

SM>               | safe    unsafe
SM> --------------+----------------
SM>  -threaded    | NO      YES
SM>  no -threaded | YES     YES

there is also `threadsafe` specifier. i don't understand from the docs
what it exactly means?

ghc commentary says:

To signal that you want an external call to be serviced by a separate
OS thread, you have to add the attribute threadsafe to a foreign
import declaration, i.e.,

foreign import "bigComp" threadsafe largeComputation :: Int -> IO ()

The distinction between 'safe' and thread-safe C calls is made so that
we may call external functions that aren't re-entrant but may cause a
GC to occur.  

The threadsafe attribute subsumes safe.


--
Best regards,
 Bulat                            mailto:[hidden email]



_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

RE: Unbound threads and FFI

Simon Peyton Jones
In reply to this post by Joel Reymont
Did you read the paper Extending the Haskell Foreign Function Interface
with Concurrency, described in the GHC Wiki page about concurrency?
It'd be a good start
http://hackage.haskell.org/trac/ghc/wiki/GhcConcurrency

Please do improve it.

Simon

| -----Original Message-----
| From: [hidden email]
[mailto:[hidden email]] On Behalf Of Joel
| Reymont
| Sent: 14 December 2005 14:37
| To: Haskell-Cafe Cafe
| Subject: [Haskell-cafe] Unbound threads and FFI
|
| How do unbound threads play with FFI? According to Simon PJ, each
| foreign call will get its own OS thread if its blocked.
|
| How does GHC determine if the call is blocked? Does each call get its
| own OS thread from the start? Sometime later? Does this depend on the
| safe/unsafe specs of the foreign call?
|
| Does the above change if a threaded/non-threaded runtime is in use?
|
| Thanks, Joel
|
| --
| http://wagerlabs.com/
|
|
|
|
|
| _______________________________________________
| Haskell-Cafe mailing list
| [hidden email]
| http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

RE: Re[2]: Unbound threads and FFI

Simon Marlow
In reply to this post by Joel Reymont
On 14 December 2005 18:04, Bulat Ziganshin wrote:

> Hello Simon,
>
> Wednesday, December 14, 2005, 7:39:43 PM, you wrote:
>
>> Do other Haskell threads get blocked during an FFI call?
>
>>               | safe    unsafe
>> --------------+----------------
>>  -threaded    | NO      YES
>>  no -threaded | YES     YES
>
> there is also `threadsafe` specifier. i don't understand from the docs
> what it exactly means?
>
> ghc commentary says:
>
> To signal that you want an external call to be serviced by a separate
> OS thread, you have to add the attribute threadsafe to a foreign
> import declaration, i.e.,
>
> foreign import "bigComp" threadsafe largeComputation :: Int -> IO ()
>
> The distinction between 'safe' and thread-safe C calls is made so that
> we may call external functions that aren't re-entrant but may cause a
> GC to occur.
>
> The threadsafe attribute subsumes safe.

threadsafe is deprecated; it is currently exactly the same as safe.  We
thought at one time that it would be useful to have the distinction, but
it turned out to be impractical to implement safe that wasn't also
threadsafe.

Cheers,
        Simon
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Unbound threads and FFI

Joel Reymont
In reply to this post by Simon Peyton Jones
Simon,

I could not find the paper at the link below but it can be found here:

http://www.haskell.org/~simonmar/papers/conc-ffi.pdf

The paper does not explain the differences I'm seeing between runs of  
my program linked with the threaded and non-threaded runtimes. I get  
crashes with the threaded runtime (sent to both you and Simon) and  
things run slowly. With the non-threaded runtime everything flies.

All my FFI calls are marked unsafe and thus should be blocking in  
both runtimes so I cannot explain the differences.

        Thanks, Joel

On Dec 15, 2005, at 9:07 AM, Simon Peyton-Jones wrote:

> Did you read the paper Extending the Haskell Foreign Function  
> Interface
> with Concurrency, described in the GHC Wiki page about concurrency?
> It'd be a good start
> http://hackage.haskell.org/trac/ghc/wiki/GhcConcurrency

--
http://wagerlabs.com/





_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

Re: Unbound threads and FFI

John Meacham
In reply to this post by Simon Marlow
On Wed, Dec 14, 2005 at 04:39:43PM -0000, Simon Marlow wrote:
> Do other Haskell threads get blocked during an FFI call?
>
>               | safe    unsafe
> --------------+----------------
>  -threaded    | NO      YES
>  no -threaded | YES     YES

this is part of the reason I'd like to see a separate 'blockable'
specifier allowed on foreign imports. so that one can write portable
thread-safe libraries. right now, if one want's to call something like
gethostbyname(3), they must call it with 'safe' so every implementation
has to pay the price of a safe call even though it is only there so ghc
won't block. (or #ifdefs, which are anoying) if blockable were separate
then it can be declared 'blockable unsafe' and ghc will interpret that
as 'safe' while non-threaded implementations will interpret it as
'unsafe' and everyone is happy.

        John

--
John Meacham - ⑆repetae.net⑆john⑈
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe
Reply | Threaded
Open this post in threaded view
|

RE: Re[2]: Unbound threads and FFI

Simon Peyton Jones
In reply to this post by Joel Reymont
I've added a FAQ.

Simon

| -----Original Message-----
| From: [hidden email]
[mailto:[hidden email]] On Behalf Of
| Simon Marlow
| Sent: 15 December 2005 09:26
| To: Bulat Ziganshin
| Cc: Haskell-Cafe Cafe
| Subject: RE: Re[2]: [Haskell-cafe] Unbound threads and FFI
|
| On 14 December 2005 18:04, Bulat Ziganshin wrote:
|
| > Hello Simon,
| >
| > Wednesday, December 14, 2005, 7:39:43 PM, you wrote:
| >
| >> Do other Haskell threads get blocked during an FFI call?
| >
| >>               | safe    unsafe
| >> --------------+----------------
| >>  -threaded    | NO      YES
| >>  no -threaded | YES     YES
| >
| > there is also `threadsafe` specifier. i don't understand from the
docs
| > what it exactly means?
| >
| > ghc commentary says:
| >
| > To signal that you want an external call to be serviced by a
separate
| > OS thread, you have to add the attribute threadsafe to a foreign
| > import declaration, i.e.,
| >
| > foreign import "bigComp" threadsafe largeComputation :: Int -> IO ()
| >
| > The distinction between 'safe' and thread-safe C calls is made so
that
| > we may call external functions that aren't re-entrant but may cause
a
| > GC to occur.
| >
| > The threadsafe attribute subsumes safe.
|
| threadsafe is deprecated; it is currently exactly the same as safe.
We
| thought at one time that it would be useful to have the distinction,
but
| it turned out to be impractical to implement safe that wasn't also
| threadsafe.
|
| Cheers,
| Simon
| _______________________________________________
| Haskell-Cafe mailing list
| [hidden email]
| http://www.haskell.org/mailman/listinfo/haskell-cafe
_______________________________________________
Haskell-Cafe mailing list
[hidden email]
http://www.haskell.org/mailman/listinfo/haskell-cafe