module Network.Socket.STM where

import Control.Concurrent
import Control.Concurrent.STM
import Network.Socket.Types
import System.Posix.Types

-- | STM action to wait until the socket is ready for reading.
waitReadSocketSTM :: Socket -> IO (STM ())
waitReadSocketSTM :: Socket -> IO (STM ())
waitReadSocketSTM Socket
s = (STM (), IO ()) -> STM ()
forall a b. (a, b) -> a
fst ((STM (), IO ()) -> STM ()) -> IO (STM (), IO ()) -> IO (STM ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Socket -> IO (STM (), IO ())
waitAndCancelReadSocketSTM Socket
s

-- | STM action to wait until the socket is ready for reading and STM
--   action to cancel the waiting.
waitAndCancelReadSocketSTM :: Socket -> IO (STM (), IO ())
waitAndCancelReadSocketSTM :: Socket -> IO (STM (), IO ())
waitAndCancelReadSocketSTM Socket
s = Socket -> (CInt -> IO (STM (), IO ())) -> IO (STM (), IO ())
forall r. Socket -> (CInt -> IO r) -> IO r
withFdSocket Socket
s ((CInt -> IO (STM (), IO ())) -> IO (STM (), IO ()))
-> (CInt -> IO (STM (), IO ())) -> IO (STM (), IO ())
forall a b. (a -> b) -> a -> b
$ Fd -> IO (STM (), IO ())
threadWaitReadSTM (Fd -> IO (STM (), IO ()))
-> (CInt -> Fd) -> CInt -> IO (STM (), IO ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CInt -> Fd
Fd

-- | STM action to wait until the socket is ready for writing.
waitWriteSocketSTM :: Socket -> IO (STM ())
waitWriteSocketSTM :: Socket -> IO (STM ())
waitWriteSocketSTM Socket
s = (STM (), IO ()) -> STM ()
forall a b. (a, b) -> a
fst ((STM (), IO ()) -> STM ()) -> IO (STM (), IO ()) -> IO (STM ())
forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Socket -> IO (STM (), IO ())
waitAndCancelWriteSocketSTM Socket
s

-- | STM action to wait until the socket is ready for writing and STM
--   action to cancel the waiting.
waitAndCancelWriteSocketSTM :: Socket -> IO (STM (), IO ())
waitAndCancelWriteSocketSTM :: Socket -> IO (STM (), IO ())
waitAndCancelWriteSocketSTM Socket
s = Socket -> (CInt -> IO (STM (), IO ())) -> IO (STM (), IO ())
forall r. Socket -> (CInt -> IO r) -> IO r
withFdSocket Socket
s ((CInt -> IO (STM (), IO ())) -> IO (STM (), IO ()))
-> (CInt -> IO (STM (), IO ())) -> IO (STM (), IO ())
forall a b. (a -> b) -> a -> b
$ Fd -> IO (STM (), IO ())
threadWaitWriteSTM (Fd -> IO (STM (), IO ()))
-> (CInt -> Fd) -> CInt -> IO (STM (), IO ())
forall b c a. (b -> c) -> (a -> b) -> a -> c
. CInt -> Fd
Fd