Chan is an abstract type representing an unbounded FIFO channel.
Modulerebase-1.21.2Haskell2010
Rebase.Control.Concurrent
- 5 types
- 54 values
- Packagerebase-1.21.2
- Exports59
- LanguageHaskell2010
- LicenceMIT
- SourceChan.hs
An MVar (pronounced "em-var") is a synchronising variable, used for communication between concurrent threads. It can be thought of as a box, which may be empty or full.
Instances3NFData1, Eq, NFData
Block the current thread until data is available to read on the given file descriptor (GHC only).
This will throw an IOError if the file descriptor was closed while this thread was blocked. To safely close a file descriptor that has been used with threadWaitRead, use closeFdWith.
Returns an STM action that can be used to wait for data to read from a file descriptor. The second returned value is an IO action that can be used to deregister interest in the file descriptor.
Block the current thread until data can be written to the given file descriptor (GHC only).
This will throw an IOError if the file descriptor was closed while this thread was blocked. To safely close a file descriptor that has been used with threadWaitWrite, use closeFdWith.
Returns an STM action that can be used to wait until data can be written to a file descriptor. The second returned value is an IO action that can be used to deregister interest in the file descriptor.
withMVar is an exception-safe wrapper for operating on the contents of an MVar. This operation is exception-safe: it will replace the original contents of the MVar if an exception is raised (see Control.Exception). However, it is only atomic if there are no other producers for this MVar. In other words, it cannot guarantee that, by the time withMVar gets the chance to write to the MVar, the value of the MVar has not been altered by a write operation from another thread.
Build a new QSem with a supplied initial quantity. The initial quantity must be at least 0.
Signal that a unit of the QSem is available.
Wait for a unit to become available.
Deprecated. use mkWeakMVar instead
A slight variation on modifyMVar_ that allows a value to be
returned (b) in addition to the modified value of the MVar.
Like modifyMVar, but the IO action in the second argument is executed with
asynchronous exceptions masked.
Like modifyMVar_, but the IO action in the second argument is executed with
asynchronous exceptions masked.
An exception-safe wrapper for modifying the contents of an MVar. Like withMVar, modifyMVar will replace the original contents of the MVar if an exception is raised during the operation. This function is only atomic if there are no other producers for this MVar. In other words, it cannot guarantee that, by the time modifyMVar_ gets the chance to write to the MVar, the value of the MVar has not been altered by a write operation from another thread.
Take a value from an MVar, put a new value into the MVar and return the value taken. This function is atomic only if there are no other producers for this MVar. In other words, it cannot guarantee that, by the time swapMVar gets the chance to write to the MVar, the value of the MVar has not been altered by a write operation from another thread.
Like withMVar, but the IO action in the second argument is executed
with asynchronous exceptions masked.
Check whether a given MVar is empty.
Notice that the boolean value returned is just a snapshot of the state of the MVar. By the time you get to react on its result, the MVar may have been filled (or emptied) - so be extremely careful when using this operation. Use tryTakeMVar instead if possible.
Create an MVar which is initially empty.
Create an MVar which contains the supplied value.
Put a value into an MVar. If the MVar is currently full, putMVar will wait until it becomes empty.
There are two further important properties of putMVar:
putMVar is single-wakeup. That is, if there are multiple threads blocked in putMVar, and the MVar becomes empty, only one thread will be woken up. The runtime guarantees that the woken thread completes its putMVar operation.
When multiple threads are blocked on an MVar, they are woken up in FIFO order. This is useful for providing fairness properties of abstractions built using MVars.
Atomically read the contents of an MVar. If the MVar is currently empty, readMVar will wait until it is full. readMVar is guaranteed to receive the next putMVar.
readMVar is multiple-wakeup, so when multiple readers are blocked on an MVar, all of them are woken up at the same time. The runtime guarantees that all woken threads complete their readMVar operation.
Compatibility note: Prior to base 4.7, readMVar was a combination of takeMVar and putMVar. This mean that in the presence of other threads attempting to putMVar, readMVar could block. Furthermore, readMVar would not receive the next putMVar if there was already a pending thread blocked on takeMVar. The old behavior can be recovered by implementing 'readMVar as follows:
readMVar :: MVar a -> IO a
readMVar m =
mask_ $ do
a <- takeMVar m
putMVar m a
return a
Return the contents of the MVar. If the MVar is currently empty, takeMVar will wait until it is full. After a takeMVar, the MVar is left empty.
There are two further important properties of takeMVar:
takeMVar is single-wakeup. That is, if there are multiple threads blocked in takeMVar, and the MVar becomes full, only one thread will be woken up. The runtime guarantees that the woken thread completes its takeMVar operation.
When multiple threads are blocked on an MVar, they are woken up in FIFO order. This is useful for providing fairness properties of abstractions built using MVars.
A non-blocking version of putMVar. The tryPutMVar function
attempts to put the value a into the MVar, returning True if
it was successful, or False otherwise.
A non-blocking version of takeMVar. The tryTakeMVar function
returns immediately, with Nothing if the MVar was empty, or
Just a if the MVar was full with contents a. After tryTakeMVar,
the MVar is left empty.
Duplicate a Chan: the duplicate channel begins empty, but data written to either channel from then on will be available from both. Hence this creates a kind of broadcast channel, where data written by anyone is seen by everyone else.
(Note that a duplicated channel is not equal to its original.
So: fmap (c /=) $ dupChan c returns True for all c.)
Return a lazy list representing the contents of the supplied
Chan, much like GHC.Internal.System.IO.hGetContents.
Build and return a new instance of Chan.
Read the next value from the Chan. Blocks when the channel is empty. Since the read end of a channel is an MVar, this operation inherits fairness guarantees of MVars (e.g. threads blocked in this operation are woken up in FIFO order).
Throws BlockedIndefinitelyOnMVar when the channel is empty and no other thread holds a reference to the channel.
Write a value to a Chan.
Write an entire list of items to a Chan.
Build a new QSemN with a supplied initial quantity. The initial quantity must be at least 0.
Signal that a given quantity is now available from the QSemN.
Wait for the specified quantity to become available.
Fork a thread and call the supplied function when the thread is about to terminate, with an exception or a returned value. The function is called with asynchronous exceptions masked.
forkFinally action and_then =
mask $ \restore ->
forkIO $ try (restore action) >>= and_thenThis function is useful for informing the parent when a child terminates, for example.
Like forkIO, this sparks off a new thread to run the IO computation passed as the first argument, and returns the ThreadId of the newly created thread.
However, forkOS creates a bound thread, which is necessary if you
need to call foreign (non-Haskell) libraries that make use of
thread-local state, such as OpenGL (see Control.Concurrent#boundthreads).
Using forkOS instead of forkIO makes no difference at all to the
scheduling behaviour of the Haskell runtime system. It is a common
misconception that you need to use forkOS instead of forkIO to
avoid blocking all the Haskell threads when making a foreign call;
this isn't the case. To allow foreign calls to be made without
blocking all the Haskell threads (with GHC), it is only necessary to
use the -threaded option when linking your program, and to make sure
the foreign import is not marked unsafe.
Like forkIOWithUnmask, but the child thread is a bound thread, as with forkOS.
Returns True if the calling thread is bound, that is, if it is safe to use foreign libraries that rely on thread-local state from the calling thread.
True if bound threads are supported.
If rtsSupportsBoundThreads is False, isCurrentThreadBound
will always return False and both forkOS and runInBoundThread will
fail.
Run the IO computation passed as the first argument. If the calling thread
is not bound, a bound thread is created temporarily. runInBoundThread
doesn't finish until the IO computation finishes.
You can wrap a series of foreign function calls that rely on thread-local state
with runInBoundThread so that you can use them without knowing whether the
current thread is bound.
Run the IO computation passed as the first argument. If the calling thread
is bound, an unbound thread is created temporarily using forkIO.
runInBoundThread doesn't finish until the IO computation finishes.
Use this function only in the rare case that you have actually observed a
performance loss due to the use of bound threads. A program that
doesn't need its main thread to be bound and makes heavy use of concurrency
(e.g. a web server), might want to wrap its main action in
runInUnboundThread.
Note that exceptions which are thrown to the current thread are thrown in turn to the thread that is executing the given computation. This ensures there's always a way of killing the forked thread.
Suspends the current thread for a given number of microseconds (GHC only).
There is no guarantee that the thread will be rescheduled promptly when the delay has expired, but the thread will never continue to run earlier than specified.
Be careful not to exceed maxBound :: Int, which on 32-bit machines is only
2147483647 μs, less than 36 minutes.
Consider using Control.Concurrent.Thread.Delay.delay from unbounded-delays package.
Creates a new thread to run the IO computation passed as the first argument, and returns the ThreadId of the newly created thread.
The new thread will be a lightweight, unbound thread. Foreign calls made by this thread are not guaranteed to be made by any particular OS thread; if you need foreign calls to be made by a particular OS thread, then use forkOS instead.
The new thread inherits the masked state of the parent (see
GHC.Control.Exception.mask).
The newly created thread has an exception handler that discards the exceptions BlockedIndefinitelyOnMVar, BlockedIndefinitelyOnSTM, and ThreadKilled, and passes all other exceptions to the uncaught exception handler.
WARNING: Exceptions in the new thread will not be rethrown in the thread that created it. This means that you might be completely unaware of the problem if/when this happens. You may want to use the async library instead.
Like forkIO, but the child thread is passed a function that can be used to unmask asynchronous exceptions. This function is typically used in the following way
... mask_ $ forkIOWithUnmask $ \unmask ->
catch (unmask ...) handlerso that the exception handler in the child thread is established with asynchronous exceptions masked, meanwhile the main body of the child thread is executed in the unmasked state.
Note that the unmask function passed to the child thread should only be used in that thread; the behaviour is undefined if it is invoked in a different thread.
Like forkIO, but lets you specify on which capability the thread should run. Unlike a forkIO thread, a thread created by forkOn will stay on the same capability for its entire lifetime (forkIO threads can migrate between capabilities according to the scheduling policy). forkOn is useful for overriding the scheduling policy when you know in advance how best to distribute the threads.
The Int argument specifies a capability number (see getNumCapabilities). Typically capabilities correspond to physical processors, but the exact behaviour is implementation-dependent. The value passed to forkOn is interpreted modulo the total number of capabilities as returned by getNumCapabilities.
GHC note: the number of capabilities is specified by the +RTS -N
option when the program is started. Capabilities can be fixed to
actual processor cores with +RTS -qa if the underlying operating
system supports that, although in practice this is usually unnecessary
(and may actually degrade performance in some cases - experimentation
is recommended).
Like forkIOWithUnmask, but the child thread is pinned to the given CPU, as with forkOn.
Returns the number of Haskell threads that can run truly simultaneously (on separate physical processors) at any given time. To change this value, use setNumCapabilities.
killThread raises the ThreadKilled exception in the given thread (GHC only).
killThread tid = throwTo tid ThreadKilledMake a weak pointer to a ThreadId. It can be important to do
this if you want to hold a reference to a ThreadId while still
allowing the thread to receive the BlockedIndefinitely family of
exceptions (e.g. BlockedIndefinitelyOnMVar). Holding a normal
ThreadId reference will prevent the delivery of
BlockedIndefinitely exceptions because the reference could be
used as the target of throwTo at any time, which would unblock
the thread.
Holding a Weak ThreadId, on the other hand, will not prevent the
thread from receiving BlockedIndefinitely exceptions. It is
still possible to throw an exception to a Weak ThreadId, but the
caller must use deRefWeak first to determine whether the thread
still exists.
Returns the ThreadId of the calling thread (GHC only).
Set the number of Haskell threads that can run truly simultaneously
(on separate physical processors) at any given time. The number
passed to forkOn is interpreted modulo this value. The initial
value is given by the +RTS -N runtime flag.
This is also the number of threads that will participate in parallel garbage collection. It is strongly recommended that the number of capabilities is not set larger than the number of physical processor cores, and it may often be beneficial to leave one or more cores free to avoid contention with other processes in the machine.
Returns the number of the capability on which the thread is currently
running, and a boolean indicating whether the thread is locked to
that capability or not. A thread is locked to a capability if it
was created with forkOn.
throwTo raises an arbitrary exception in the target thread (GHC only).
Exception delivery synchronizes between the source and target thread: throwTo does not return until the exception has been raised in the target thread. The calling thread can thus be certain that the target thread has received the exception. Exception delivery is also atomic with respect to other exceptions. Atomicity is a useful property to have when dealing with race conditions: e.g. if there are two threads that can kill each other, it is guaranteed that only one of the threads will get to kill the other.
Whatever work the target thread was doing when the exception was raised is not lost: the computation is suspended until required by another thread.
If the target thread is currently making a foreign call, then the
exception will not be raised (and hence throwTo will not return)
until the call has completed. This is the case regardless of whether
the call is inside a mask or not. However, in GHC a foreign call
can be annotated as interruptible, in which case a throwTo will
cause the RTS to attempt to cause the call to return; see the GHC
documentation for more details.
Important note: the behaviour of throwTo differs from that described in the paper "Asynchronous exceptions in Haskell" (http://research.microsoft.com/~simonpj/Papers/asynch-exns.htm). In the paper, throwTo is non-blocking; but the library implementation adopts a more synchronous design in which throwTo does not return until the exception is received by the target thread. The trade-off is discussed in Section 9 of the paper. Like any blocking operation, throwTo is therefore interruptible (see Section 5.3 of the paper). Unlike other interruptible operations, however, throwTo is always interruptible, even if it does not actually block.
There is no guarantee that the exception will be delivered promptly, although the runtime will endeavour to ensure that arbitrary delays don't occur. In GHC, an exception can only be raised when a thread reaches a safe point, where a safe point is where memory allocation occurs. Some loops do not perform any memory allocation inside the loop and therefore cannot be interrupted by a throwTo.
If the target of throwTo is the calling thread, then the behaviour is the same as throwIO, except that the exception is thrown as an asynchronous exception. This means that if there is an enclosing pure computation, which would be the case if the current IO operation is inside unsafePerformIO or unsafeInterleaveIO, that computation is not permanently replaced by the exception, but is suspended as if it had received an asynchronous exception.
Note that if throwTo is called with the current thread as the target, the exception will be thrown even if the thread is currently inside mask or uninterruptibleMask.
The yield action allows (forces, in a co-operative multitasking implementation) a context-switch to any other currently runnable threads (if any), and is occasionally useful when implementing concurrency abstractions.
A ThreadId is an abstract type representing a handle to a thread. ThreadId is an instance of Eq, Ord and Show, where the Ord instance implements an arbitrary total ordering over ThreadIds. The Show instance lets you convert an arbitrary-valued ThreadId to string form; showing a ThreadId value is occasionally useful when debugging or diagnosing the behaviour of a concurrent program.
Note: in GHC, if you have a ThreadId, you essentially have a pointer to the thread itself. This means the thread itself can't be garbage collected until you drop the ThreadId. This misfeature would be difficult to correct while continuing to support threadStatus.
Instances5Eq, Ord, Show, NFData, Hashable
Eq ThreadIdDefined in ghc-internal-9.1003.0 · GHC.Internal.Conc.SyncOrd ThreadIdDefined in ghc-internal-9.1003.0 · GHC.Internal.Conc.SyncShow ThreadIdDefined in ghc-internal-9.1003.0 · GHC.Internal.Conc.SyncNFData ThreadIdDefined in deepseq-1.5.0.0 · Control.DeepSeqHashable ThreadIdDefined in hashable-1.4.7.0 · Data.Hashable.Class