Skip to content

Commit

Permalink
Merge pull request haskell#586 from kazu-yamamoto/socket-stm
Browse files Browse the repository at this point in the history
adding the STM module.
  • Loading branch information
kazu-yamamoto authored Aug 28, 2024
2 parents b6ffeff + 3da2c21 commit 15e2454
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 1 deletion.
7 changes: 7 additions & 0 deletions Network/Socket.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -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)
Expand Down
24 changes: 24 additions & 0 deletions Network/Socket/STM.hs
Original file line number Diff line number Diff line change
@@ -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
4 changes: 3 additions & 1 deletion network.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -132,7 +133,8 @@ library
base >=4.9 && <5,
bytestring >=0.10,
deepseq,
directory
directory,
stm

if !os(windows)
other-modules:
Expand Down

0 comments on commit 15e2454

Please sign in to comment.