TreeCounter (empty) → 0.0.1
raw patch · 4 files changed
+111/−0 lines, 4 filesdep +basedep +monad-loopsdep +ref-mtlsetup-changed
Dependencies added: base, monad-loops, ref-mtl, stm
Files
- Data/NonBlocking/WaitFree/TreeCounter.hs +72/−0
- LICENSE +20/−0
- Setup.hs +3/−0
- TreeCounter.cabal +16/−0
+ Data/NonBlocking/WaitFree/TreeCounter.hs view
@@ -0,0 +1,72 @@+{-# LANGUAGE TupleSections #-}++{-|+Module : TreeCounter+Description : Wait-free Tree Counter.+License : BSD3+Maintainer : Julian Sutherland (julian.sutherland10@imperial.ac.uk)++A wait-free tree counter. Creates a binary tree of counters, with each leaf associated with a thread. Leaves can be split, creating a new leaf for the current thread and another that can be used by another thread. Each thread will act on different leaves, meaning the actions are wait-free. A read is performed on the counter by recursively traversing it and summing the value of the counters in the nodes and leaves of the tree.+-}++module Data.NonBlocking.WaitFree.TreeCounter(TreeCounter(), TreeCounterIO, TreeCounterSTM, newTreeCounter, splitTreeCounter, incTreeCounter, readTreeCounter) where++import Control.Concurrent.STM (STM())+import Control.Concurrent.STM.TVar (TVar())+import Control.Monad (join)+import Control.Monad.Ref (MonadAtomicRef, newRef, readRef, writeRef, atomicModifyRef)+import Data.IORef(IORef())++-- |TreeCounter inside the IO Monad.+type TreeCounterIO = TreeCounter IORef+-- |TreeCounter inside the STM Monad.+type TreeCounterSTM = TreeCounter TVar++-- |A wait-free concurrent Tree Counter, a binary tree of counters, with each leaf associated with a thread. Leaves can be split, creating a new leaf for the current thread and another that can be used by another thread. Increments are wait-free as long as each thread performs them on different instance of TreeCounter split from an initial instance using 'splitTreeCounter', prone to ABA problem otherwise.+data TreeCounter r = TreeCounter (r (r (CounterTree r), r (CounterTree r)))+data CounterTree r = Node Integer (r (CounterTree r)) (r (CounterTree r)) | Leaf (r Integer)++-- |Creates a new instance of the 'TreeCounter' data type, instanciated to the value of the input, with type in the 'Integral' class. +{-# SPECIALIZE newTreeCounter :: (Integral a) => a -> IO TreeCounterIO #-}+{-# SPECIALIZE newTreeCounter :: (Integral a) => a -> STM TreeCounterSTM #-}+newTreeCounter :: (MonadAtomicRef r m, Integral a) => a -> m (TreeCounter r)+newTreeCounter n = newRef (toInteger n) >>= newRef . Leaf >>= newRef . join (,) >>= return . TreeCounter++-- |Splits a 'TreeCounter' instance, updating it to a new leaf and creating a new one, allowing another thread to increment the counter in a wait-free manner.+{-# SPECIALIZE splitTreeCounter :: TreeCounterIO -> IO TreeCounterIO #-}+{-# SPECIALIZE splitTreeCounter :: TreeCounterSTM -> STM TreeCounterSTM #-}+splitTreeCounter :: (MonadAtomicRef r m) => TreeCounter r -> m (TreeCounter r)+splitTreeCounter (TreeCounter tupleRef) = do+ (leafRef, rootRef) <- readRef tupleRef+ (Leaf lCountRef) <- readRef leafRef+ lCount <- readRef lCountRef+ leftRef <- newRef 0 >>= newRef . Leaf+ rightRef <- newRef 0 >>= newRef . Leaf+ writeRef leafRef (Node lCount leftRef rightRef)+ writeRef tupleRef (leftRef, rootRef)+ newRef (rightRef, rootRef) >>= return . TreeCounter++-- |Increments the 'TreeCounter' in an atomic manner as long as this thread is the only thread incrementing the counter from this instance 'TreeCounter'+{-# SPECIALIZE incTreeCounter :: TreeCounterIO -> IO () #-}+{-# SPECIALIZE incTreeCounter :: TreeCounterSTM -> STM () #-}+incTreeCounter :: (MonadAtomicRef r m) => TreeCounter r -> m ()+incTreeCounter (TreeCounter tupleRef) = do+ (leafRef, _) <- readRef tupleRef+ (Leaf lCountRef) <- readRef leafRef+ atomicModifyRef lCountRef ((,()) . (+1))++-- |Reads the total value of the binary tree of counters associated with this instance of 'TreeCounter'.+{-# SPECIALIZE readTreeCounter :: (Num a) => TreeCounterIO -> IO a #-}+{-# SPECIALIZE readTreeCounter :: (Num a) => TreeCounterSTM -> STM a #-}+readTreeCounter :: (MonadAtomicRef r m, Num a) => TreeCounter r -> m a+readTreeCounter (TreeCounter tupleRef) = readRef tupleRef >>= readRef . snd >>= sumTree >>= return . fromInteger++{-# SPECIALIZE sumTree :: CounterTree IORef -> IO Integer #-}+{-# SPECIALIZE sumTree :: CounterTree TVar -> STM Integer #-}+sumTree :: (MonadAtomicRef r m) => CounterTree r -> m Integer+sumTree (Leaf lCountRef) = readRef lCountRef+sumTree (Node nCount leftRef rightRef) = do+ lCount <- readRef leftRef >>= sumTree+ rCount <- readRef rightRef >>= sumTree+ return (nCount + lCount + rCount)+
+ 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
+ TreeCounter.cabal view
@@ -0,0 +1,16 @@+Name: TreeCounter+Version: 0.0.1+Cabal-Version: >= 1.2+License: BSD3+Author: Julian Sutherland+Homepage: https://github.com/Julek+Category: Data+Synopsis: Wait-free Tree Counter+Build-Type: Simple+Maintainer: Julian Sutherland (julian.sutherland10@imperial.ac.uk)+Description: A wait-free tree counter. Creates a binary tree of counters, with each leaf associated with a thread. Leaves can be split, creating a new leaf for the current thread and another that can be used by another thread. Each thread will act on different leaves, meaning the actions are wait-free. A read is performed on the counter by recursively traversing it and summing the value of the counters in the nodes and leaves of the tree.+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.WaitFree.TreeCounter