lockpool (empty) → 0.1.0.0
raw patch · 5 files changed
+175/−0 lines, 5 filesdep +basedep +clockdep +stmsetup-changed
Dependencies added: base, clock, stm
Files
- ChangeLog.md +5/−0
- Control/Concurrent/LockPool.hs +114/−0
- LICENSE +30/−0
- Setup.hs +2/−0
- lockpool.cabal +24/−0
+ ChangeLog.md view
@@ -0,0 +1,5 @@+# Revision history for lockpool++## 0.1.0.0 -- YYYY-mm-dd++* First version. Released on an unsuspecting world.
+ Control/Concurrent/LockPool.hs view
@@ -0,0 +1,114 @@+{-# LANGUAGE BangPatterns #-}++module Control.Concurrent.LockPool+ ( LockPool+ , LockingStats(..)+ , withLockPool+ , withLockPoolStats+ , incrementLockPoolSize+ , decrementLockPoolSize+ , readLockPoolSize+ , newLockPool+ ) where++import Control.Monad.STM+import Control.Concurrent.STM.TVar+import Control.Exception (bracket_,bracket)+import qualified System.Clock as CLK++-- | A LockPool lets us set an upper bound on the number of threads+-- that are doing something simultaneously.+data LockPool = LockPool + Int -- ^ absolute maximum holders+ (TVar Int) -- ^ maximum number of lock holders+ (TVar Int) -- ^ current number of lock holders++data Clocked a = Clocked !Integer !a++-- | The result of a computation along with some information+-- about how long we had to wait to acquire a lock and how+-- long the computation took.+data LockingStats = LockingStats+ { lsWaitedNanoseconds :: !Integer+ , lsActionNanoseconds :: !Integer+ , lsTakenLocks :: !Int+ -- ^ Count of taken locks before releasing the lock+ , lsTotalLocks :: !Int + -- ^ Count of total locks before adjusting the total+ }++stopwatch :: IO a -> IO (Clocked a)+stopwatch x = do+ t1 <- CLK.getTime CLK.Monotonic+ a <- x+ t2 <- CLK.getTime CLK.Monotonic+ return (Clocked (CLK.toNanoSecs (CLK.diffTimeSpec t2 t1)) a)++stopwatch_ :: IO a -> IO Integer+stopwatch_ x = do+ t1 <- CLK.getTime CLK.Monotonic+ _ <- x+ t2 <- CLK.getTime CLK.Monotonic+ return (CLK.toNanoSecs (CLK.diffTimeSpec t2 t1))++withLockPool :: LockPool -> IO a -> IO a+withLockPool (LockPool _ maxHoldersVar currentHoldersVar) action = + bracket_ acquire release action+ where+ acquire = atomically $ do+ maxHolders <- readTVar maxHoldersVar+ currentHolders <- readTVar currentHoldersVar+ check (currentHolders < maxHolders)+ writeTVar currentHoldersVar $! (currentHolders + 1)+ release = atomically $ modifyTVar' currentHoldersVar (subtract 1)++withLockPoolStats :: LockPool -> IO a -> IO (LockingStats,a)+withLockPoolStats (LockPool _ maxHoldersVar currentHoldersVar) x = + bracket acquire release action+ where+ acquire = stopwatch_ $ atomically $ do+ maxHolders <- readTVar maxHoldersVar+ currentHolders <- readTVar currentHoldersVar+ check (currentHolders < maxHolders)+ writeTVar currentHoldersVar $! (currentHolders + 1)+ action waitedNs = do+ Clocked actionNs a <- stopwatch x+ taken <- readTVarIO currentHoldersVar+ total <- readTVarIO maxHoldersVar+ return (LockingStats waitedNs actionNs taken total, a)+ release _ = atomically $ modifyTVar' currentHoldersVar (subtract 1)++-- | We do not allow the size to drop to zero since that+-- would halt all progress.+decrementLockPoolSize :: LockPool -> IO Int+decrementLockPoolSize (LockPool _ maxHoldersVar _) = atomically $ do+ i <- readTVar maxHoldersVar+ let i' = max 1 (i - 1)+ writeTVar maxHoldersVar $! i'+ return i'++incrementLockPoolSize :: LockPool -> IO Int+incrementLockPoolSize (LockPool absMax maxHoldersVar _) = atomically $ do+ i <- readTVar maxHoldersVar+ let i' = min absMax (i + 1)+ writeTVar maxHoldersVar $! i'+ return i'++readLockPoolSize :: LockPool -> IO Int+readLockPoolSize (LockPool _ maxHoldersVar _) = + readTVarIO maxHoldersVar++_readLockPoolHolds :: LockPool -> IO Int+_readLockPoolHolds (LockPool _ _ currentHoldersVar) = + readTVarIO currentHoldersVar++newLockPool :: + Int -- ^ absolute maximum number of holders+ -> IO LockPool+newLockPool n = LockPool n+ <$> newTVarIO 1+ <*> newTVarIO 0++-- setLockPoolSize :: LockPool -> Int -> IO ()+-- setLockPoolSize (LockPool maxHoldersVar _) !newMax =+-- atomically (writeTVar maxHoldersVar newMax)
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2018, Layer 3 Communications, Andrew Martin++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++ * Redistributions of source code must retain the above copyright+ notice, this list of conditions and the following disclaimer.++ * Redistributions in binary form must reproduce the above+ copyright notice, this list of conditions and the following+ disclaimer in the documentation and/or other materials provided+ with the distribution.++ * Neither the name of chessai nor the names of other+ contributors may be used to endorse or promote products derived+ from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ lockpool.cabal view
@@ -0,0 +1,24 @@+name: lockpool+version: 0.1.0.0+synopsis: set a maximum on the number of concurrent actions +description: with a 'LockPool', you can specify the+ maximum number of threads that are running+ concurrently.+homepage: https://github.com/chessai/lockpool.git+license: BSD3+license-file: LICENSE+author: Andrew Martin+maintainer: chessai1996@gmail.com+copyright: Layer 3 Communications, Andrew Martin +category: Concurrency+build-type: Simple+extra-source-files: ChangeLog.md+cabal-version: >=1.10++library+ exposed-modules: Control.Concurrent.LockPool+ other-extensions: BangPatterns+ build-depends: base >=4.10 && <5.0+ , clock+ , stm+ default-language: Haskell2010