[Haskell-cafe] Re: Threads with high CPU usage
duncan.coutts at worc.ox.ac.uk
Mon Dec 22 06:42:22 EST 2008
On Mon, 2008-12-22 at 10:30 +0000, Malcolm Wallace wrote:
> For those who are puzzled, Don is suggesting that
> foreign import ccall unsafe "foo" :: Bar -> Baz
> should simply be changed to
> foreign import ccall safe "foo" :: Bar -> Baz
> And in case anyone is wondering whether fiddling with safety
> declarations is entirely wise:
> "unsafe" means I, the programmer, guarantee that this foreign function
> cannot call back into Haskell land, so you, the compiler, are free to
> do unsafe things when implementing the call.
> "safe" means I the programmer, cannot guarantee that this foreign
> function is safe, so please, you the compiler, make sure that you add
> extra checks and assurances to ensure that the call really will be
> safe in case it calls back into Haskell land.
> The terminology seems counter-intuitive, but in other other words, a
> "safe" call is slower but more flexible, an "unsafe" call is fast and
> dangerous. Therefore it is always OK to convert an "unsafe" declaration
> into a "safe" one, but never OK to convert from "safe" to "unsafe"
> without looking at what the foreign side actually does.
And in general we would not even bother with considering using "unsafe"
for calls that are already expensive. It's only worth considering when
the length of the call is always very short.
For example in a database library it might make sense to use 'unsafe' on
the data-access functions that extract data from a local query result
but we should always use 'safe' on any DB function that might want to
talk to the network (eg to get more query results).
More information about the Haskell-Cafe