Skip to content

Commit

Permalink
incCounter and decCounter are used in forkManaged
Browse files Browse the repository at this point in the history
  • Loading branch information
kazu-yamamoto committed Jul 7, 2024
1 parent d820eed commit fde1a50
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 12 deletions.
11 changes: 3 additions & 8 deletions Network/HTTP2/Client/Run.hs
Original file line number Diff line number Diff line change
Expand Up @@ -234,19 +234,14 @@ sendStreaming Context{..} strm strmbdy = do
tbq <- newTBQueueIO 10 -- fixme: hard coding: 10
let label = "H2 streaming supporter for stream " ++ show (streamNumber strm)
forkManagedUnmask threadManager label $ \unmask -> do
decrementedCounter <- newIORef False
let decCounterOnce = do
alreadyDecremented <- atomicModifyIORef decrementedCounter $ \b -> (True, b)
unless alreadyDecremented $ decCounter threadManager
let iface =
OutBodyIface
{ outBodyUnmask = unmask
, outBodyPush = \b -> atomically $ writeTBQueue tbq (StreamingBuilder b Nothing)
, outBodyPushFinal = \b -> atomically $ writeTBQueue tbq (StreamingBuilder b (Just decCounterOnce))
, outBodyPush = \b -> atomically $ writeTBQueue tbq $ StreamingBuilder b Nothing
, outBodyPushFinal = \b -> atomically $ writeTBQueue tbq $ StreamingBuilder b Nothing
, outBodyFlush = atomically $ writeTBQueue tbq StreamingFlush
}
finished = atomically $ writeTBQueue tbq $ StreamingFinished decCounterOnce
incCounter threadManager
finished = atomically $ writeTBQueue tbq $ StreamingFinished (return ())
strmbdy iface `finally` finished
return tbq

Expand Down
4 changes: 2 additions & 2 deletions Network/HTTP2/H2/Manager.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ module Network.HTTP2.H2.Manager (
timeoutKillThread,
timeoutClose,
KilledByHttp2ThreadManager (..),
incCounter,
decCounter,
waitCounter0,
) where

Expand Down Expand Up @@ -85,10 +83,12 @@ forkManagedUnmask mgr label io =
void $ mask_ $ forkIOWithUnmask $ \unmask -> E.handleSyncOrAsync handler $ do
labelMe label
addMyId mgr
incCounter mgr
-- We catch the exception and do not rethrow it: we don't want the
-- exception printed to stderr.
io unmask `catch` \(_e :: SomeException) -> return ()
deleteMyId mgr
decCounter mgr
where
handler (E.SomeException _) = return ()

Expand Down
3 changes: 1 addition & 2 deletions Network/HTTP2/Server/Worker.hs
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,7 @@ sendStreaming Context{..} strm th strmbdy = do
T.resume th
, outBodyFlush = atomically $ writeTBQueue tbq StreamingFlush
}
finished = atomically $ writeTBQueue tbq $ StreamingFinished $ decCounter threadManager
incCounter threadManager
finished = atomically $ writeTBQueue tbq $ StreamingFinished (return ())
strmbdy iface `E.finally` finished
return tbq

Expand Down

0 comments on commit fde1a50

Please sign in to comment.