A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from http://hackage.haskell.org/packages/archive/base/4.5.0.0/doc/html/src/Control-Concurrent-QSem.html below:

Control/Concurrent/QSem.hs



#ifdef __GLASGOW_HASKELL__

#endif















module Control.Concurrent.QSem
        ( 
          QSem,         
          newQSem,      
          waitQSem,     
          signalQSem    
        ) where

import Prelude
import Control.Concurrent.MVar
import Control.Exception ( mask_ )
import Data.Typeable

#include "Typeable.h"










newtype QSem = QSem (MVar (Int, [MVar ()])) deriving Eq

INSTANCE_TYPEABLE0(QSem,qSemTc,"QSem")



newQSem :: Int -> IO QSem
newQSem initial =
    if initial < 0
    then fail "newQSem: Initial quantity must be non-negative"
    else do sem <- newMVar (initial, [])
            return (QSem sem)


waitQSem :: QSem -> IO ()
waitQSem (QSem sem) = mask_ $ do
   (avail,blocked) <- takeMVar sem  
   if avail > 0 then
     let avail' = avail1
     in avail' `seq` putMVar sem (avail',[])
    else do
     b <- newEmptyMVar
      
     putMVar sem (0, blocked++[b])
     takeMVar b


signalQSem :: QSem -> IO ()
signalQSem (QSem sem) = mask_ $ do
   (avail,blocked) <- takeMVar sem
   case blocked of
     [] -> let avail' = avail+1
           in avail' `seq` putMVar sem (avail',blocked)

     (b:blocked') -> do
           putMVar sem (0,blocked')
           putMVar b ()

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4