From ca1d5a7205a7e222c1f961e300a12c2e7b6ee72e Mon Sep 17 00:00:00 2001 From: Joel Stanley Date: Sun, 10 Jul 2016 17:28:10 -0500 Subject: [PATCH] minor tweaks to withPermission --- .../GRPC/LowLevel/CompletionQueue/Internal.hs | 23 +++++++------------ 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/src/Network/GRPC/LowLevel/CompletionQueue/Internal.hs b/src/Network/GRPC/LowLevel/CompletionQueue/Internal.hs index af87452..6aa29a6 100644 --- a/src/Network/GRPC/LowLevel/CompletionQueue/Internal.hs +++ b/src/Network/GRPC/LowLevel/CompletionQueue/Internal.hs @@ -15,8 +15,6 @@ import qualified Network.GRPC.Unsafe as C import qualified Network.GRPC.Unsafe.Constants as C import qualified Network.GRPC.Unsafe.Time as C -import Debug.Trace - -- NOTE: the concurrency requirements for a CompletionQueue are a little -- complicated. There are two read operations: next and pluck. We can either -- call next on a CQ or call pluck up to 'maxCompletionQueuePluckers' times @@ -82,22 +80,17 @@ withPermission :: CQOpType -> CompletionQueue -> IO (Either GRPCIOError a) -> IO (Either GRPCIOError a) -withPermission op cq act = bracket acquire release doOp +withPermission op cq act = bracket acquire release $ \gotResource -> + if gotResource then act else return (Left GRPCIOShutdown) where acquire = atomically $ do isShuttingDown <- readTVar (shuttingDown cq) - if isShuttingDown - then return False - else do currCount <- readTVar (getCount op cq) - if currCount < getLimit op - then do - writeTVar (getCount op cq) (currCount+1) - return True - else retry - - doOp gotResource = - if gotResource then act else return (Left GRPCIOShutdown) - + unless isShuttingDown $ do + currCount <- readTVar (getCount op cq) + if currCount < getLimit op + then writeTVar (getCount op cq) (currCount + 1) + else retry + return (not isShuttingDown) release gotResource = when gotResource $ atomically $ modifyTVar' (getCount op cq) (subtract 1)