diff options
author | Bin Jin <bjin@ctrl-d.org> | 2017-03-16 00:26:40 +0800 |
---|---|---|
committer | Bin Jin <bjin@ctrl-d.org> | 2017-03-16 00:26:40 +0800 |
commit | 72ce5a8715976ec5eccedab0552fc7d9233903c1 (patch) | |
tree | b88f568383bdd1c20b0e3ed7a1df0da12e939596 /src/Network/WireGuard/Internal | |
parent | Disable multi-threading for now (diff) | |
download | wireguard-hs-72ce5a8715976ec5eccedab0552fc7d9233903c1.tar.xz wireguard-hs-72ce5a8715976ec5eccedab0552fc7d9233903c1.zip |
remove STM packet queue
Diffstat (limited to 'src/Network/WireGuard/Internal')
-rw-r--r-- | src/Network/WireGuard/Internal/Constant.hs | 3 | ||||
-rw-r--r-- | src/Network/WireGuard/Internal/PacketQueue.hs | 47 |
2 files changed, 9 insertions, 41 deletions
diff --git a/src/Network/WireGuard/Internal/Constant.hs b/src/Network/WireGuard/Internal/Constant.hs index c615a0f..73dbd42 100644 --- a/src/Network/WireGuard/Internal/Constant.hs +++ b/src/Network/WireGuard/Internal/Constant.hs @@ -21,9 +21,6 @@ mac2Length = 16 maxQueuedUdpPackets :: Int maxQueuedUdpPackets = 4096 -maxQueuedTunPackets :: Int -maxQueuedTunPackets = 4096 - udpReadBufferLength :: Int udpReadBufferLength = 4096 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 |