| 39 | | * A separate addendum specifies concurrency according to the ''fairness'' requirements (see below). |
| 40 | | |
| 41 | | == Proposal 2 == |
| 42 | | |
| 43 | | * The Haskell' standard includes concurrency with the ''fairness'' requirements. |
| 44 | | |
| 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. |
| 47 | | |
| 48 | | == Proposal 3 == |
| 49 | | |
| 50 | | * The Haskell' standard includes concurrency with a weaker version of ''fairness'', that is enough |
| 51 | | to admit implementations with ''cooperative scheduling''. |
| 52 | | |
| 53 | | * A separate addendum specifies concurrency with the full ''fairness'' requirements. |
| 54 | | |
| 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). |
| 60 | | * Do we require ForeignBlocking? |
| 61 | | |
| 62 | | == Thread-safety == |
| 63 | | |
| 64 | | In order to write library code that is thread-safe when run in a multi-threaded environment, two things are needed: |
| 65 | | |
| 66 | | 1. a way to protect mutable state against race conditions |
| 67 | | 2. a way to declare that foreign calls are blocking |
| 68 | | |
| 69 | | For (1), we have two choices: |
| 70 | | |
| 71 | | * Provide {{{MVar}}}s. A non-concurrent implementation might implement them in terms of {{{IORef}}}, |
| 72 | | for example. |
| 73 | | |
| 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 [http://www.haskell.org//pipermail/haskell-prime/2006-March/001108.html implementation]. |
| 76 | | |
| 77 | | For (2), one option is ForeignBlocking. |
| 78 | | |
| 79 | | ---------- |
| 80 | | = Terminology = |
| 81 | | |
| 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. |
| 89 | | |
| 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. |
| 93 | | |
| 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. |
| 95 | | |
| 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. |
| 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. |
| 108 | | |
| 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. |
| 110 | | |
| 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. |
| 112 | | |
| 113 | | ---------- |
| 114 | | = Levels = |
| 115 | | |
| 116 | | There are several levels of concurrency support which require sucessivly more |
| 117 | | implementation support and imply more implementation overhead. |
| 118 | | |
| 119 | | == No Concurrency == |
| 120 | | |
| 121 | | The report says nothing about concurrency at all |
| 122 | | |
| 123 | | == Concurrent Friendly == |
| 124 | | |
| 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. |
| 128 | | |
| 129 | | == Concurrent Capabale == |
| 130 | | |
| 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. |
| 135 | | |
| 136 | | == Concurrent Built-in == |
| | 50 | = Proposal = |
| 139 | | == Concurrent Preemptive/SMP == |
| | 53 | == what is provided == |
| | 54 | |
| | 55 | At least the following interface will be provided |
| | 56 | |
| | 57 | * Control.Concurrent.MVar - everything except addMVarFinalizer |
| | 58 | * Control.Concurrent - ThreadId,myThreadId,forkIO,yield,threadWaitRead[1],threadWaitWrite[1],threadDelay,threadSetPriority[2],threadGetPriority[2] |
| | 59 | |
| | 60 | the FFI must be able to handle 'concurrent nonrentrant' imports, but not |
| | 61 | necessarily 'concurrent reentrant' ones. |
| | 62 | |
| | 63 | == Progress Guarentee == |
| | 64 | |
| | 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. |
| | 67 | |
| | 68 | == MVar Guarentees == |
| | 69 | |
| | 70 | initial proposal is here: |
| | 71 | |
| | 72 | http://www.haskell.org//pipermail/haskell-prime/2006-March/001168.html |
| | 73 | |
| | 74 | == Misc library stuff == |
| | 75 | |
| | 76 | yield is guarenteed to choose an alternate thread if another one exists and is |
| | 77 | runnable. |
| | 78 | |
| | 79 | [2] priorities are advisory, but higher priority threads should be run in |
| | 80 | preference to lower priority ones. |
| | 81 | |
| | 82 | threadDelay guarentees the thread will wait as long as its argument at a |
| | 83 | minimum. it may be blocked for longer. |
| | 84 | |
| | 85 | == notes == |
| | 86 | |
| | 87 | [1] may be moved to another module, routines working on Handles should be |
| | 88 | added too. |
| | 89 | |
| | 90 | [2] possibly, if we decide to go with priorities |
| | 91 | |
| | 92 | |
| | 93 | = Optional extensions to basic standard = |
| | 94 | |
| | 95 | These are optional extensions a compiler may implement. In some |
| | 96 | implementations they may entail a run-time cost to non-concurrent code or a |
| | 97 | compiler might need a special option to enable them. However, A compiler is |
| | 98 | not required to provide more than one concurrency model as long as it can meet |
| | 99 | the requirements of the standard and any options it claims to support. |
| | 100 | |
| | 101 | If a compiler documents that it supports one of the following options, then it |
| | 102 | must adhere to the rules of that option as well. |
| | 103 | |
| | 104 | == Optional Feature 1 - Preemption == |
| | 105 | |
| | 106 | |
| | 107 | The standard only requires a progress guarentee, that a thread is always |
| | 108 | running, making progress. If an implementation supports context switching |
| | 109 | during arbitrary, perhaps even pure, computations and meets the stronger |
| | 110 | fairness guarentees, then it can be said to support the 'Preemption' option. |
| | 111 | |
| | 112 | Fairness Guarentee |
| | 113 | * no starvation |
| | 114 | |
| | 115 | new library calls provided |
| | 116 | * mergeIO, nmergeIO |
| | 117 | |
| | 118 | == Optional Feature 2 - OS threads == |
| | 119 | |
| | 120 | The implementation allows multiple haskell threads to run at once via the |
| | 121 | operating systems threading service. May take advantange of SMP architectures. |
| | 122 | |
| | 123 | * forkOS,isCurrentThreadBound,runInBoundThread,runInUnboundThread |
| | 124 | * bound threads |
| | 125 | * concurrent reentrant supported |