packages feed

Treiber (empty) → 0.0.1

raw patch · 4 files changed

+96/−0 lines, 4 filesdep +basedep +ghc-primdep +monad-loopssetup-changed

Dependencies added: base, ghc-prim, monad-loops, ref-mtl

Files

+ Data/NonBlocking/LockFree/Treiber.hs view
@@ -0,0 +1,57 @@+{-# LANGUAGE BangPatterns, MagicHash #-}+module Data.NonBlocking.LockFree.Treiber(TreiberStack(), newTreiberStack, pushTreiberStack, popTreiberStack) where++import Control.Monad(join, when)+import Control.Monad.Loops(whileM_)+import Control.Monad.Ref+import GHC.Exts (Int(I#))+import GHC.Prim (reallyUnsafePtrEquality#)++-- |A Lock-free concurrent Treiber stack usable in any monad, m, that is paired with a reference type, r, by an instance of 'MonadAtomicRef'.+data TreiberStack r a = TreiberStack (r (TreiberElem r a))+data TreiberElem r a = TreiberElem a (r (TreiberElem r a)) | End++instance (Eq a) => Eq (TreiberElem r a) where+  End == End = True+  (TreiberElem x rest1) == (TreiberElem y rest2) = (x == y) && (ptrEq rest1 rest2)++-- |Creates a new empty instance of the 'TreiberStack'. Internally implemented with a reference of type r, which is why they must be atomically modifiable.+newTreiberStack :: (MonadAtomicRef r m, Eq a, Show a) => m (TreiberStack r a)+newTreiberStack = do+  ref <- newRef End+  return (TreiberStack ref)++-- |Pushes an element on to a Treiber stack.+pushTreiberStack :: (MonadAtomicRef r m, Eq a, Show a) => TreiberStack r a -> a -> m ()+pushTreiberStack (TreiberStack x) v = do+  let partConstr = TreiberElem v+  b <- newRef False+  whileM_ (readRef b >>= return . not) $ do+    z <- readRef x+    res <- newRef z+    suc <- cas x z (partConstr res)+    writeRef b suc++-- |Pops an element of a Treiber stack. Returns 'Nothing' if the stack is empty.+popTreiberStack :: (MonadAtomicRef r m, Eq a, Show a) => TreiberStack r a -> m (Maybe a)+popTreiberStack (TreiberStack x) = do+  b <- newRef False+  ret <- newRef Nothing+  whileM_ (readRef b >>= return . not) $ do+    y <- readRef x+    case y of+      End -> writeRef b True+      (TreiberElem elem rest) -> do+        z <- readRef rest+        suc <- cas x y z+        t <- readRef x+        writeRef b suc+        when suc $ writeRef ret (Just elem)+  readRef ret++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))++{-# NOINLINE ptrEq #-}+ptrEq :: a -> a -> Bool+ptrEq !x !y = I# (reallyUnsafePtrEquality# x y) == 1
+ 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
+ Treiber.cabal view
@@ -0,0 +1,16 @@+Name:		Treiber+Version:	0.0.1+Cabal-Version:  >= 1.2+License:	BSD3+Author:		Julian Sutherland+Homepage:       https://github.com/Julek+Category:	Data+Synopsis:	Lock free Treiber stack+Build-Type:     Simple+Maintainer:     Julian Sutherland (julian.sutherland10@imperial.ac.uk)+Description:    An implementation of Treiber stacks, a lock free stack. Works with any monad that has atomically modificable references.+License-file:   LICENSE++Library+  Build-Depends:	base >= 4.6 && < 4.8, ghc-prim >= 0.3 && < 0.4, monad-loops >= 0.4.2 && < 0.5, ref-mtl <0.4 && >= 0.3+  Exposed-modules:      Data.NonBlocking.LockFree.Treiber