Development discussion of WireGuard
 help / color / mirror / Atom feed
From: Simon Ruderich <simon@ruderich.org>
To: wireguard@lists.zx2c4.com
Subject: [PATCH 03/12] receive, send: use AtomicBool for dropped in QueueInboundElement, QueueOutboundElement
Date: Mon,  1 Jan 2018 11:52:53 +0100	[thread overview]
Message-ID: <19b07517ede8926908b1d827d4e263342e76c292.1514803815.git.simon@ruderich.org> (raw)
In-Reply-To: <cover.1514803815.git.simon@ruderich.org>
In-Reply-To: <cover.1514803815.git.simon@ruderich.org>

Also remove explicit initialization which defaults to false anyway.

This removes code duplication with AtomicBool.
---
 src/receive.go | 7 +++----
 src/send.go    | 9 ++++-----
 2 files changed, 7 insertions(+), 9 deletions(-)

diff --git a/src/receive.go b/src/receive.go
index dbd2813..2c4f191 100644
--- a/src/receive.go
+++ b/src/receive.go
@@ -20,7 +20,7 @@ type QueueHandshakeElement struct {
 }
 
 type QueueInboundElement struct {
-	dropped  int32
+	dropped  AtomicBool
 	mutex    sync.Mutex
 	buffer   *[MaxMessageSize]byte
 	packet   []byte
@@ -30,11 +30,11 @@ type QueueInboundElement struct {
 }
 
 func (elem *QueueInboundElement) Drop() {
-	atomic.StoreInt32(&elem.dropped, AtomicTrue)
+	elem.dropped.Set(true)
 }
 
 func (elem *QueueInboundElement) IsDropped() bool {
-	return atomic.LoadInt32(&elem.dropped) == AtomicTrue
+	return elem.dropped.Get()
 }
 
 func (device *Device) addToInboundQueue(
@@ -177,7 +177,6 @@ func (device *Device) RoutineReceiveIncoming(IP int, bind Bind) {
 				packet:   packet,
 				buffer:   buffer,
 				keyPair:  keyPair,
-				dropped:  AtomicFalse,
 				endpoint: endpoint,
 			}
 			elem.mutex.Lock()
diff --git a/src/send.go b/src/send.go
index 9537f5e..163b75f 100644
--- a/src/send.go
+++ b/src/send.go
@@ -36,7 +36,7 @@ import (
  */
 
 type QueueOutboundElement struct {
-	dropped int32
+	dropped AtomicBool
 	mutex   sync.Mutex
 	buffer  *[MaxMessageSize]byte // slice holding the packet data
 	packet  []byte                // slice of "buffer" (always!)
@@ -58,17 +58,16 @@ func (peer *Peer) FlushNonceQueue() {
 
 func (device *Device) NewOutboundElement() *QueueOutboundElement {
 	return &QueueOutboundElement{
-		dropped: AtomicFalse,
 		buffer:  device.pool.messageBuffers.Get().(*[MaxMessageSize]byte),
 	}
 }
 
 func (elem *QueueOutboundElement) Drop() {
-	atomic.StoreInt32(&elem.dropped, AtomicTrue)
+	elem.dropped.Set(true)
 }
 
 func (elem *QueueOutboundElement) IsDropped() bool {
-	return atomic.LoadInt32(&elem.dropped) == AtomicTrue
+	return elem.dropped.Get()
 }
 
 func addToOutboundQueue(
@@ -227,7 +226,7 @@ func (peer *Peer) RoutineNonce() {
 			elem.peer = peer
 			elem.nonce = atomic.AddUint64(&keyPair.sendNonce, 1) - 1
 			elem.keyPair = keyPair
-			elem.dropped = AtomicFalse
+			elem.dropped.Set(false)
 			elem.mutex.Lock()
 
 			// add to parallel and sequential queue
-- 
2.15.1

  parent reply	other threads:[~2018-01-01 10:52 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-01-01 10:52 [PATCH 00/12] Misc patches Simon Ruderich
2018-01-01 10:52 ` [PATCH 01/12] Fix typos in comments Simon Ruderich
2018-01-01 10:52 ` [PATCH 02/12] tun_linux: use getDummySock() Simon Ruderich
2018-01-01 10:52 ` Simon Ruderich [this message]
2018-01-01 10:52 ` [PATCH 04/12] device: move removePeerUnsafe() into Device Simon Ruderich
2018-01-01 10:52 ` [PATCH 05/12] index: NewIndex(): don't use separate read-lock to check if index is present Simon Ruderich
2018-01-01 10:52 ` [PATCH 06/12] device: use UnderLoadAfterTime constant Simon Ruderich
2018-01-01 10:52 ` [PATCH 07/12] peer: NewPeer(): add missing device.mutex.Unlock() in error paths Simon Ruderich
2018-01-01 10:52 ` [PATCH 08/12] conn_linux: move comment to make its meaning more obvious Simon Ruderich
2018-01-01 10:52 ` [PATCH 09/12] ratelimiter: Allow(): don't use separate read-lock to check if ip is present Simon Ruderich
2018-01-01 10:53 ` [PATCH 10/12] receive, send: specialize addTo*Queue() functions Simon Ruderich
2018-01-01 10:53 ` [PATCH 11/12] noise_protocol: mixHash(): remove unnecessary Reset() Simon Ruderich
2018-01-01 10:53 ` [PATCH 12/12] timers: log error if handshake sending fails Simon Ruderich

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=19b07517ede8926908b1d827d4e263342e76c292.1514803815.git.simon@ruderich.org \
    --to=simon@ruderich.org \
    --cc=wireguard@lists.zx2c4.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).