ú΋úA      !"#$%&'()*+,-./0123456789:;<=>?@ XBas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com>Like A, but not bounded by an B. KSuspends the current thread for a given number of microseconds (GHC only). LThere is no guarantee that the thread will be rescheduled promptly when the Jdelay has expired, but the thread will never continue to run earlier than  specified. XBas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com>CDLike E, but not bounded by an B. Wrap an F$ computation to time out and return G in case no result is available within n microseconds (1/10^6 seconds). In case a result is &available before the timeout expires, H a! is returned. A negative timeout interval means "wait indefinitely". ?The design of this combinator was guided by the objective that  timeout n f "should behave exactly the same as f as long as f doesn't time out. This  means that f has the same I# it would have without the timeout wrapper. Any exceptions f6 might throw cancel the timeout and propagate further up. It also possible for f/ to receive exceptions thrown to it by another thread. BA tricky implementation detail is the question of how to abort an F Pcomputation. This combinator relies on asynchronous exceptions internally. The Ktechnique works very well for computations executing inside of the Haskell runtime system, but it doesn',t work at all for non-Haskell code. Foreign Mfunction calls, for example, cannot be timed out with this combinator simply Mbecause an arbitrary C function cannot receive asynchronous exceptions. When timeoutB is used to wrap an FFI call that blocks, no timeout event can be Odelivered until the FFI call returns, which pretty much negates the purpose of Qthe combinator. In practice, however, this limitation is less severe than it may sound. Standard I/O functions like J, K, Network.Socket.accept, or L appear to be blocking, but they really don'=t because the runtime system uses scheduling mechanisms like  select(2) to perform asynchronous I/"O, so it is possible to interrupt standard socket I/ O or file I/O using this combinator.  MStrict function composition. NOPQRMNOPQRMNOPQRXBas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com> .A broadcast is in one of two possible states:  "Silent": ing. to the broadcast will block until a value is ed.  " Broadcasting x": ing( to the broadcast will return the value x without blocking. STnew Creates a broadcast in the "silent" state. newBroadcasting x Creates a broadcast in the " broadcasting x" state. Listen to a broadcast.  If the broadcast is " broadcasting x", listen will return x  immediately.  If the broadcast is "silent", listen! will block until another thread s a value to the broadcast. ,Try to listen to a broadcast; non blocking.  If the broadcast is " broadcasting x",  tryListen will return H x  immediately.  If the broadcast is "silent",  tryListen returns G immediately. HListen to a broadcast if it is available within a given amount of time. Like (, but with a timeout. A return value of G indicates a timeout occurred. *The timeout is specified in microseconds. If the broadcast is "silent" and a timeout of 0 ¼s is specified the function returns G without blocking. 9Negative timeouts are treated the same as a timeout of 0 ¼s. Broadcast a value.  broadcast b x$ changes the state of the broadcast b to " broadcasting x". If the broadcast was "silent" all threads that are ing to the broadcast will be woken. "Broadcast a value before becoming "silent". )The state of the broadcast is changed to "silent" after all threads that are ingA to the broadcast are woken and resume with the signalled value. DThe semantics of signal are equivalent to the following definition:   signal b x = U $  b x >>   b V?Internally used function that performs the actual broadcast in   and  ( then changes to the given final state. Set a broadcast to the "silent" state.    XBas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com> +An event is in one of two possible states: "set" or "cleared". WX Create an event in the "cleared" state. Create an event in the "set" state. Block until the event is . %If the state of the event is already "set" this function will return @immediately. Otherwise it will block until another thread calls . J(You can also resume a thread that is waiting for an event by throwing an asynchronous exception.) Block until the event is  or until a timer expires. Like (, but with a timeout. A return value of Y indicates a timeout  occurred. *The timeout is specified in microseconds. If the event is "cleared" and a timeout of 0 ¼s is specified the function returns Y without blocking. 9Negative timeouts are treated the same as a timeout of 0 ¼s. Returns Z if the state of the event is "set" and Y if the state is "cleared". ONotice that this is only a snapshot of the state. By the time a program reacts -on its result it may already be out of date. "Changes the state of the event to "set"!. All threads that where waiting 'for this event are woken. Threads that  after the state is changed to "set" will not block at all. Changes the state to "cleared"/ after all threads that where waiting for this event are woken. Threads that  after a signal will block until the event is  again. DThe semantics of signal are equivalent to the following definition:   signal e = U $  e >>  e "Changes the state of the event to "cleared". Threads that  after the state is changed to "cleared"* will block until the state is changed to "set".   XBas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com>  A lock is in one of two states: "locked" or "unlocked". [\Create a lock in the "unlocked" state. Create a lock in the "locked" state.  Acquires the ,. Blocks if another thread has acquired the . acquire behaves as follows:  When the state is "unlocked" acquire changes the state to "locked".  When the state is "locked" acquire blocks until a call to  in Qanother thread wakes the calling thread. Upon awakening it will change the state to "locked". .There are two further important properties of acquire:  acquireE is single-wakeup. That is, if there are multiple threads blocked on acquireA and the lock is released, only one thread will be woken up. The 7runtime guarantees that the woken thread completes its acquire operation. & When multiple threads are blocked on acquire, they are woken up in FIFO Norder. This is useful for providing fairness properties of abstractions built Nusing locks. (Note that this differs from the Python implementation where the wake-up order is undefined.) A non-blocking .  When the state is "unlocked"  tryAcquire changes the state to "locked"  and returns True.  When the state is "locked"  tryAcquire leaves the state unchanged and returns False. release changes the state to "unlocked" and returns immediately. 2Note that it is an error to release a lock in the "unlocked" state! $If there are any threads blocked on  the thread that first called acquire will be woken up. KA convenience function which first acquires the lock and then performs the Pcomputation. When the computation terminates, whether normally or by raising an !exception, the lock is released.  Note that: with = ] ^  . A non-blocking . tryWith0 is a convenience function which first tries to !acquire the lock. If that fails, G" is returned. If it succeeds, the Ocomputation is performed. When the computation terminates, whether normally or 2by raising an exception, the lock is released and H the result of the computation is returned.  When the state is "locked", wait blocks until a call to  in another thread changes it to "unlocked".  When the state is "unlocked" wait returns immediately. wait' does not alter the state of the lock.  Note that wait, is just a convenience function defined as:  wait l = U _  l `  l!Determines if the lock is in the "locked" state. MNote that this is only a snapshot of the state. By the time a program reacts -on its result it may already be out of date.   XBas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com> The state of an .  G indicates an "unlocked" state.  H (tid, n) indicates a "locked"& state where the thread identified by tid acquired the lock n times. *A reentrant lock is in one of two states: "locked" or "unlocked". When the lock is in the "locked") state it has two additional properties:  Its owner%: the thread that acquired the lock.  Its acquired count.: how many times its owner acquired the lock. ab Create a reentrant lock in the "unlocked" state. !Create a reentrant lock in the "locked"# state (with the current thread as #owner and an acquired count of 1). " Acquires the ,. Blocks if another thread has acquired the . acquire behaves as follows:  When the state is "unlocked", acquire changes the state to "locked" =with the current thread as owner and an acquired count of 1.  When the state is "locked"& and the current thread owns the lock acquire $only increments the acquired count.  When the state is "locked". and the current thread does not own the lock acquire blocks> until the owner releases the lock. If the thread that called acquireE is woken upon release of the lock it will take ownership and change  the state to "locked" with an acquired count of 1. .There are two further important properties of acquire:  acquireE is single-wakeup. That is, if there are multiple threads blocked on acquireB, and the lock is released, only one thread will be woken up. The 7runtime guarantees that the woken thread completes its acquire operation. & When multiple threads are blocked on acquire they are woken up in FIFO Norder. This is useful for providing fairness properties of abstractions built Nusing locks. (Note that this differs from the Python implementation where the wake-up order is undefined.) #A non-blocking ".  When the state is "unlocked"  tryAcquire changes the state to "locked" J(with the current thread as owner and an acquired count of 1) and returns Z.  When the state is "locked"  tryAcquire leaves the state unchanged and returns Y. $release@ decrements the acquired count. When a lock is released with an ,acquired count of 1 its state is changed to "unlocked". 7Note that it is both an error to release a lock in the "unlocked" state and to 8release a lock that is not owned by the current thread. $If there are any threads blocked on " the thread that first called acquire will be woken up. %>A convenience function which first acquires the lock and then Cperforms the computation. When the computation terminates, whether ;normally or by raising an exception, the lock is released.  Note that: with = ] ^ " $. &A non-blocking %. tryWith0 is a convenience function which first tries to !acquire the lock. If that fails, G" is returned. If it succeeds, the Ocomputation is performed. When the computation terminates, whether normally or 2by raising an exception, the lock is released and H the result of the computation is returned. ' When the state is "locked" wait blocks until a call to $ in another thread changes it to "unlocked".  When the state is "unlocked" wait returns immediately. wait' does not alter the state of the lock.  Note that wait, is just a convenience function defined as:  wait l = U _ " l ` $ l(+Determine the state of the reentrant lock. PNote that this is only a snapshot of the state. By the time a program reacts on *its result it may already be out of date.  !"#$%&'(  !"#$%&'(  !"#$%&'(XBas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com>cInternal state of the ). def)@Multiple-reader, single-writer lock. Is in one of three states:  "Free"9: Read or write access can be acquired without blocking.  "Read"F: One or more threads have acquired read access. Blocks write access.  "Write"B: A single thread has acquired write access. Blocks other threads +from acquiring both read and write access. ghij* Create a new ) in the "free"$ state; either read or write access # can be acquired without blocking. + Create a new ) in the "read"" state; only read can be acquired  without blocking. , Create a new ) in the "write"! state; either acquiring read or  write will block. -Acquire the read lock. 7Blocks if another thread has acquired write access. If  acquireRead terminates /without throwing an exception the state of the ) will be "read". JImplementation note: Throws an exception when more than (maxBound :: Int) Bsimultaneous threads acquire the read lock. But that is unlikely. .,Try to acquire the read lock; non blocking. Like - , but doesn't block. Returns Z if the resulting state is "read", Y otherwise. /Release the read lock. PIf the calling thread was the last one to relinquish read access the state will  revert to "free". ,It is an error to release read access to an ) which is not in the "read" state. 0MA convenience function wich first acquires read access and then performs the Pcomputation. When the computation terminates, whether normally or by raising an &exception, the read lock is released. 1A non-blocking 02. First tries to acquire the lock. If that fails, GE is returned. If it succeeds, the computation is performed. When the Qcomputation terminates, whether normally or by raising an exception, the lock is  released and H, the result of the computation is returned. 2 When the state is "write", waitRead blocks until a call to 5( in another thread changes the state to "free".  When the state is "free" or "read" waitRead returns immediately. waitRead' does not alter the state of the lock.  Note that waitRead, is just a convenience function defined as:  waitRead l = U _ - l ` / l3Acquire the write lock. FBlocks if another thread has acquired either read or write access. If  acquireWrite; terminates without throwing an exception the state of the ) will be "write". 4-Try to acquire the write lock; non blocking. Like 3 , but doesn't block. Returns Z if the resulting state is "write", Y otherwise. 5Release the write lock. If  releaseWrite< terminates without throwing an exception the state will be "free". -It is an error to release write access to an ) which is not in the "write" state. 6JA convenience function wich first acquires write access and then performs Qthe computation. When the computation terminates, whether normally or by raising *an exception, the write lock is released. 7A non-blocking 62. First tries to acquire the lock. If that fails, GE is returned. If it succeeds, the computation is performed. When the Qcomputation terminates, whether normally or by raising an exception, the lock is  released and H, the result of the computation is returned. 8 When the state is "write" or "read"  waitWrite blocks until a call to 5 or /( in another thread changes the state to "free".  When the state is "free"  waitWrite returns immediately.  waitWrite' does not alter the state of the lock.  Note that  waitWrite, is just a convenience function defined as: waitWrite l = U _ 3 l ` 5 lk)*+,-./012345678)*+,-/02.1356847)*+,-./012345678XBas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com> 9:Concurrently readable and sequentially writable variable. l: Create a new 9. ;7Execute an action that operates on the contents of the 9. LThe action is guaranteed to have a consistent view of the stored value. Any function that attempts to >- the contents will block until the action is  completed. 3If another thread is modifying the contents of the 9 this function will 2block until the other thread finishes its action. <Like ; but doesn't block. Returns H the result if read access $could be acquired without blocking, G otherwise. =Modify the contents of an 9. 2This function needs exclusive write access to the 9. Only one thread can  modify an 9* at the same time. All others will block. >Modify the contents of an 9! and return an additional value. Like =%, but allows a value to be returned (²) in addition to the modified value of the 9. ?%Attempt to modify the contents of an 9. Like = , but doesn't block. Returns Z if the contents could be  replaced, Y otherwise. @%Attempt to modify the contents of an 9! and return an additional value. Like > , but doesn't block. Returns H the additional value if the contents could be replaced, G otherwise. 9:;<=>?@9:;<=>?@9:;<=>?@m       !"#$%  !"&' ()*+,-./0123456 !"789:;<=>?@AA;B >?C;DE;DF;<G;HI;HJ;HK L M N O P Q R;STUV>WX>WYZ;[\;]^;_`;_a%Z$bcd'&efg6hconcurrent-extra-0.5Control.Concurrent.Thread.DelayControl.Concurrent.TimeoutControl.Concurrent.BroadcastControl.Concurrent.EventControl.Concurrent.LockControl.Concurrent.RLock Control.Concurrent.ReadWriteLockControl.Concurrent.ReadWriteVarUtilsdelaytimeout BroadcastnewnewBroadcastinglisten tryListen listenTimeout broadcastsignalsilenceEventnewSetwait waitTimeoutisSetsetclearLock newAcquiredacquire tryAcquirereleasewithtryWithlockedStateRLockstateRWLocknewAcquiredReadnewAcquiredWrite acquireReadtryAcquireRead releaseReadwithRead tryWithReadwaitRead acquireWritetryAcquireWrite releaseWrite withWrite tryWithWrite waitWriteRWVarmodify_modify tryModify_ tryModifybaseGHC.Conc threadDelayghc-prim GHC.TypesIntTimeoutSystem.TimeoutIO Data.MaybeNothingJust myThreadIdGHC.IO.Handle.TexthGetBufhPutBuf hWaitForInput∘!voidifMpurelyModifyMVar modifyIORefM modifyIORefM_ unBroadcastGHC.IOblock broadcastThen evBroadcastGHC.BoolFalseTrueunControl.ApplicativeliftA2Control.Exception.Basebracket_GHC.Base$>>WriteReadFreereadLock writeLock moduleName