úÎI wI      !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGH Safe-InferredIStrict function composition. IJKLMNOPIJKLMIJKLMXBas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com> Trustworthy  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.  When the state is "locked" acquire will Q the transaction.  When the state is "unlocked" acquire will change the state to "locked". A non-blocking .  When the state is "unlocked"  tryAcquire changes the state to "locked"  and returns R.  When the state is "locked"  tryAcquire leaves the state unchanged and returns S. release changes the state to "unlocked" and returns immediately. 2Note that it is an error to release a lock in the "unlocked" state! 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. A non-blocking . tryWith0 is a convenience function which first tries to !acquire the lock. If that fails, T" 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 U the result of the computation is returned.  When the state is "locked", wait will Q the transaction  When the state is "unlocked" wait returns immediately. wait' does not alter the state of the lock.  Note that wait9 is just a convenience function which can be defined as:  wait l =  l V  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. WX   WX XBas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com>Safe .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. new 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 U x  immediately.  If the broadcast is "silent",  tryListen returns T immediately. HListen to a broadcast if it is available within a given amount of time. Like  (, but with a timeout. A return value of T 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 T 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 = block $  b x >>  b Y?Internally used function that performs the actual broadcast in   and ( then changes to the given final state. Set a broadcast to the "silent" state. Z[ Y   Z[ YXBas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com>Safe +An event is in one of two possible states: "set" or "cleared". 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 S 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 S without blocking. 9Negative timeouts are treated the same as a timeout of 0 ¼s. Returns R if the state of the event is "set" and S 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 = \ $  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>Safe  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 R.  When the state is "locked"  tryAcquire leaves the state unchanged and returns S. !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, T" 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 U 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 wait6 is just a convenience function we can be defined as:  wait l = block a  l V ! 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. bc !"#$%  !"#$%  !"#$% bc !"#$%XBas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com>Safe &The state of an '.  T indicates an "unlocked" state.  U (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. (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 R.  When the state is "locked"  tryAcquire leaves the state unchanged and returns S. ,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, T" 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 U 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 = block a * l V , l0+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. &'de()*+,-./0 &'()*+,-./0 '()*+,-./&0 &'de()*+,-./0XBas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com>SafefInternal state of the 1. 1@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. 2 Create a new 1 in the "free"$ state; either read or write access # can be acquired without blocking. 3 Create a new 1 in the "read"" state; only read can be acquired  without blocking. 4 Create a new 1 in the "write"! state; either acquiring read or  write will block. 5Acquire the read lock. 7Blocks if another thread has acquired write access. If  acquireRead terminates /without throwing an exception the state of the 1 will be "read". JImplementation note: Throws an exception when more than (maxBound :: Int) Bsimultaneous threads acquire the read lock. But that is unlikely. 6,Try to acquire the read lock; non blocking. Like 5 , but doesn't block. Returns R if the resulting state is "read", S otherwise. 7Release 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 1 which is not in the "read" state. 8MA 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. 9A non-blocking 82. First tries to acquire the lock. If that fails, TE 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 U, the result of the computation is returned. : When the state is "write", waitRead blocks until a call to =( 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 = O a 5 l V 7 l;Acquire 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 1 will be "write". <-Try to acquire the write lock; non blocking. Like ; , but doesn't block. Returns R if the resulting state is "write", S otherwise. =Release 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 1 which is not in the "write" state. >JA 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. ?A non-blocking >2. First tries to acquire the lock. If that fails, TE 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 U, the result of the computation is returned. @ When the state is "write" or "read"  waitWrite blocks until a call to = or 7( 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 = O a ; l V = lfghi1jklm23456789:;<=>?@no123456789:;<=>?@1234578:69;=>@<?fihg1jklm23456789:;<=>?@noXBas van Dijk <v.dijk.bas@gmail.com> , Roel van Dijk <vandijk.roel@gmail.com>SafeA:Concurrently readable and sequentially writable variable. B Create a new A. C7Execute an action that operates on the contents of the A. LThe action is guaranteed to have a consistent view of the stored value. Any function that attempts to F- the contents will block until the action is  completed. 3If another thread is modifying the contents of the A this function will 2block until the other thread finishes its action. DLike C but doesn't block. Returns U the result if read access $could be acquired without blocking, T otherwise. EModify the contents of an A. 2This function needs exclusive write access to the A. Only one thread can  modify an A* at the same time. All others will block. FModify the contents of an A! and return an additional value. Like E%, but allows a value to be returned (²) in addition to the modified value of the A. G%Attempt to modify the contents of an A. Like E , but doesn't block. Returns R if the contents could be  replaced, S otherwise. H%Attempt to modify the contents of an A! and return an additional value. Like F , but doesn't block. Returns U the additional value if the contents could be replaced, T otherwise. ApBCDEFGHqABCDEFGHABCDEFGH ApBCDEFGHqr             !"    #$ %&'()*+,-./0123 456789:;<=>?=>@=AB=CDEFGEFH=IJ=IK=LM NOP=>QR=ST=UV=LW N"N!XYZ$#[\]^3_`concurrent-extra-0.7.0.6Control.Concurrent.STM.LockControl.Concurrent.BroadcastControl.Concurrent.EventControl.Concurrent.LockControl.Concurrent.RLock Control.Concurrent.ReadWriteLockControl.Concurrent.ReadWriteVarUtilsLocknew newAcquiredacquire tryAcquirereleasewithtryWithwaitlocked BroadcastnewBroadcastinglisten tryListen listenTimeout broadcastsignalsilenceEventnewSet waitTimeoutisSetsetclearStateRLockstateRWLocknewAcquiredReadnewAcquiredWrite acquireReadtryAcquireRead releaseReadwithRead tryWithReadwaitRead acquireWritetryAcquireWrite releaseWrite withWrite tryWithWrite waitWriteRWVarmodify_modify tryModify_ tryModify∘!ifMpurelyModifyMVar modifyIORefM modifyIORefM_baseGHC.IOmaskmask_ Control.Monadvoid GHC.Conc.Syncretryghc-prim GHC.TypesTrueFalse Data.MaybeNothingJustGHC.Base>>un broadcastThen unBroadcastblock evBroadcastControl.ApplicativeliftA2Control.Exception.Basebracket_$WriteReadFreereadLock writeLock moduleName $fEqRWLock $fEqRWVar