[commit: packages/primitive] ghc-head: Add Data.Primitive.MutVar, bump version to 0.4.1 (1f732f8)
git at git.haskell.org
git at git.haskell.org
Thu Sep 26 11:44:46 CEST 2013
Repository : ssh://git@git.haskell.org/primitive
On branch : ghc-head
Link : http://git.haskell.org/packages/primitive.git/commitdiff/1f732f8028b0385910ddb1495258ee45acc35ff9
>---------------------------------------------------------------
commit 1f732f8028b0385910ddb1495258ee45acc35ff9
Author: Justin Bonnar <justinbonnar at gmail.com>
Date: Sun Jan 8 23:35:26 2012 -0700
Add Data.Primitive.MutVar, bump version to 0.4.1
>---------------------------------------------------------------
1f732f8028b0385910ddb1495258ee45acc35ff9
Data/Primitive/MutVar.hs | 58 ++++++++++++++++++++++++++++++++++++++++++++++
primitive.cabal | 7 +++++-
2 files changed, 64 insertions(+), 1 deletion(-)
diff --git a/Data/Primitive/MutVar.hs b/Data/Primitive/MutVar.hs
new file mode 100644
index 0000000..ebccd6a
--- /dev/null
+++ b/Data/Primitive/MutVar.hs
@@ -0,0 +1,58 @@
+{-# LANGUAGE MagicHash, UnboxedTuples, DeriveDataTypeable #-}
+
+-- |
+-- Module : Data.Primitive.MutVar
+-- Copyright : (c) Justin Bonnar 2011
+-- License : BSD-style
+--
+-- Maintainer : Justin Bonnar <justinbonnar at gmail.com>
+-- Portability : non-portable
+--
+-- Primitive boxed mutable variables
+--
+
+module Data.Primitive.MutVar (
+ MutVar(..),
+
+ newMutVar,
+ readMutVar,
+ writeMutVar,
+
+ modifyMutVar
+) where
+
+import Control.Monad.Primitive ( PrimMonad(..), primitive_ )
+import GHC.Prim ( MutVar#, sameMutVar#, newMutVar#, readMutVar#, writeMutVar# )
+import Data.Typeable ( Typeable )
+
+-- | A 'MutVar' behaves like a single-element mutable array associated
+-- with a primitive state token.
+data MutVar s a = MutVar (MutVar# s a)
+ deriving ( Typeable )
+
+instance Eq (MutVar s a) where
+ MutVar mva# == MutVar mvb# = sameMutVar# mva# mvb#
+
+-- | Create a new 'MutVar' with the specified initial value
+newMutVar :: PrimMonad m => a -> m (MutVar (PrimState m) a)
+{-# INLINE newMutVar #-}
+newMutVar initialValue = primitive $ \s# ->
+ case newMutVar# initialValue s# of
+ (# s'#, mv# #) -> (# s'#, MutVar mv# #)
+
+-- | Read the value of a 'MutVar'
+readMutVar :: PrimMonad m => MutVar (PrimState m) a -> m a
+{-# INLINE readMutVar #-}
+readMutVar (MutVar mv#) = primitive (readMutVar# mv#)
+
+-- | Write a new value into a 'MutVar'
+writeMutVar :: PrimMonad m => MutVar (PrimState m) a -> a -> m ()
+{-# INLINE writeMutVar #-}
+writeMutVar (MutVar mv#) newValue = primitive_ (writeMutVar# mv# newValue)
+
+-- | Mutate the contents of a 'MutVar'
+modifyMutVar :: PrimMonad m => MutVar (PrimState m) a -> (a -> a) -> m ()
+{-# INLINE modifyMutVar #-}
+modifyMutVar (MutVar mv#) g = primitive_ $ \s# ->
+ case readMutVar# mv# s# of
+ (# s'#, a #) -> writeMutVar# mv# (g a) s'#
diff --git a/primitive.cabal b/primitive.cabal
index 3e19e84..bb8faf3 100644
--- a/primitive.cabal
+++ b/primitive.cabal
@@ -1,5 +1,5 @@
Name: primitive
-Version: 0.4.0.1
+Version: 0.4.1
License: BSD3
License-File: LICENSE
Author: Roman Leshchinskiy <rl at cse.unsw.edu.au>
@@ -13,6 +13,10 @@ Description:
This package provides wrappers for primitive array operations from
GHC.Prim.
.
+ Changes in version 0.4.1
+ .
+ * New module "Data.Primitive.MutVar"
+ .
Changes in version 0.4.0.1
.
* Critical bug fix in @fillByteArray@
@@ -47,6 +51,7 @@ Library
Data.Primitive.Array
Data.Primitive.ByteArray
Data.Primitive.Addr
+ Data.Primitive.MutVar
Other-Modules:
Data.Primitive.Internal.Compat
More information about the ghc-commits
mailing list