You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by cc...@apache.org on 2017/07/19 00:18:55 UTC

[mynewt-newtmgr] branch master updated: nmxact: Don't modify sesn state on aleady-open err

This is an automated email from the ASF dual-hosted git repository.

ccollins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/mynewt-newtmgr.git


The following commit(s) were added to refs/heads/master by this push:
     new abdaac0  nmxact: Don't modify sesn state on aleady-open err
abdaac0 is described below

commit abdaac0248ce7fdd91917db8ac27450b9ee64158
Author: Christopher Collins <cc...@apache.org>
AuthorDate: Tue Jul 18 17:16:14 2017 -0700

    nmxact: Don't modify sesn state on aleady-open err
---
 nmxact/nmble/ble_oic_sesn.go   | 17 +++++++++--------
 nmxact/nmble/ble_plain_sesn.go | 17 +++++++++--------
 nmxact/nmxutil/block.go        | 41 +++++++++++++++++++++++++++++++++++++++++
 3 files changed, 59 insertions(+), 16 deletions(-)

diff --git a/nmxact/nmble/ble_oic_sesn.go b/nmxact/nmble/ble_oic_sesn.go
index bbdafb1..19b29a9 100644
--- a/nmxact/nmble/ble_oic_sesn.go
+++ b/nmxact/nmble/ble_oic_sesn.go
@@ -22,8 +22,7 @@ type BleOicSesn struct {
 	closeTimeout time.Duration
 	onCloseCb    sesn.OnCloseFn
 	wg           sync.WaitGroup
-
-	closeChan chan struct{}
+	closeBlocker nmxutil.Blocker
 }
 
 func NewBleOicSesn(bx *BleXport, cfg sesn.SesnCfg) *BleOicSesn {
@@ -62,17 +61,19 @@ func (bos *BleOicSesn) AbortRx(seq uint8) error {
 }
 
 func (bos *BleOicSesn) Open() error {
-	// This channel gets closed when the session closes.
-	bos.closeChan = make(chan struct{})
+	// Ensure subsequent calls to Close() block.
+	bos.closeBlocker.Block()
 
 	if err := bos.bf.Start(); err != nil {
-		close(bos.closeChan)
+		if !nmxutil.IsSesnAlreadyOpen(err) {
+			bos.closeBlocker.Unblock()
+		}
 		return err
 	}
 
 	d, err := omp.NewDispatcher(true, 3)
 	if err != nil {
-		close(bos.closeChan)
+		bos.closeBlocker.Unblock()
 		return err
 	}
 	bos.d = d
@@ -81,7 +82,7 @@ func (bos *BleOicSesn) Open() error {
 	bos.wg.Add(1)
 	go func() {
 		// If the session is being closed, unblock the close() call.
-		defer close(bos.closeChan)
+		defer bos.closeBlocker.Unblock()
 
 		// Block until disconnect.
 		<-bos.bf.DisconnectChan()
@@ -127,7 +128,7 @@ func (bos *BleOicSesn) Close() error {
 	}
 
 	// Block until close completes.
-	<-bos.closeChan
+	bos.closeBlocker.Wait()
 	return nil
 }
 
diff --git a/nmxact/nmble/ble_plain_sesn.go b/nmxact/nmble/ble_plain_sesn.go
index 9268c3d..e85ffd5 100644
--- a/nmxact/nmble/ble_plain_sesn.go
+++ b/nmxact/nmble/ble_plain_sesn.go
@@ -18,8 +18,7 @@ type BlePlainSesn struct {
 	closeTimeout time.Duration
 	onCloseCb    sesn.OnCloseFn
 	wg           sync.WaitGroup
-
-	closeChan chan struct{}
+	closeBlocker nmxutil.Blocker
 }
 
 func NewBlePlainSesn(bx *BleXport, cfg sesn.SesnCfg) *BlePlainSesn {
@@ -53,11 +52,13 @@ func (bps *BlePlainSesn) AbortRx(seq uint8) error {
 }
 
 func (bps *BlePlainSesn) Open() error {
-	// This channel gets closed when the session closes.
-	bps.closeChan = make(chan struct{})
+	// Ensure subsequent calls to Close() block.
+	bos.closeBlocker.Block()
 
-	if err := bps.bf.Start(); err != nil {
-		close(bps.closeChan)
+	if err := bos.bf.Start(); err != nil {
+		if !nmxutil.IsSesnAlreadyOpen(err) {
+			bos.closeBlocker.Unblock()
+		}
 		return err
 	}
 
@@ -67,7 +68,7 @@ func (bps *BlePlainSesn) Open() error {
 	bps.wg.Add(1)
 	go func() {
 		// If the session is being closed, unblock the close() call.
-		defer close(bps.closeChan)
+		defer bos.closeBlocker.Unblock()
 
 		// Block until disconnect.
 		<-bps.bf.DisconnectChan()
@@ -113,7 +114,7 @@ func (bps *BlePlainSesn) Close() error {
 	}
 
 	// Block until close completes.
-	<-bps.closeChan
+	bos.closeBlocker.Wait()
 	return nil
 }
 
diff --git a/nmxact/nmxutil/block.go b/nmxact/nmxutil/block.go
new file mode 100644
index 0000000..7a0e2e9
--- /dev/null
+++ b/nmxact/nmxutil/block.go
@@ -0,0 +1,41 @@
+package nmxutil
+
+import (
+	"sync"
+)
+
+// Blocks a variable number of waiters until Unblock() is called.  Subsequent
+// waiters are unblocked until the next call to Block().
+type Blocker struct {
+	ch  chan struct{}
+	mtx sync.Mutex
+}
+
+func (b *Blocker) Wait() {
+	b.mtx.Lock()
+	ch := b.ch
+	b.mtx.Unlock()
+
+	if ch != nil {
+		<-ch
+	}
+}
+
+func (b *Blocker) Block() {
+	b.mtx.Lock()
+	defer b.mtx.Unlock()
+
+	if b.ch == nil {
+		b.ch = make(chan struct{})
+	}
+}
+
+func (b *Blocker) Unblock() {
+	b.mtx.Lock()
+	defer b.mtx.Unlock()
+
+	if b.ch != nil {
+		close(b.ch)
+		b.ch = nil
+	}
+}

-- 
To stop receiving notification emails like this one, please contact
['"commits@mynewt.apache.org" <co...@mynewt.apache.org>'].