Changes between Version 17 and Version 18 of Concurrency

Mar 31, 2006 8:03:16 AM (9 years ago)



  • Concurrency

    v17 v18  
    11= Concurrency = 
    2 [[PageOutline]] 
    33== References == 
    1616 * [ Extending the Haskell FFI with Concurrency] (a specification of the interaction between concurrency and the FFI, with a semantics) 
    1717 * [ A Draft report addendum] (a shorter version of the above paper). 
    18  * [ A Poor Man's Concurrency Monad] (Hugs' implementation of Concurrency)  
    1918 * [ Software Transactional Memory] 
    2827 * Things like the 'poor man's concurrency monad' can achieve some of the benefits 
     29== Proposal ==           
    31 ---------------------- 
    32 = Proposals =           
     31 * Standardise on Concurrent Haskell without STM.  It is our view that even in the presence of STM, {{{MVar}}}s offer 
     32   functionality that is distinct from STM and separately useful, so this leaves room for growth. 
     34 * Use the semantics from [ Extending the Haskell FFI with Concurrency]  
    34 == Proposal 1 == 
    36  * The Haskell' standard does not include concurrency, but includes enough to allow portable thread-safe 
    37    libraries to be written.  (see "thread safe", below). 
     38 * Decide how much pre-emption is acceptable, and figure out how to specify this. 
    39  * A separate addendum specifies concurrency according to the ''fairness'' requirements (see below). 
    41 == Proposal 2 == 
    43  * The Haskell' standard includes concurrency with the ''fairness'' requirements. 
    45  * A separate addendum specifies how implementations that do not provide concurrency behave: at the least, 
    46    it should include enough to allow portable thread-safe libraries. 
    48 == Proposal 3 == 
    50  * The Haskell' standard includes concurrency with a weaker version of ''fairness'', that is enough 
    51    to admit implementations with ''cooperative scheduling''. 
    53  * A separate addendum specifies concurrency with the full ''fairness'' requirements. 
    55 == Open questions == 
     40 * Should we specify what an implementation that doesn't provide concurrency should do? (e.g. provide an implementation 
     41   of MVars in terms of IORefs, so that concurrency-safe libraries can be written portably). 
    5743 * Require bound thread support, or make it optional?  (YHC has concurrency with non-blocking foreign calls, but doesn't 
    5844   have bound threads as yet.) 
    60  * Do we require ForeignBlocking? 
    62 == Thread-safety == 
    64 In order to write library code that is thread-safe when run in a multi-threaded environment, two things are needed: 
    66  1. a way to protect mutable state against race conditions 
    67  2. a way to declare that foreign calls are blocking 
    69 For (1), we have two choices: 
    71  * Provide {{{MVar}}}s.  A non-concurrent implementation might implement them in terms of {{{IORef}}}, 
    72    for example. 
    74  * Provide STM.  Not entirely trivial to implement, even in a single-threaded implementation, because 
    75    exceptions have to abort a transaction.  Ross Paterson provided an [ implementation]. 
    77 For (2), one option is ForeignBlocking. 
    79 ---------- 
    80 = Terminology = 
    82 '''Pre-emption''' means that (1) threads have priority levels, and (2) that a 
    83 higher priority thread can steal the processor from a currently-running 
    84 lower priority thread, and (3) it can do so "immediately" it needs to, 
    85 without waiting for some "safe" synchronisation point. 
    86 By these criteria, none of the current Haskell implementations are 
    87 pre-emptive, because no API assigns priorities to threads.  So let's try 
    88 to avoid using the term. 
    90 '''Fairness''' can be defined by two main criteria: 
    91  * No runnable process will be indefinitely delayed. 
    92  * No thread can be blocked indefinitely on an MVar unless another thread holds the MVar indefinitely. 
    94 '''Cooperative scheduling''' describes an implementation in which it is the programmer's responsibility to insert context switch points in the code.  An implementation that only provides cooperative scheduling cannot satisfy the fairness properties given above.  A programmer who has access to all the code ''may'' be able to insert enough context switch points to satisfy fairness, but this isn't always possible. 
    96 '''Concurrent foreign call''' means a foreign call that should run concurrently with other Haskell threads.  It is a requirement of "fairness" (above) that a foreign call should not prevent other threads from making progress indefinitely.  Note that we used to use the term '''non-blocking foreign call''' here, but that lead to confusion with "foreign calls that block", which are foreign calls that may wait indefinitely for some resource, such as reading data from a socket.  "foreign calls that block" are the main reason for wanting support for concurrent foreign calls. 
    99 '''Scheduling.''' 
    100 Most current implementations of 
    101 concurrency in Haskell use non-preemptive scheduling.  That is, there are 
    102 explicit time points at which any one thread can ''yield'', allowing other threads to run. 
    103 The only differences between implementations are in the granularity and positioning of the yield. 
    105   * For Hugs, yield is inserted at certain I/O actions. 
    106   * For ghc in non-SMP mode,  yield is inserted after some count of allocations. 
    107   * For yhc,  yield is inserted after some count of bytecode instructions. 
    109 Arguably, Hugs has made the wrong choice from a fairness point of view.  It would be possible to make Hugs yield more often, such as in IO-monad's bind operator, but even this wouldn't be quite enough for fairness, because a thread might hang indefinitely performing a non-IO computation.  Yielding outside of the IO monad in Hugs doesn't seem possible without overhauling the concurrency implementation completely. 
    111 The notable exception to the above is GHC in SMP mode, which can run multiple Haskell threads simultaneously in separate OS threads, and hence simultaneously on multiple CPUs if available.  This implementation is truly preemptive. 
    113 ---------- 
    114 = Levels = 
    116 There are several levels of concurrency support which require sucessivly more 
    117 implementation support and imply more implementation overhead. 
    119 == No Concurrency == 
    121 The report says nothing about concurrency at all 
    123 == Concurrent Friendly == 
    125 Enough is specified to allow people to write completley portable programs and 
    126 libraries that while they may not depend on concurrency, will not break in the 
    127 presence of it. See "Thread-safety" above. 
    129 == Concurrent Capabale == 
    131 This would allow concurrency needing programs to be written, but perhaps not 
    132 as transparently as it curently is with GHC. This would include everything 
    133 needed to write concurrent programs without anything that would imply a 
    134 run-time or implementation overhead in the non-concurrent case.  
    136 == Concurrent Built-in == 
     50= Proposal =  
    139 == Concurrent Preemptive/SMP == 
     53== what is provided == 
     55At least the following interface will be provided 
     57 * Control.Concurrent.MVar - everything except addMVarFinalizer 
     58 * Control.Concurrent - ThreadId,myThreadId,forkIO,yield,threadWaitRead[1],threadWaitWrite[1],threadDelay,threadSetPriority[2],threadGetPriority[2] 
     60the FFI must be able to handle 'concurrent nonrentrant' imports, but not 
     61necessarily 'concurrent reentrant' ones. 
     63== Progress Guarentee == 
     65 * if any haskell thread is runnable then at least one thread will be running. 
     66 * foreign calls marked 'concurrent' will not interfere will the above rule. 
     68== MVar Guarentees == 
     70initial proposal is here: 
     74== Misc library stuff == 
     76yield is guarenteed to choose an alternate thread if another one exists and is 
     79[2] priorities are advisory, but higher priority threads should be run in 
     80preference to lower priority ones. 
     82threadDelay guarentees the thread will wait as long as its argument at a 
     83minimum. it may be blocked for longer. 
     85== notes == 
     87[1] may be moved to another module, routines working on Handles should be 
     88added too. 
     90[2] possibly, if we decide to go with priorities 
     93= Optional extensions to basic standard = 
     95These are optional extensions a compiler may implement. In some 
     96implementations they may entail a run-time cost to non-concurrent code or a 
     97compiler might need a special option to enable them. However, A compiler is 
     98not required to provide more than one concurrency model as long as it can meet 
     99the requirements of the standard and any options it claims to support. 
     101If a compiler documents that it supports one of the following options, then it 
     102must adhere to the rules of that option as well. 
     104== Optional Feature 1 - Preemption == 
     107The standard only requires a progress guarentee, that a thread is always 
     108running, making progress. If an implementation supports context switching 
     109during arbitrary, perhaps even pure, computations and meets the stronger 
     110fairness guarentees, then it can be said to support the 'Preemption' option.  
     112Fairness Guarentee  
     113 * no starvation 
     115new library calls provided 
     116 * mergeIO, nmergeIO 
     118== Optional Feature 2 - OS threads == 
     120The implementation allows multiple haskell threads to run at once via the 
     121operating systems threading service. May take advantange of SMP architectures. 
     123 * forkOS,isCurrentThreadBound,runInBoundThread,runInUnboundThread 
     124 * bound threads 
     125 * concurrent reentrant supported