From 72ce5a8715976ec5eccedab0552fc7d9233903c1 Mon Sep 17 00:00:00 2001 From: Bin Jin Date: Thu, 16 Mar 2017 00:26:40 +0800 Subject: remove STM packet queue --- src/Network/WireGuard/Internal/PacketQueue.hs | 47 +++++---------------------- 1 file changed, 9 insertions(+), 38 deletions(-) (limited to 'src/Network/WireGuard/Internal/PacketQueue.hs') diff --git a/src/Network/WireGuard/Internal/PacketQueue.hs b/src/Network/WireGuard/Internal/PacketQueue.hs index bc390f8..2840a73 100644 --- a/src/Network/WireGuard/Internal/PacketQueue.hs +++ b/src/Network/WireGuard/Internal/PacketQueue.hs @@ -1,49 +1,20 @@ -{-# LANGUAGE RecordWildCards #-} - module Network.WireGuard.Internal.PacketQueue ( PacketQueue , newPacketQueue , popPacketQueue , pushPacketQueue - , tryPushPacketQueue + , module Control.Concurrent.Chan ) where -import Control.Concurrent.STM - -data PacketQueue packet = PacketQueue - { tqueue :: TQueue packet - , allowance :: TVar Int - } - --- | Create a new PacketQueue with size limit of |maxQueuedPackets|. -newPacketQueue :: Int -> STM (PacketQueue packet) -newPacketQueue maxQueuedPackets = PacketQueue <$> newTQueue <*> newTVar maxQueuedPackets +import Control.Concurrent.Chan --- | Pop a packet out from the queue, blocks if no packet is available. -popPacketQueue :: PacketQueue packet -> STM packet -popPacketQueue PacketQueue{..} = do - packet <- readTQueue tqueue - modifyTVar' allowance (+1) - return packet +type PacketQueue packet = Chan packet --- | Push a packet into the queue. Blocks if it's full. -pushPacketQueue :: PacketQueue packet -> packet -> STM () -pushPacketQueue PacketQueue{..} packet = do - allowance' <- readTVar allowance - if allowance' <= 0 - then retry - else do - writeTQueue tqueue packet - writeTVar allowance (allowance' - 1) +newPacketQueue :: IO (PacketQueue packet) +newPacketQueue = newChan --- | Try to push a packet into the queue. Returns True if it's pushed. -tryPushPacketQueue :: PacketQueue packet -> packet -> STM Bool -tryPushPacketQueue PacketQueue{..} packet = do - allowance' <- readTVar allowance - if allowance' <= 0 - then return False - else do - writeTQueue tqueue packet - writeTVar allowance (allowance' - 1) - return True +popPacketQueue :: PacketQueue packet -> IO packet +popPacketQueue = readChan +pushPacketQueue :: PacketQueue packet -> packet -> IO () +pushPacketQueue = writeChan -- cgit v1.2.3-59-g8ed1b