diff --git a/Network/Socket.hs b/Network/Socket.hs index 0e922256..8686e82d 100644 --- a/Network/Socket.hs +++ b/Network/Socket.hs @@ -382,6 +382,12 @@ module Network.Socket ( -- * Special constants maxListenQueue, + + -- * STM to check read and write + waitReadSocketSTM, + waitAndCancelReadSocketSTM, + waitWriteSocketSTM, + waitAndCancelWriteSocketSTM, ) where import Network.Socket.Buffer hiding ( @@ -399,6 +405,7 @@ import Network.Socket.Info import Network.Socket.Internal import Network.Socket.Name hiding (getPeerName, getSocketName) import Network.Socket.Options +import Network.Socket.STM import Network.Socket.Shutdown import Network.Socket.SockAddr import Network.Socket.Syscall hiding (accept, bind, connect) diff --git a/Network/Socket/STM.hs b/Network/Socket/STM.hs new file mode 100644 index 00000000..fb368eec --- /dev/null +++ b/Network/Socket/STM.hs @@ -0,0 +1,24 @@ +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 s = fst <$> waitAndCancelReadSocketSTM 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 s = withFdSocket s $ threadWaitReadSTM . Fd + +-- | STM action to wait until the socket is ready for writing. +waitWriteSocketSTM :: Socket -> IO (STM ()) +waitWriteSocketSTM s = fst <$> waitAndCancelWriteSocketSTM 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 s = withFdSocket s $ threadWaitWriteSTM . Fd diff --git a/network.cabal b/network.cabal index a3f1d641..b4fa6a6b 100644 --- a/network.cabal +++ b/network.cabal @@ -115,6 +115,7 @@ library Network.Socket.Name Network.Socket.Options Network.Socket.ReadShow + Network.Socket.STM Network.Socket.Shutdown Network.Socket.SockAddr Network.Socket.Syscall @@ -132,7 +133,8 @@ library base >=4.9 && <5, bytestring >=0.10, deepseq, - directory + directory, + stm if !os(windows) other-modules: