summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorathanclark <>2017-11-14 18:12:00 (GMT)
committerhdiff <hdiff@hdiff.luite.com>2017-11-14 18:12:00 (GMT)
commita62ad5397c71772520411882238487344edff703 (patch)
tree94d921d845a3295286b404ca4f798e8f0beb614b
version 0.0.0HEAD0.0.0master
-rw-r--r--LICENSE30
-rw-r--r--README.md1
-rw-r--r--Setup.hs2
-rw-r--r--src/Control/Concurrent/STM/TSetChan.hs65
-rw-r--r--test/Spec.hs32
-rw-r--r--tsetchan.cabal37
6 files changed, 167 insertions, 0 deletions
diff --git a/LICENSE b/LICENSE
new file mode 100644
index 0000000..6a042c2
--- /dev/null
+++ b/LICENSE
@@ -0,0 +1,30 @@
+Copyright Author name here (c) 2017
+
+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 Author name here 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. \ No newline at end of file
diff --git a/README.md b/README.md
new file mode 100644
index 0000000..4cf418b
--- /dev/null
+++ b/README.md
@@ -0,0 +1 @@
+# tsetchan
diff --git a/Setup.hs b/Setup.hs
new file mode 100644
index 0000000..9a994af
--- /dev/null
+++ b/Setup.hs
@@ -0,0 +1,2 @@
+import Distribution.Simple
+main = defaultMain
diff --git a/src/Control/Concurrent/STM/TSetChan.hs b/src/Control/Concurrent/STM/TSetChan.hs
new file mode 100644
index 0000000..248bde4
--- /dev/null
+++ b/src/Control/Concurrent/STM/TSetChan.hs
@@ -0,0 +1,65 @@
+{-# LANGUAGE
+ RecordWildCards
+ , NamedFieldPuns
+ #-}
+
+module Control.Concurrent.STM.TSetChan
+ ( TSetChan, ChanID, newTSetChan, openTSetChan, closeTSetChan, writeTSetChan, readTSetChan
+ ) where
+
+import Data.IntMap.Strict (IntMap)
+import qualified Data.IntMap.Strict as IntMap
+import Control.Concurrent.STM (STM)
+import Control.Concurrent.STM.TVar (TVar, modifyTVar, readTVar, newTVar, writeTVar)
+import Control.Concurrent.STM.TChan (TChan, newBroadcastTChan, dupTChan, newTChan, writeTChan, readTChan)
+
+
+
+newtype ChanID = ChanID {getChanID :: Int}
+
+
+data TSetChan a = TSetChan
+ { tSetChanContent :: TVar (IntMap (TChan a))
+ , tSetChanBroadcast :: (TChan a)
+ , tSetChanNonce :: TVar Int
+ }
+
+
+newTSetChan :: STM (TSetChan a)
+newTSetChan = do
+ bCast <- newBroadcastTChan
+ content <- newTVar IntMap.empty
+ counter <- newTVar minBound
+ pure TSetChan
+ { tSetChanContent = content
+ , tSetChanBroadcast = bCast
+ , tSetChanNonce = counter
+ }
+
+
+openTSetChan :: TSetChan a -> STM ChanID
+openTSetChan TSetChan{..} = do
+ newChan <- dupTChan tSetChanBroadcast
+ nonce <- do
+ x <- readTVar tSetChanNonce
+ writeTVar tSetChanNonce (x+1)
+ pure x
+ xs <- readTVar tSetChanContent
+ writeTVar tSetChanContent (IntMap.insert nonce newChan xs)
+ pure (ChanID nonce)
+
+
+writeTSetChan :: TSetChan a -> a -> STM ()
+writeTSetChan TSetChan{tSetChanBroadcast} x = writeTChan tSetChanBroadcast x
+
+
+readTSetChan :: TSetChan a -> ChanID -> STM (Maybe a)
+readTSetChan TSetChan{tSetChanContent} (ChanID nonce) = do
+ xs <- readTVar tSetChanContent
+ case IntMap.lookup nonce xs of
+ Nothing -> pure Nothing
+ Just chan -> Just <$> readTChan chan
+
+
+closeTSetChan :: TSetChan a -> ChanID -> STM ()
+closeTSetChan TSetChan{tSetChanContent} (ChanID nonce) = modifyTVar tSetChanContent (IntMap.delete nonce)
diff --git a/test/Spec.hs b/test/Spec.hs
new file mode 100644
index 0000000..b9f4cfa
--- /dev/null
+++ b/test/Spec.hs
@@ -0,0 +1,32 @@
+{-# LANGUAGE
+ ScopedTypeVariables
+ #-}
+
+import Control.Monad (forever)
+import Control.Concurrent.STM (atomically)
+import Control.Concurrent.STM.TSetChan (TSetChan, newTSetChan, openTSetChan, closeTSetChan, writeTSetChan, readTSetChan)
+import Control.Concurrent.Async (async)
+import Control.Concurrent.STM.TSem (newTSem, waitTSem, signalTSem)
+
+main :: IO ()
+main = do
+ (xs :: TSetChan Int) <- atomically newTSetChan
+ c1 <- atomically $ openTSetChan xs
+ c2 <- atomically $ openTSetChan xs
+
+ q1 <- atomically $ newTSem 0
+ q2 <- atomically $ newTSem 0
+
+ _ <- async $ forever $ do
+ x <- atomically $ readTSetChan xs c1
+ print x
+ atomically $ signalTSem q1
+ _ <- async $ forever $ do
+ x <- atomically $ readTSetChan xs c2
+ print x
+ atomically $ signalTSem q2
+ atomically $ writeTSetChan xs 1
+
+ atomically $ do
+ waitTSem q1
+ waitTSem q2
diff --git a/tsetchan.cabal b/tsetchan.cabal
new file mode 100644
index 0000000..4b06827
--- /dev/null
+++ b/tsetchan.cabal
@@ -0,0 +1,37 @@
+name: tsetchan
+version: 0.0.0
+synopsis: Hides duplicating channels when broadcasting
+-- description:
+homepage: https://github.com/githubuser/tsetchan#readme
+license: BSD3
+license-file: LICENSE
+author: Author name here
+maintainer: example@example.com
+copyright: 2017 Author name here
+category: Web
+build-type: Simple
+extra-source-files: README.md
+cabal-version: >=1.10
+
+library
+ hs-source-dirs: src
+ exposed-modules: Control.Concurrent.STM.TSetChan
+ build-depends: base >= 4.7 && < 5
+ , stm
+ , containers
+ default-language: Haskell2010
+
+test-suite tsetchan-test
+ type: exitcode-stdio-1.0
+ hs-source-dirs: test
+ main-is: Spec.hs
+ build-depends: base
+ , tsetchan
+ , async
+ , stm
+ ghc-options: -threaded -rtsopts -with-rtsopts=-N
+ default-language: Haskell2010
+
+source-repository head
+ type: git
+ location: https://github.com/githubuser/tsetchan