minor tweaks to withPermission

This commit is contained in:
Joel Stanley 2016-07-10 17:28:10 -05:00
parent a0a14dd7d8
commit ca1d5a7205

View file

@ -15,8 +15,6 @@ import qualified Network.GRPC.Unsafe as C
import qualified Network.GRPC.Unsafe.Constants as C import qualified Network.GRPC.Unsafe.Constants as C
import qualified Network.GRPC.Unsafe.Time as C import qualified Network.GRPC.Unsafe.Time as C
import Debug.Trace
-- NOTE: the concurrency requirements for a CompletionQueue are a little -- NOTE: the concurrency requirements for a CompletionQueue are a little
-- complicated. There are two read operations: next and pluck. We can either -- complicated. There are two read operations: next and pluck. We can either
-- call next on a CQ or call pluck up to 'maxCompletionQueuePluckers' times -- call next on a CQ or call pluck up to 'maxCompletionQueuePluckers' times
@ -82,22 +80,17 @@ withPermission :: CQOpType
-> CompletionQueue -> CompletionQueue
-> IO (Either GRPCIOError a) -> IO (Either GRPCIOError a)
-> 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 where
acquire = atomically $ do acquire = atomically $ do
isShuttingDown <- readTVar (shuttingDown cq) isShuttingDown <- readTVar (shuttingDown cq)
if isShuttingDown unless isShuttingDown $ do
then return False currCount <- readTVar (getCount op cq)
else do currCount <- readTVar (getCount op cq)
if currCount < getLimit op if currCount < getLimit op
then do then writeTVar (getCount op cq) (currCount + 1)
writeTVar (getCount op cq) (currCount+1)
return True
else retry else retry
return (not isShuttingDown)
doOp gotResource =
if gotResource then act else return (Left GRPCIOShutdown)
release gotResource = when gotResource $ release gotResource = when gotResource $
atomically $ modifyTVar' (getCount op cq) (subtract 1) atomically $ modifyTVar' (getCount op cq) (subtract 1)