packages feed

thread-utils-finalizers (empty) → 0.1.0.0

raw patch · 7 files changed

+132/−0 lines, 7 filesdep +basedep +ghc-primdep +thread-utils-finalizerssetup-changed

Dependencies added: base, ghc-prim, thread-utils-finalizers

Files

+ ChangeLog.md view
@@ -0,0 +1,3 @@+# Changelog for thread-finalizers++## Unreleased changes
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright Ian Duncan (c) 2021++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 Ian Duncan 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.
+ README.md view
@@ -0,0 +1,1 @@+# thread-finalizers
+ Setup.hs view
@@ -0,0 +1,2 @@+import Distribution.Simple+main = defaultMain
+ src/Control/Concurrent/Thread/Finalizers.hs view
@@ -0,0 +1,42 @@+{-# LANGUAGE MagicHash #-}+{-# LANGUAGE UnboxedTuples #-}+module Control.Concurrent.Thread.Finalizers where+import Control.Concurrent+import Control.Exception+import Control.Monad ( void )+import GHC.IO (IO(..))+import GHC.Prim ( mkWeak# )+import GHC.Weak ( Weak(..) )+import GHC.Conc.Sync ( ThreadId(..) )++-- | A variant of 'Control.Concurrent.mkWeakThreadId' that supports+-- finalization.+--+-- Make a weak pointer to a 'ThreadId'.  It can be important to do+-- this if you want to hold a reference to a 'ThreadId' while still+-- allowing the thread to receive the @BlockedIndefinitely@ family of+-- exceptions (e.g. 'BlockedIndefinitelyOnMVar').  Holding a normal+-- 'ThreadId' reference will prevent the delivery of+-- @BlockedIndefinitely@ exceptions because the reference could be+-- used as the target of 'throwTo' at any time, which would unblock+-- the thread.+--+-- Holding a @Weak ThreadId@, on the other hand, will not prevent the+-- thread from receiving @BlockedIndefinitely@ exceptions.  It is+-- still possible to throw an exception to a @Weak ThreadId@, but the+-- caller must use @deRefWeak@ first to determine whether the thread+-- still exists.+--+mkWeakThreadIdWithFinalizer :: ThreadId -> IO () -> IO (Weak ThreadId)+mkWeakThreadIdWithFinalizer t@(ThreadId t#) (IO finalizer) = IO $ \s ->+  case mkWeak# t# t finalizer s of+    (# s1, w #) -> (# s1, Weak w #)++{-|+  A specialised version of 'mkWeakThreadIdWithFinalizer', where the 'Weak' object+  returned is simply thrown away (however the finalizer will be+  remembered by the garbage collector, and will still be run+  when the key becomes unreachable).+-}+addThreadFinalizer :: ThreadId -> IO () -> IO ()+addThreadFinalizer tid m = void $ mkWeakThreadIdWithFinalizer tid m
+ test/Spec.hs view
@@ -0,0 +1,2 @@+main :: IO ()+main = putStrLn "Test suite not yet implemented"
+ thread-utils-finalizers.cabal view
@@ -0,0 +1,52 @@+cabal-version: 1.12++-- This file has been generated from package.yaml by hpack version 0.34.4.+--+-- see: https://github.com/sol/hpack++name:           thread-utils-finalizers+version:        0.1.0.0+synopsis:       Perform finalization for threads.+description:    Please see the README on GitHub at <https://github.com/iand675/thread-finalizers#readme>+category:       Concurrency+homepage:       https://github.com/iand675/thread-utils#readme+bug-reports:    https://github.com/iand675/thread-utils/issues+author:         Ian Duncan+maintainer:     ian@iankduncan.com+copyright:      2021 Ian Duncan+license:        BSD3+license-file:   LICENSE+build-type:     Simple+extra-source-files:+    README.md+    ChangeLog.md++source-repository head+  type: git+  location: https://github.com/iand675/thread-utils++library+  exposed-modules:+      Control.Concurrent.Thread.Finalizers+  other-modules:+      Paths_thread_utils_finalizers+  hs-source-dirs:+      src+  build-depends:+      base >=4.7 && <5+    , ghc-prim+  default-language: Haskell2010++test-suite thread-finalizers-test+  type: exitcode-stdio-1.0+  main-is: Spec.hs+  other-modules:+      Paths_thread_utils_finalizers+  hs-source-dirs:+      test+  ghc-options: -threaded -rtsopts -with-rtsopts=-N+  build-depends:+      base >=4.7 && <5+    , ghc-prim+    , thread-utils-finalizers+  default-language: Haskell2010