keera-callbacks (empty) → 0.1
raw patch · 5 files changed
+177/−0 lines, 5 filesdep +basedep +mtlsetup-changed
Dependencies added: base, mtl
Files
- LICENSE +30/−0
- Setup.hs +2/−0
- keera-callbacks.cabal +65/−0
- src/Data/CBMVar.hs +36/−0
- src/Data/CBRef.hs +44/−0
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c)2012, Ivan Perez++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 Ivan Perez 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
+ keera-callbacks.cabal view
@@ -0,0 +1,65 @@+-- keera-callbacks.cabal auto-generated by cabal init. For additional+-- options, see+-- http://www.haskell.org/cabal/release/cabal-latest/doc/users-guide/authors.html#pkg-descr.+-- The name of the package.+Name: keera-callbacks++-- The package version. See the Haskell package versioning policy+-- (http://www.haskell.org/haskellwiki/Package_versioning_policy) for+-- standards guiding when and how versions should be incremented.+Version: 0.1++-- A short (one-line) description of the package.+Synopsis: Mutable memory locations with callbacks++-- A longer description of the package.+Description: Mutable memory locations with handlers that are executed when the+ shared values are modified.++-- The license under which the package is released.+License: BSD3++-- The file containing the license text.+License-file: LICENSE++-- The package author(s).+Author: Ivan Perez++-- An email address to which users can send suggestions, bug reports,+-- and patches.+Maintainer: ivan.perez@keera.es++-- A copyright notice.+-- Copyright: ++Category: Concurrency++Build-type: Simple++-- Extra files to be distributed with the package, such as examples or+-- a README.+-- Extra-source-files: ++-- Constraint on the version of Cabal needed to build this package.+Cabal-version: >=1.2+++Library+ -- Modules exported by the library.+ Exposed-modules: Data.CBRef+ Data.CBMVar+ + -- Packages needed in order to build this package.+ hs-source-dirs: src/++ ghc-options: -Wall -fno-warn-unused-do-bind -O2++ Build-depends: base >= 4.0 && < 5.0+ , mtl+ + -- Modules not exported by this package.+ -- Other-modules: + + -- Extra tools (e.g. alex, hsc2hs, ...) needed to build the source.+ -- Build-tools: +
+ src/Data/CBMVar.hs view
@@ -0,0 +1,36 @@+module Data.CBMVar+ ( CBMVar+ , newCBMVar+ , readCBMVar+ , writeCBMVar+ , modifyCBMVar+ , installCallbackCBMVar+ )+ where++import Control.Concurrent.MVar++data CBMVar a = CBMVar (MVar (a, [IO ()]))++newCBMVar :: a -> IO (CBMVar a)+newCBMVar v = fmap CBMVar $ newMVar (v,[])++readCBMVar :: CBMVar a -> IO a+readCBMVar (CBMVar ref) = fmap fst $ readMVar ref++writeCBMVar :: CBMVar a -> a -> IO ()+writeCBMVar (CBMVar ref) v = do+ modifyMVar_ ref (\(_,cb) -> return (v,cb))+ runCallbacks ref++runCallbacks :: MVar (a, [IO ()]) -> IO ()+runCallbacks ref = (sequence_.snd) =<< readMVar ref++modifyCBMVar :: CBMVar a -> (a -> IO a) -> IO ()+modifyCBMVar (CBMVar ref) f = do+ modifyMVar_ ref (\(v,cb) -> f v >>= \v' -> return (v',cb))+ runCallbacks ref++installCallbackCBMVar :: CBMVar a -> IO () -> IO ()+installCallbackCBMVar (CBMVar ref) p =+ modifyMVar_ ref (\(v,cb) -> return (v,cb ++ [p]))
+ src/Data/CBRef.hs view
@@ -0,0 +1,44 @@+module Data.CBRef+ ( CBRef+ , newCBRef+ , readCBRef+ , writeCBRef+ , modifyCBRef+ , atomicModifyCBRef+ , installCallbackCBRef+ )+ where++import Data.IORef++data CBRef a = CBRef (IORef (a, [IO ()]))++newCBRef :: a -> IO (CBRef a)+newCBRef v = fmap CBRef $ newIORef (v,[])++readCBRef :: CBRef a -> IO a+readCBRef (CBRef ref) = fmap fst $ readIORef ref++writeCBRef :: CBRef a -> a -> IO ()+writeCBRef (CBRef ref) v = do+ modifyIORef ref (\(_,cb) -> (v,cb))+ runCallbacks ref++runCallbacks :: IORef (a, [IO ()]) -> IO ()+runCallbacks ref = (sequence_.snd) =<< readIORef ref++modifyCBRef :: CBRef a -> (a -> a) -> IO ()+modifyCBRef (CBRef ref) f = do+ modifyIORef ref (\(v,cb) -> (f v,cb))+ runCallbacks ref++atomicModifyCBRef :: CBRef a -> (a -> (a, b)) -> IO b+atomicModifyCBRef (CBRef ref) f = do+ v <- atomicModifyIORef ref modifier+ runCallbacks ref+ return v+ where modifier (v,cb) = let (v', r) = f v in ((v',cb), r)++installCallbackCBRef :: CBRef a -> IO () -> IO ()+installCallbackCBRef (CBRef ref) p =+ modifyIORef ref (\(v,cb) -> (v,cb ++ [p]))