Changes between Version 2 and Version 3 of Concurrency/DraftReportText


Ignore:
Timestamp:
Jan 16, 2007 12:59:52 PM (8 years ago)
Author:
simonmar@…
Comment:

add bound threads text

Legend:

Unmodified
Added
Removed
Modified
  • Concurrency/DraftReportText

    v2 v3  
    22= Draft Chapter: Concurrency = 
    33 
    4 Haskell supports ''concurrency'', in the sense that a program can contain multiple threads of execution interacting non-deterministically.  Precisely what we mean by this, and what guarantees the programmer is given, are described in the following chapter.  The material here is to be read concurrently with the description of the `Control.Concurrent` library, in Chapter ??. 
     4Haskell supports ''concurrency'', in the sense that a program can contain multiple threads of execution interacting non-deterministically.  Precisely what we mean by this, and what guarantees the programmer is given, are described in this chapter.  The material here is to be read concurrently with the description of the `Control.Concurrent` library, in Chapter ??. 
    55 
    66== Threads == 
     
    1616Operations performed by multiple threads may be interleaved arbitrarily by the implementation, with the following restriction: 
    1717 
    18  * A thread that is not blocked on a resource will not be indefinitely delayed by the implementation. 
     18 * A thread that is not ''blocked'' will not be indefinitely delayed by the implementation. 
     19 
     20Where a thread is ''blocked'' if 
     21 
     22 * the thread is waiting for an external resource that is currently not available (for example input 
     23   from the console), or it is performing an operation that involves a shared resource and another thread 
     24   currently has ownership of the resource (for example, output to a `Handle`). 
    1925 
    2026This is called the ''fairness guarantee''.  An implementation technique that is often used to provide this fairness guarnatee is ''pre-emption'': running threads are periodically pre-empted in software in order to let other threads proceed.  There are other implementation techniques that also work; for example, hardware concurrency.  ('''ToDo''': move this text to a footnote or separate section?) 
    2127 
    22 '''ToDo''': explain "blocking". 
    2328 
    2429== The `ThreadId` type == 
     
    6671'''ToDo'''. 
    6772 
    68 Bound threads allow a Haskell program more control over the mapping between Haskell threads and OS threads.  Such control is required for interacting with foreign code that is either single-threaded or makes use of thread-local state in the native OS threading model. 
     73Bound threads allow a Haskell program more control over the mapping between Haskell threads and OS threads.  Such control is required for interacting with foreign code that makes use of thread-local state in the native OS threading model.  When foreign code uses OS thread-local state, the Haskell programmer needs to restrict calls to that foreign code to happen in a particular OS thread, which is what bound threads provide. 
     74 
     75Bound threads are an optional feature in Haskell'; the documentation for an implementation should state whether bound threads are supported or not. 
     76 
     77A Haskell thread can be either ''bound'' or ''unbound'', depending on how it was created: 
     78 
     79 * Calls from foreign code to Haskell functions exposed by `foreign export` or `foreign import "wrapper"` create a 
     80   ''bound'' thread in which to run the computation. 
     81 
     82 * `forkIO` creates ''unbound'' threads 
     83 
     84Whether a thread is bound or unbound affects how foreign calls are made: 
     85 
     86  * Foreign calls made by an unbound thread happen in an arbitrary OS thread. 
     87 
     88  * Foreign calls made by a bound thread are always made by the OS thread that made the original call-in. 
     89 
     90Note that a valid implementation of bound threads is one in which every Haskell thread is associated with a distinct OS thread, where in effect every thread is bound.  However, the `isCurrentThreadBound` operation (see below) should always return `False` for threads created by `forkIO`, regardless of the implementation, to aid portabilty. 
     91 
     92The following operations exported by `Control.Concurrent` are related to bound threads: 
     93 
     94{{{ 
     95  supportsBoundThreads :: Bool 
     96}}} 
     97 
     98This value is `True` if the implementation supports bound threads ('''NB.''' this is called `rtsSupportsBoundThreads` currently). 
     99 
     100{{{ 
     101  isCurrentThreadBound :: IO Bool 
     102}}} 
     103 
     104This operation returns `False` if the current thread was created by `forkIO`, and `True` otherwise. 
     105 
     106{{{ 
     107  forkOS :: IO () -> IO ThreadId 
     108}}} 
     109 
     110Like `forkIO`, `forkOS` creates a new Haskell thread to run the supplied `IO` action.  However, in the case of `forkOS`, the new Haskell thread is bound to a freshly created OS thread.  This is achieved by making a foreign call to create the new OS thread, and having the new OS thread invoke the `IO` action, thus creating a bound thread.