packages feed

SpinCounter (empty) → 0.0.1

raw patch · 4 files changed

+91/−0 lines, 4 filesdep +basedep +monad-loopsdep +ref-mtlsetup-changed

Dependencies added: base, monad-loops, ref-mtl, stm

Files

+ Data/NonBlocking/LockFree/SpinCounter.hs view
@@ -0,0 +1,52 @@++{-|+Module      : SpinCounter+Description : Implementation of a lock-free Spin Counter.+License     : BSD3+Maintainer  : Julian Sutherland (julian.sutherland10@imperial.ac.uk)++An implementation of a lock-free spin counter. Works with any monad that has atomically modificable references.+-}++module Data.NonBlocking.LockFree.SpinCounter(SpinCounter(), SpinCounterIO, SpinCounterSTM, newSpinCounter, incSpinCounter, readSpinCounter) where++import Control.Concurrent.STM (STM())+import Control.Concurrent.STM.TVar (TVar())+import Control.Monad.Loops(whileM_)+import Control.Monad.Ref(MonadAtomicRef, newRef, readRef, writeRef, atomicModifyRef)+import Data.IORef(IORef)++-- |SpinCounter inside the IO Monad.+type SpinCounterIO = SpinCounter IORef+-- |SpinCounter inside the STM Monad.+type SpinCounterSTM = SpinCounter TVar++-- |A lock-free concurrent Spin counter usable in any monad, m, that is paired with a reference type, r, by an instance of 'MonadAtomicRef'. Can use Specializations 'SpinCounterIO' and 'SpinCounterSTM'+data SpinCounter r = SpinCounter (r Integer)++-- |Creates a new instance of the 'SpinCounter' data type initialized to value of the input to the function, an instance of the class 'Integral'.+{-# SPECIALIZE newSpinCounter :: (Integral a) => a -> IO (SpinCounterIO)   #-}+{-# SPECIALIZE newSpinCounter :: (Integral a) => a -> STM (SpinCounterSTM) #-}+newSpinCounter :: (MonadAtomicRef r m, Integral a) => a -> m (SpinCounter r)+newSpinCounter n = newRef (toInteger n) >>= return . SpinCounter++-- |Increments an instance of the 'SpinCounter' data type by one in a lock-free manner.+{-# SPECIALIZE incSpinCounter :: SpinCounterIO -> IO ()   #-}+{-# SPECIALIZE incSpinCounter :: SpinCounterSTM -> STM () #-}+incSpinCounter :: (MonadAtomicRef r m) => SpinCounter r -> m ()+incSpinCounter (SpinCounter ref) = do+  b <- newRef False+  whileM_ (readRef b >>= return . not) $ do+    v <- readRef ref+    cas ref v (v+1) >>= writeRef b++-- |Reads the value of an instance of the 'SpinCounter' data type in a lock-free manner.+{-# SPECIALIZE readSpinCounter :: (Num a) => SpinCounterIO -> IO a   #-}+{-# SPECIALIZE readSpinCounter :: (Num a) => SpinCounterSTM -> STM a #-}+readSpinCounter :: (MonadAtomicRef r m, Num a) => SpinCounter r -> m a+readSpinCounter (SpinCounter ref) = readRef ref >>= return . fromInteger++{-# SPECIALIZE cas :: IORef Integer -> Integer -> Integer -> IO Bool   #-}+{-# SPECIALIZE cas :: TVar Integer -> Integer -> Integer -> STM Bool   #-}+cas :: (MonadAtomicRef r m, Eq a) => r a -> a -> a -> m Bool+cas ref comp rep = atomicModifyRef ref (\val -> let b = val == comp in (if b then rep else val, b))
+ LICENSE view
@@ -0,0 +1,20 @@+<OWNER> = Julian Sutherland+<ORGANIZATION> = Imperial College London+<YEAR> = 2014++In the original BSD license, the occurrence of "copyright holder" in the 3rd clause read "ORGANIZATION", placeholder for "University of California". In the original BSD license, both occurrences of the phrase "COPYRIGHT HOLDERS AND CONTRIBUTORS" in the disclaimer read "REGENTS AND CONTRIBUTORS".++Here is the license template:++Copyright (c) <YEAR>, <OWNER>+All rights reserved.++Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:++1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.++2. 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.++3. Neither the name of the copyright holder nor the names of its 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 HOLDER 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,3 @@+import Distribution.Simple++main = defaultMain
+ SpinCounter.cabal view
@@ -0,0 +1,16 @@+Name:		SpinCounter+Version:	0.0.1+Cabal-Version:  >= 1.2+License:	BSD3+Author:		Julian Sutherland+Homepage:       https://github.com/Julek+Category:	Data+Synopsis:	Lock free Spin Counter+Build-Type:     Simple+Maintainer:     Julian Sutherland (julian.sutherland10@imperial.ac.uk)+Description:    A simple lock-free spin counter.+License-file:   LICENSE++Library+  Build-Depends:	base >= 4.6 && < 4.8, monad-loops >= 0.4.2 && < 0.5, ref-mtl <2.3 && >= 0.2.1, stm >= 0.2.4 && < 2.5+  Exposed-modules:      Data.NonBlocking.LockFree.SpinCounter