packages feed

unsafe-promises (empty) → 0.0

raw patch · 4 files changed

+124/−0 lines, 4 filesdep +basedep +threadssetup-changed

Dependencies added: base, threads

Files

+ Control/Concurrent/Promise/Unsafe.hs view
@@ -0,0 +1,68 @@+-- |An experimental library for creating lazy promises that can be evaluated in +-- pure code.+--+-- Evaluation of a promise before its thread completes results in an +-- indefinite block. This is accomplished by the use of 'unsafeInterleaveIO'.+-- Thus, care should be taken in using this library, since it couples the+-- execution time of pure code with an arbitrary IO computation.+-- Using "System.Timeout" from the timeout package can help to ensure that+-- forcing a promise is always well-defined.+--+-- For safer implementations of promises, see "Control.Concurrent.Spawn" from +-- the spawn package, and "Control.Concurrent.Future" from the future package. +module Control.Concurrent.Promise.Unsafe+       ( -- *Creating lazy promises+         promise, tryPromise+         -- *Creating lists of lazy promises+       , promises, tryPromises+       ) where++import Control.Concurrent.Thread+import Control.Concurrent.Chan+import Control.Exception+import System.IO.Unsafe++import Control.Monad+import Control.Applicative+import Data.Function+import Prelude hiding (catch)++safePromises :: [IO a] -> IO (Chan (Result a))+safePromises ios = do+  c <- newChan+  forM ios $ +    \io ->  forkIO $ (writeChan c . Right =<< io)+                     `catch` (writeChan c . Left)+  return c+                   +-- |Forks an IO computation as a thread and immediately returns +-- a lazy future. Evaluating the future before the thread completes+-- causes it to wait for a result. If the thread halts with a thrown exception,+-- then evaluating the future will re-throw the exception.+promise :: IO a -> IO a+promise io = head <$> promises [io]++-- |Like 'promise', but does not rethrow exceptions. Instead the exception is+-- wrapped as part of the 'Result'.+tryPromise :: IO a -> IO (Result a)+tryPromise io = head <$> tryPromises [io]+++-- |Forks a sequence of IO computations in multiple threads, and immediately+-- returns a list of futures. The order of the futures is determined by+-- the order in which the threads terminate. If an exception is thrown by the+-- list of threads, then the exception is re-thrown when its corresponding+-- future is evaluated.+promises :: [IO a] -> IO [a]+promises ios = do+  c <- safePromises ios+  forM ios $ \_ -> unsafeInterleaveIO (result =<< readChan c)+{-# NOINLINE promises #-}+++-- |Like 'promises', but doesn't re-throw exceptions.+tryPromises :: [IO a] -> IO [Result a]+tryPromises ios = do+  c <- safePromises ios+  forM ios $ \_ -> unsafeInterleaveIO (readChan c)+{-# NOINLINE tryPromises #-}
+ LICENSE view
@@ -0,0 +1,25 @@+Copyright (c) 2012, Adam Curtis+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 Adam Curtis 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,2 @@+import Distribution.Simple+main = defaultMain
+ unsafe-promises.cabal view
@@ -0,0 +1,29 @@+Name: unsafe-promises+Version: 0.0+Cabal-Version: >= 1.6+License: BSD3+License-File: LICENSE+Author: Adam Curtis+Maintainer: acurtis@spsu.edu+Homepage: https://github.com/kallisti-dev/unsafe-promises+Category: Concurrency+Synopsis: Create pure futures using lazy IO.+Build-type: Simple+Description:+        An experimental library for creating lazy promises that can be+        evaluated in pure code.++source-repository this+  type: git+  location: git://github.com/kallisti-dev/unsafe-promises.git+  tag: 0.0++source-repository head+  type: git+  location: git://github.com/kallisti-dev/unsafe-promises.git++library+  ghc-options: -Wall+  exposed-modules: Control.Concurrent.Promise.Unsafe+  build-depends:  base == 4.*+                , threads >= 0.4 && < 0.6