{-# LANGUAGE Safe #-}
{- arch-tag: Thread utilities main file
Copyright (c) 2004-2011 John Goerzen <jgoerzen@complete.org>

All rights reserved.

For license and copyright information, see the file LICENSE
-}

{- |
   Module     : Control.Concurrent.Thread.Utils
   Copyright  : Copyright (C) 2004-2011 John Goerzen
   SPDX-License-Identifier: BSD-3-Clause

   Stability  : stable
   Portability: portable

This module provides various helpful utilities for dealing with threads.

Written by John Goerzen, jgoerzen\@complete.org
-}

module Control.Concurrent.Thread.Utils(-- * I\/O utilities
                        runInThread
                       )
where

import safe Control.Concurrent ( forkIO, ThreadId )

{- | Takes a IO action and a function.  The IO action will be called in a
separate thread.  When it is completed, the specified function is called with
its result.  This is a simple way of doing callbacks. -}

runInThread :: IO a -> (a -> IO b) -> IO ThreadId
runInThread :: forall a b. IO a -> (a -> IO b) -> IO ThreadId
runInThread IO a
action a -> IO b
callback = IO () -> IO ThreadId
forkIO (IO () -> IO ThreadId) -> IO () -> IO ThreadId
forall a b. (a -> b) -> a -> b
$ IO a
action IO a -> (a -> IO b) -> IO b
forall a b. IO a -> (a -> IO b) -> IO b
forall (m :: * -> *) a b. Monad m => m a -> (a -> m b) -> m b
>>= a -> IO b
callback IO b -> IO () -> IO ()
forall a b. IO a -> IO b -> IO b
forall (m :: * -> *) a b. Monad m => m a -> m b -> m b
>> () -> IO ()
forall a. a -> IO a
forall (m :: * -> *) a. Monad m => a -> m a
return ()