packages feed

threads-extras (empty) → 0.1.0.0

raw patch · 5 files changed

+157/−0 lines, 5 filesdep +basedep +stmdep +threadssetup-changed

Dependencies added: base, stm, threads, threads-extras

Files

+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Author name here (c) 2016++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.
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Control/Concurrent/Thread/BoundedThreadGroup.hs view
@@ -0,0 +1,84 @@+{-| This module wraps Control.Concurrent.Thread.Group and provides a bounded +    version of ThreadGroup, mainly 'BoundedThreadGroup'.+    +    In addition to the functionality of 'ThreadGroup', 'BoundedThreadGroup'+    will block creation of threads until there fewer than the given +    max size.+-}+{-# LANGUAGE RecordWildCards, RankNTypes #-}+module Control.Concurrent.Thread.BoundedThreadGroup where+import Control.Concurrent.Thread.Group (ThreadGroup)+import qualified Control.Concurrent.Thread as Thread+import qualified Control.Concurrent.Thread.Group as ThreadGroup+import Control.Concurrent+import Control.Concurrent.STM++-- | A 'BoundedThreadGroup' extends the concept of a 'ThreadGroup' by+--   restricting the number of active threads to a given max size.+--   If one attempts to create more than the max size number of threads+--   the fork function will block until threads finish.+data BoundedThreadGroup = BoundedThreadGroup +    { maxSize     :: Int+    , threadGroup :: ThreadGroup+    }++-- | Create a new 'BoundedThreadGroup' with the passed in max size+new :: Int -> IO BoundedThreadGroup+new maxSize = do +  threadGroup <- ThreadGroup.new +  return BoundedThreadGroup {..}++-- | Same as Control.Concurrent.Thread.Group.nrOfRunning+nrOfRunning :: BoundedThreadGroup -> STM Int+nrOfRunning BoundedThreadGroup {..} = ThreadGroup.nrOfRunning threadGroup++-- | Same as Control.Concurrent.Thread.Group.wait+wait :: BoundedThreadGroup -> IO ()+wait BoundedThreadGroup {..} = ThreadGroup.wait threadGroup++-- | Same as Control.Concurrent.Thread.Group.waitN+waitN :: Int -> BoundedThreadGroup -> IO ()+waitN i BoundedThreadGroup {..} = ThreadGroup.waitN i threadGroup++-- | Same as Control.Concurrent.Thread.Group.forkIO but waits there are less+--   then the max size number of threads.+forkIO :: BoundedThreadGroup -> IO a -> IO (ThreadId, IO (Thread.Result a))+forkIO BoundedThreadGroup {..} action = do+  ThreadGroup.waitN maxSize threadGroup+  ThreadGroup.forkIO threadGroup action ++-- | Same as Control.Concurrent.Thread.Group.forkOS but waits there are less+--   then the max size number of threads.+forkOS :: BoundedThreadGroup -> IO a -> IO (ThreadId, IO (Thread.Result a))+forkOS BoundedThreadGroup {..} action = do+  ThreadGroup.waitN maxSize threadGroup+  ThreadGroup.forkOS threadGroup action ++-- | Same as Control.Concurrent.Thread.Group.forkOn but waits there are less+--   then the max size number of threads.+forkOn :: Int +       -> BoundedThreadGroup +       -> IO a +       -> IO (ThreadId, IO (Thread.Result a))+forkOn i BoundedThreadGroup {..} action = do+  ThreadGroup.waitN maxSize threadGroup+  ThreadGroup.forkOn i threadGroup action++-- | Same as Control.Concurrent.Thread.Group.forkIOWithUnmask +--   but waits there are less then the max size number of threads.+forkIOWithUnmask :: BoundedThreadGroup+                 -> ((forall b. IO b -> IO b) -> IO a)+                 -> IO (ThreadId, IO (Thread.Result a))+forkIOWithUnmask BoundedThreadGroup {..} f = do +  ThreadGroup.waitN maxSize threadGroup+  ThreadGroup.forkIOWithUnmask threadGroup f++-- | Same as Control.Concurrent.Thread.Group.forkOnWithUnmask +--   but waits there are less then the max size number of threads.+forkOnWithUnmask :: Int +                 -> BoundedThreadGroup +                 -> ((forall b. IO b -> IO b) -> IO a) +                 -> IO (ThreadId, IO (Thread.Result a))+forkOnWithUnmask i BoundedThreadGroup {..} f = do +  ThreadGroup.waitN maxSize threadGroup+  ThreadGroup.forkOnWithUnmask i threadGroup f
+ test/Spec.hs view
@@ -0,0 +1,2 @@+main :: IO ()+main = putStrLn "Test suite not yet implemented"
+ threads-extras.cabal view
@@ -0,0 +1,39 @@+name: threads-extras+version: 0.1.0.0+cabal-version: >=1.10+build-type: Simple+license: BSD3+license-file: LICENSE+copyright: 2016 Author name here+maintainer: example@example.com+homepage: https://github.com/githubuser/threads-extras#readme+synopsis: Initial project template from stack+description:+    Please see README.md+category: Web+author: Author name here++source-repository head+    type: git+    location: https://github.com/skedgeme/threads-extras++library+    exposed-modules:+        Control.Concurrent.Thread.BoundedThreadGroup+    build-depends:+        base >=4.7 && <5,+        threads >=0.5.1.4 && <0.6,+        stm >=2.4.4.1 && <2.5+    default-language: Haskell2010+    hs-source-dirs: src+    ghc-options: -Wall++test-suite threads-extras-test+    type: exitcode-stdio-1.0+    main-is: Spec.hs+    build-depends:+        base >=4.8.2.0 && <4.9,+        threads-extras >=0.1.0.0 && <0.2+    default-language: Haskell2010+    hs-source-dirs: test+    ghc-options: -threaded -rtsopts -with-rtsopts=-N