Remove references to Haskell 98
[ghc-hetmet.git] / docs / users_guide / ffi-chap.xml
1 <?xml version="1.0" encoding="iso-8859-1"?>
2 <!-- FFI docs as a chapter -->
3
4 <chapter id="ffi">
5  <title>
6 Foreign function interface (FFI)
7  </title>
8
9   <para>GHC (mostly) conforms to the Haskell Foreign Function Interface,
10   whose definition is part of the Haskell Report on <ulink url="http://www.haskell.org/"><literal>http://www.haskell.org/</literal></ulink>.</para>
11
12   <para>FFI support is enabled by default, but can be enabled or disabled explicitly with the <option>-XForeignFunctionInterface</option><indexterm><primary><option>-XForeignFunctionInterface</option></primary>
13     </indexterm> flag.</para>
14
15   <para>GHC implements a number of GHC-specific extensions to the FFI
16     Addendum.  These extensions are described in <xref linkend="ffi-ghcexts" />, but please note that programs using
17     these features are not portable.  Hence, these features should be
18     avoided where possible.</para>
19
20   <para>The FFI libraries are documented in the accompanying library
21   documentation; see for example the
22     <ulink url="&libraryBaseLocation;/Control-Concurrent.html"><literal>Foreign</literal></ulink> module.</para>
23
24   <sect1 id="ffi-ghcexts">
25     <title>GHC extensions to the FFI Addendum</title>
26
27     <para>The FFI features that are described in this section are specific to
28     GHC.  Your code will not be portable to other compilers if you use them.</para>
29
30     <sect2>
31       <title>Unboxed types</title>
32
33       <para>The following unboxed types may be used as basic foreign types
34       (see FFI Addendum, Section 3.2): <literal>Int#</literal>,
35       <literal>Word#</literal>, <literal>Char#</literal>,
36       <literal>Float#</literal>, <literal>Double#</literal>,
37       <literal>Addr#</literal>, <literal>StablePtr# a</literal>,
38       <literal>MutableByteArray#</literal>, <literal>ForeignObj#</literal>,
39       and <literal>ByteArray#</literal>.</para>
40     </sect2>
41
42     <sect2 id="ffi-newtype-io">
43       <title>Newtype wrapping of the IO monad</title>
44       <para>The FFI spec requires the IO monad to appear in various  places,
45       but it can sometimes be convenient to wrap the IO monad in a
46       <literal>newtype</literal>, thus:
47 <programlisting>
48   newtype MyIO a = MIO (IO a)
49 </programlisting>
50      (A reason for doing so might be to prevent the programmer from
51         calling arbitrary IO procedures in some part of the program.)
52 </para>
53 <para>The Haskell FFI already specifies that arguments and results of
54 foreign imports and exports will be automatically unwrapped if they are 
55 newtypes (Section 3.2 of the FFI addendum).  GHC extends the FFI by automatically unwrapping any newtypes that
56 wrap the IO monad itself.
57 More precisely, wherever the FFI specification requires an IO type, GHC will
58 accept any newtype-wrapping of an IO type.  For example, these declarations are
59 OK:
60 <programlisting>
61    foreign import foo :: Int -> MyIO Int
62    foreign import "dynamic" baz :: (Int -> MyIO Int) -> CInt -> MyIO Int
63 </programlisting>
64 </para>
65       </sect2>
66
67       <sect2 id="ffi-prim">
68         <title>Primitive imports</title>
69         <para>
70           GHC extends the FFI with an additional calling convention
71           <literal>prim</literal>, e.g.:
72 <programlisting>
73    foreign import prim "foo" foo :: ByteArray# -> (# Int#, Int# #)
74 </programlisting>
75           This is used to import functions written in Cmm code that follow an
76           internal GHC calling convention. This feature is not intended for
77           use outside of the core libraries that come with GHC. For more
78           details see the GHC developer wiki.
79         </para>
80       </sect2>
81
82       <sect2 id="ffi-interruptible">
83         <title>Interruptible foreign calls</title>
84         <para>
85           This concerns the interaction of foreign calls
86           with <literal>Control.Concurrent.throwTo</literal>.
87           Normally when the target of a <literal>throwTo</literal> is
88           involved in a foreign call, the exception is not raised
89           until the call returns, and in the meantime the caller is
90           blocked.  This can result in unresponsiveness, which is
91           particularly undesirable in the case of user interrupt
92           (e.g. Control-C).  The default behaviour when a Control-C
93           signal is received (<literal>SIGINT</literal> on Unix) is to raise
94           the <literal>UserInterrupt</literal> exception in the main
95           thread; if the main thread is blocked in a foreign call at
96           the time, then the program will not respond to the user
97           interrupt.
98         </para>
99         
100         <para>
101           The problem is that it is not possible in general to
102           interrupt a foreign call safely.  However, GHC does provide
103           a way to interrupt blocking system calls which works for
104           most system calls on both Unix and Windows.  A foreign call
105           can be annotated with <literal>interruptible</literal> instead
106           of <literal>safe</literal> or <literal>unsafe</literal>:
107
108 <programlisting>
109 foreign import ccall interruptible 
110    "sleep" :: CUint -> IO CUint
111 </programlisting>
112
113           <literal>interruptble</literal> behaves exactly as
114           <literal>safe</literal>, except that when
115           a <literal>throwTo</literal> is directed at a thread in an
116           interruptible foreign call, an OS-specific mechanism will be
117           used to attempt to cause the foreign call to return:
118
119           <variablelist>
120             <varlistentry>
121               <term>Unix systems</term>
122               <listitem>
123                 <para>
124                   The thread making the foreign call is sent
125                   a <literal>SIGPIPE</literal> signal
126                   using <literal>pthread_kill()</literal>.  This is
127                   usually enough to cause a blocking system call to
128                   return with <literal>EINTR</literal> (GHC by default
129                   installs an empty signal handler
130                   for <literal>SIGPIPE</literal>, to override the
131                   default behaviour which is to terminate the process
132                   immediately).
133                 </para>
134               </listitem>
135             </varlistentry>
136             <varlistentry>
137               <term>Windows systems</term>
138               <listitem>
139                 <para>
140                   [Vista and later only] The RTS calls the Win32
141                   function <literal>CancelSynchronousIO</literal>,
142                   which will cause a blocking I/O operation to return
143                   with the
144                   error <literal>ERROR_OPERATION_ABORTED</literal>.
145                 </para>
146               </listitem>
147             </varlistentry>
148           </variablelist>
149
150           If the system call is successfully interrupted, it will
151           return to Haskell whereupon the exception can be raised.  Be
152           especially careful when
153           using <literal>interruptible</literal> that the caller of
154           the foreign function is prepared to deal with the
155           consequences of the call being interrupted; on Unix it is
156           good practice to check for <literal>EINTR</literal> always,
157           but on Windows it is not typically necessary to
158           handle <literal>ERROR_OPERATION_ABORTED</literal>.
159         </para>
160       </sect2>
161   </sect1>
162
163   <sect1 id="ffi-ghc">
164     <title>Using the FFI with GHC</title>
165
166     <para>The following sections also give some hints and tips on the
167     use of the foreign function interface in GHC.</para>
168
169     <sect2 id="foreign-export-ghc">
170       <title>Using <literal>foreign export</literal> and <literal>foreign import ccall "wrapper"</literal> with GHC</title>
171
172       <indexterm><primary><literal>foreign export
173       </literal></primary><secondary>with GHC</secondary>
174       </indexterm>
175
176       <para>When GHC compiles a module (say <filename>M.hs</filename>)
177       which uses <literal>foreign export</literal> or 
178       <literal>foreign import "wrapper"</literal>, it generates two
179       additional files, <filename>M_stub.c</filename> and
180       <filename>M_stub.h</filename>.  GHC will automatically compile
181       <filename>M_stub.c</filename> to generate
182       <filename>M_stub.o</filename> at the same time.</para>
183
184       <para>For a plain <literal>foreign export</literal>, the file
185       <filename>M_stub.h</filename> contains a C prototype for the
186       foreign exported function, and <filename>M_stub.c</filename>
187       contains its definition.  For example, if we compile the
188       following module:</para>
189
190 <programlisting>
191 module Foo where
192
193 foreign export ccall foo :: Int -> IO Int
194
195 foo :: Int -> IO Int
196 foo n = return (length (f n))
197
198 f :: Int -> [Int]
199 f 0 = []
200 f n = n:(f (n-1))</programlisting>
201
202       <para>Then <filename>Foo_stub.h</filename> will contain
203       something like this:</para>
204
205 <programlisting>
206 #include "HsFFI.h"
207 extern HsInt foo(HsInt a0);</programlisting>
208
209       <para>and <filename>Foo_stub.c</filename> contains the
210       compiler-generated definition of <literal>foo()</literal>.  To
211       invoke <literal>foo()</literal> from C, just <literal>#include
212       "Foo_stub.h"</literal> and call <literal>foo()</literal>.</para>
213
214       <para>The <filename>foo_stub.c</filename> and
215         <filename>foo_stub.h</filename> files can be redirected using the
216         <option>-stubdir</option> option; see <xref linkend="options-output"
217           />.</para>
218
219       <para>When linking the program, remember to include
220         <filename>M_stub.o</filename> in the final link command line, or
221         you'll get link errors for the missing function(s) (this isn't
222         necessary when building your program with <literal>ghc
223         &ndash;&ndash;make</literal>, as GHC will automatically link in the
224         correct bits).</para>
225
226       <sect3 id="using-own-main"> 
227         <title>Using your own <literal>main()</literal></title>
228
229         <para>Normally, GHC's runtime system provides a
230         <literal>main()</literal>, which arranges to invoke
231         <literal>Main.main</literal> in the Haskell program.  However,
232         you might want to link some Haskell code into a program which
233         has a main function written in another language, say C.  In
234         order to do this, you have to initialize the Haskell runtime
235         system explicitly.</para>
236
237         <para>Let's take the example from above, and invoke it from a
238         standalone C program.  Here's the C code:</para>
239
240 <programlisting>
241 #include &lt;stdio.h&gt;
242 #include "HsFFI.h"
243
244 #ifdef __GLASGOW_HASKELL__
245 #include "foo_stub.h"
246 #endif
247
248 #ifdef __GLASGOW_HASKELL__
249 extern void __stginit_Foo ( void );
250 #endif
251
252 int main(int argc, char *argv[])
253 {
254   int i;
255
256   hs_init(&amp;argc, &amp;argv);
257 #ifdef __GLASGOW_HASKELL__
258   hs_add_root(__stginit_Foo);
259 #endif
260
261   for (i = 0; i &lt; 5; i++) {
262     printf("%d\n", foo(2500));
263   }
264
265   hs_exit();
266   return 0;
267 }</programlisting>
268
269         <para>We've surrounded the GHC-specific bits with
270         <literal>#ifdef __GLASGOW_HASKELL__</literal>; the rest of the
271         code should be portable across Haskell implementations that
272         support the FFI standard.</para>
273
274         <para>The call to <literal>hs_init()</literal>
275         initializes GHC's runtime system.  Do NOT try to invoke any
276         Haskell functions before calling
277         <literal>hs_init()</literal>: bad things will
278         undoubtedly happen.</para>
279
280         <para>We pass references to <literal>argc</literal> and
281         <literal>argv</literal> to <literal>hs_init()</literal>
282         so that it can separate out any arguments for the RTS
283         (i.e. those arguments between
284         <literal>+RTS...-RTS</literal>).</para>
285
286         <para>Next, we call
287         <function>hs_add_root</function><indexterm><primary><function>hs_add_root</function></primary>
288         </indexterm>, a GHC-specific interface which is required to
289         initialise the Haskell modules in the program.  The argument
290         to <function>hs_add_root</function> should be the name of the
291         initialization function for the "root" module in your program
292         - in other words, the module which directly or indirectly
293         imports all the other Haskell modules in the program.  In a
294         standalone Haskell program the root module is normally
295         <literal>Main</literal>, but when you are using Haskell code
296         from a library it may not be.  If your program has multiple
297         root modules, then you can call
298         <function>hs_add_root</function> multiple times, one for each
299         root.  The name of the initialization function for module
300         <replaceable>M</replaceable> is
301         <literal>__stginit_<replaceable>M</replaceable></literal>, and
302         it may be declared as an external function symbol as in the
303         code above.  Note that the symbol name should be transformed
304         according to the Z-encoding:</para>
305
306       <informaltable>
307         <tgroup cols="2" align="left" colsep="1" rowsep="1">
308           <thead>
309             <row>
310               <entry>Character</entry>
311               <entry>Replacement</entry>
312             </row>
313           </thead>
314           <tbody>
315             <row>
316               <entry><literal>.</literal></entry>
317               <entry><literal>zd</literal></entry>
318             </row>
319             <row>
320               <entry><literal>_</literal></entry>
321               <entry><literal>zu</literal></entry>
322             </row>
323             <row>
324               <entry><literal>`</literal></entry>
325               <entry><literal>zq</literal></entry>
326             </row>
327             <row>
328               <entry><literal>Z</literal></entry>
329               <entry><literal>ZZ</literal></entry>
330             </row>
331             <row>
332               <entry><literal>z</literal></entry>
333               <entry><literal>zz</literal></entry>
334             </row>
335           </tbody>
336         </tgroup>
337       </informaltable>
338
339         <para>After we've finished invoking our Haskell functions, we
340         can call <literal>hs_exit()</literal>, which terminates the
341         RTS.</para>
342
343         <para>There can be multiple calls to
344         <literal>hs_init()</literal>, but each one should be matched
345         by one (and only one) call to
346         <literal>hs_exit()</literal><footnote><para>The outermost
347         <literal>hs_exit()</literal> will actually de-initialise the
348         system.  NOTE that currently GHC's runtime cannot reliably
349         re-initialise after this has happened,
350         see <xref linkend="ffi-divergence" />.</para>
351         </footnote>.</para>
352
353         <para>NOTE: when linking the final program, it is normally
354         easiest to do the link using GHC, although this isn't
355         essential.  If you do use GHC, then don't forget the flag
356         <option>-no-hs-main</option><indexterm><primary><option>-no-hs-main</option></primary>
357           </indexterm>, otherwise GHC will try to link
358         to the <literal>Main</literal> Haskell module.</para>
359       </sect3>
360
361       <sect3 id="ffi-library">
362         <title>Making a Haskell library that can be called from foreign
363           code</title>
364
365         <para>The scenario here is much like in <xref linkend="using-own-main"
366             />, except that the aim is not to link a complete program, but to
367           make a library from Haskell code that can be deployed in the same
368           way that you would deploy a library of C code.</para>
369
370         <para>The main requirement here is that the runtime needs to be
371           initialized before any Haskell code can be called, so your library
372           should provide initialisation and deinitialisation entry points,
373           implemented in C or C++.  For example:</para>
374
375 <programlisting>
376  HsBool mylib_init(void){
377    int argc = ...
378    char *argv[] = ...
379
380    // Initialize Haskell runtime
381    hs_init(&amp;argc, &amp;argv);
382
383    // Tell Haskell about all root modules
384    hs_add_root(__stginit_Foo);
385
386    // do any other initialization here and
387    // return false if there was a problem
388    return HS_BOOL_TRUE;
389  }
390
391  void mylib_end(void){
392    hs_exit();
393  }
394 </programlisting>
395
396         <para>The initialisation routine, <literal>mylib_init</literal>, calls
397           <literal>hs_init()</literal> and <literal>hs_add_root()</literal> as
398           normal to initialise the Haskell runtime, and the corresponding
399           deinitialisation function <literal>mylib_end()</literal> calls
400           <literal>hs_exit()</literal> to shut down the runtime.</para>
401       </sect3>
402
403     </sect2>
404     
405     <sect2 id="glasgow-foreign-headers">
406       <title>Using header files</title>
407
408       <indexterm><primary>C calls, function headers</primary></indexterm>
409
410       <para>C functions are normally declared using prototypes in a C
411         header file.  Earlier versions of GHC (6.8.3 and
412         earlier) <literal>&num;include</literal>d the header file in
413         the C source file generated from the Haskell code, and the C
414         compiler could therefore check that the C function being
415         called via the FFI was being called at the right type.</para>
416
417       <para>GHC no longer includes external header files when
418         compiling via C, so this checking is not performed.  The
419         change was made for compatibility with the native code backend
420         (<literal>-fasm</literal>) and to comply strictly with the FFI
421         specification, which requires that FFI calls are not subject
422         to macro expansion and other CPP conversions that may be
423         applied when using C header files.  This approach also
424         simplifies the inlining of foreign calls across module and
425         package boundaries: there's no need for the header file to be
426         available when compiling an inlined version of a foreign call,
427         so the compiler is free to inline foreign calls in any
428         context.</para>
429         
430       <para>The <literal>-&num;include</literal> option is now
431         deprecated, and the <literal>include-files</literal> field
432         in a Cabal package specification is ignored.</para>
433
434     </sect2>
435
436     <sect2>
437       <title>Memory Allocation</title>
438
439       <para>The FFI libraries provide several ways to allocate memory
440       for use with the FFI, and it isn't always clear which way is the
441       best.  This decision may be affected by how efficient a
442       particular kind of allocation is on a given compiler/platform,
443       so this section aims to shed some light on how the different
444       kinds of allocation perform with GHC.</para>
445
446       <variablelist>
447         <varlistentry>
448           <term><literal>alloca</literal> and friends</term>
449           <listitem>
450             <para>Useful for short-term allocation when the allocation
451             is intended to scope over a given <literal>IO</literal>
452             computation.  This kind of allocation is commonly used
453             when marshalling data to and from FFI functions.</para>
454
455             <para>In GHC, <literal>alloca</literal> is implemented
456             using <literal>MutableByteArray#</literal>, so allocation
457             and deallocation are fast: much faster than C's
458             <literal>malloc/free</literal>, but not quite as fast as
459             stack allocation in C.  Use <literal>alloca</literal>
460             whenever you can.</para>
461           </listitem>
462         </varlistentry>
463
464         <varlistentry>
465           <term><literal>mallocForeignPtr</literal></term>
466           <listitem>
467             <para>Useful for longer-term allocation which requires
468             garbage collection.  If you intend to store the pointer to
469             the memory in a foreign data structure, then
470             <literal>mallocForeignPtr</literal> is
471             <emphasis>not</emphasis> a good choice, however.</para>
472
473             <para>In GHC, <literal>mallocForeignPtr</literal> is also
474             implemented using <literal>MutableByteArray#</literal>.
475             Although the memory is pointed to by a
476             <literal>ForeignPtr</literal>, there are no actual
477             finalizers involved (unless you add one with
478             <literal>addForeignPtrFinalizer</literal>), and the
479             deallocation is done using GC, so
480             <literal>mallocForeignPtr</literal> is normally very
481             cheap.</para>
482           </listitem>
483         </varlistentry>
484
485         <varlistentry>
486           <term><literal>malloc/free</literal></term>
487           <listitem>
488             <para>If all else fails, then you need to resort to
489             <literal>Foreign.malloc</literal> and
490             <literal>Foreign.free</literal>.  These are just wrappers
491             around the C functions of the same name, and their
492             efficiency will depend ultimately on the implementations
493             of these functions in your platform's C library.  We
494             usually find <literal>malloc</literal> and
495             <literal>free</literal> to be significantly slower than
496             the other forms of allocation above.</para>
497           </listitem>
498         </varlistentry>
499
500         <varlistentry>
501           <term><literal>Foreign.Marshal.Pool</literal></term>
502           <listitem>
503             <para>Pools are currently implemented using
504             <literal>malloc/free</literal>, so while they might be a
505             more convenient way to structure your memory allocation
506             than using one of the other forms of allocation, they
507             won't be any more efficient.  We do plan to provide an
508             improved-performance implementation of Pools in the
509             future, however.</para>
510           </listitem>
511         </varlistentry>
512       </variablelist>
513     </sect2>
514     
515     <sect2 id="ffi-threads">
516       <title>Multi-threading and the FFI</title>
517       
518       <para>In order to use the FFI in a multi-threaded setting, you must
519         use the <option>-threaded</option> option
520         (see <xref linkend="options-linker" />).</para>
521         
522       <sect3>
523         <title>Foreign imports and multi-threading</title>
524         
525         <para>When you call a <literal>foreign import</literal>ed
526           function that is annotated as <literal>safe</literal> (the
527           default), and the program was linked
528           using <option>-threaded</option>, then the call will run
529           concurrently with other running Haskell threads.  If the
530           program was linked without <option>-threaded</option>,
531           then the other Haskell threads will be blocked until the
532           call returns.</para>
533         
534         <para>This means that if you need to make a foreign call to
535           a function that takes a long time or blocks indefinitely,
536           then you should mark it <literal>safe</literal> and
537           use <option>-threaded</option>.  Some library functions
538           make such calls internally; their documentation should
539           indicate when this is the case.</para>
540
541         <para>If you are making foreign calls from multiple Haskell
542           threads and using <option>-threaded</option>, make sure that
543           the foreign code you are calling is thread-safe.  In
544           particularly, some GUI libraries are not thread-safe and
545           require that the caller only invokes GUI methods from a
546           single thread.  If this is the case, you may need to
547           restrict your GUI operations to a single Haskell thread,
548           and possibly also use a bound thread (see
549           <xref linkend="haskell-threads-and-os-threads" />).</para>
550
551         <para>Note that foreign calls made by different Haskell
552           threads may execute in <emphasis>parallel</emphasis>, even
553           when the <literal>+RTS -N</literal> flag is not being used
554           (<xref linkend="parallel-options" />).  The <literal>+RTS
555           -N</literal> flag controls parallel execution of Haskell
556           threads, but there may be an arbitrary number of foreign
557           calls in progress at any one time, regardless of
558           the <literal>+RTS -N</literal> value.</para>
559
560         <para>If a call is annotated as <literal>interruptible</literal>
561           and the program was multithreaded, the call may be
562           interrupted in the event that the Haskell thread receives an
563           exception.  The mechanism by which the interrupt occurs
564           is platform dependent, but is intended to cause blocking
565           system calls to return immediately with an interrupted error
566           code.  The underlying operating system thread is not to be
567           destroyed.  See <xref linkend="ffi-interruptible"/> for more details.</para>
568       </sect3>
569
570       <sect3 id="haskell-threads-and-os-threads">
571         <title>The relationship between Haskell threads and OS
572           threads</title>
573         
574         <para>Normally there is no fixed relationship between Haskell
575           threads and OS threads.  This means that when you make a
576           foreign call, that call may take place in an unspecified OS
577           thread.  Furthermore, there is no guarantee that multiple
578           calls made by one Haskell thread will be made by the same OS
579           thread.</para>
580
581         <para>This usually isn't a problem, and it allows the GHC
582           runtime system to make efficient use of OS thread resources.
583           However, there are cases where it is useful to have more
584           control over which OS thread is used, for example when
585           calling foreign code that makes use of thread-local state.
586           For cases like this, we provide <emphasis>bound
587           threads</emphasis>, which are Haskell threads tied to a
588           particular OS thread.  For information on bound threads, see
589           the documentation
590           for the <ulink url="&libraryBaseLocation;/Control-Concurrent.html"><literal>Control.Concurrent</literal></ulink>
591           module.</para>
592       </sect3>
593       
594       <sect3>
595         <title>Foreign exports and multi-threading</title>
596         
597         <para>When the program is linked
598           with <option>-threaded</option>, then you may
599           invoke <literal>foreign export</literal>ed functions from
600           multiple OS threads concurrently.  The runtime system must
601           be initialised as usual by
602           calling <literal>hs_init()</literal>
603           and <literal>hs_add_root</literal>, and these calls must
604           complete before invoking any <literal>foreign
605           export</literal>ed functions.</para>
606       </sect3>
607
608       <sect3 id="hs-exit">
609         <title>On the use of <literal>hs_exit()</literal></title>
610
611         <para><literal>hs_exit()</literal> normally causes the termination of
612           any running Haskell threads in the system, and when
613           <literal>hs_exit()</literal> returns, there will be no more Haskell
614           threads running.  The runtime will then shut down the system in an
615           orderly way, generating profiling
616           output and statistics if necessary, and freeing all the memory it
617           owns.</para>
618
619         <para>It isn't always possible to terminate a Haskell thread forcibly:
620           for example, the thread might be currently executing a foreign call,
621           and we have no way to force the foreign call to complete.  What's
622           more, the runtime must
623           assume that in the worst case the Haskell code and runtime are about
624           to be removed from memory (e.g. if this is a <link linkend="win32-dlls">Windows DLL</link>,
625           <literal>hs_exit()</literal> is normally called before unloading the
626           DLL).  So <literal>hs_exit()</literal> <emphasis>must</emphasis> wait
627           until all outstanding foreign calls return before it can return
628           itself.</para>
629
630         <para>The upshot of this is that if you have Haskell threads that are
631           blocked in foreign calls, then <literal>hs_exit()</literal> may hang
632           (or possibly busy-wait) until the calls return.  Therefore it's a
633           good idea to make sure you don't have any such threads in the system
634           when calling <literal>hs_exit()</literal>.  This includes any threads
635           doing I/O, because I/O may (or may not, depending on the
636           type of I/O and the platform) be implemented using blocking foreign
637           calls.</para>
638
639         <para>The GHC runtime treats program exit as a special case, to avoid
640           the need to wait for blocked threads when a standalone
641           executable exits.  Since the program and all its threads are about to
642           terminate at the same time that the code is removed from memory, it
643           isn't necessary to ensure that the threads have exited first.
644           (Unofficially, if you want to use this fast and loose version of
645           <literal>hs_exit()</literal>, then call
646           <literal>shutdownHaskellAndExit()</literal> instead).</para> 
647       </sect3>
648     </sect2>
649         
650   </sect1>
651 </chapter>
652
653 <!-- Emacs stuff:
654      ;;; Local Variables: ***
655      ;;; sgml-parent-document: ("users_guide.xml" "book" "chapter") ***
656      ;;; End: ***
657  -->