mynewt-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ccoll...@apache.org
Subject [mynewt-newtmgr] 01/03: nmxact - protect discoverer from multiple Abort()s
Date Thu, 07 Sep 2017 01:52:32 GMT
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

commit 16694db6aaaa0f3b4f5d329cbe72a4f23bc57ccb
Author: Christopher Collins <ccollins@apache.org>
AuthorDate: Wed Sep 6 16:06:38 2017 -0700

    nmxact - protect discoverer from multiple Abort()s
---
 nmxact/nmble/discover.go | 52 ++++++++++++++++++++++++++++++++++++++----------
 1 file changed, 42 insertions(+), 10 deletions(-)

diff --git a/nmxact/nmble/discover.go b/nmxact/nmble/discover.go
index 4431497..e5132ae 100644
--- a/nmxact/nmble/discover.go
+++ b/nmxact/nmble/discover.go
@@ -20,6 +20,7 @@
 package nmble
 
 import (
+	"sync"
 	"time"
 
 	log "github.com/Sirupsen/logrus"
@@ -41,6 +42,7 @@ type Discoverer struct {
 	params    DiscovererParams
 	abortChan chan struct{}
 	blocker   nmxutil.Blocker
+	mtx       sync.Mutex
 }
 
 func NewDiscoverer(params DiscovererParams) *Discoverer {
@@ -66,10 +68,34 @@ func (d *Discoverer) scanCancel() error {
 }
 
 func (d *Discoverer) Start(advRptCb BleAdvRptFn) error {
-	if d.abortChan != nil {
-		return nmxutil.NewAlreadyError("Attempt to start BLE discoverer twice")
+	// Sets up the abort channel to allow discovery to be cancelled.  Ensures
+	// only one discovery procedure is active at a time.
+	initiate := func() error {
+		d.mtx.Lock()
+		defer d.mtx.Unlock()
+
+		if d.abortChan != nil {
+			return nmxutil.NewAlreadyError(
+				"Attempt to start BLE discoverer twice")
+		}
+
+		d.abortChan = make(chan struct{})
+
+		return nil
+	}
+
+	finalize := func() {
+		d.mtx.Lock()
+		defer d.mtx.Unlock()
+
+		d.abortChan = nil
 	}
 
+	if err := initiate(); err != nil {
+		return err
+	}
+	defer finalize()
+
 	// Scanning requires dedicated master privileges.
 	if err := AcquireMaster(d.params.Bx, MASTER_PRIO_SCAN, d); err != nil {
 		return err
@@ -90,10 +116,6 @@ func (d *Discoverer) Start(advRptCb BleAdvRptFn) error {
 	}
 	defer d.params.Bx.RemoveListener(bl)
 
-	// Set up the abort channel to allow discovery to be cancelled.
-	d.abortChan = make(chan struct{}, 1)
-	defer func() { d.abortChan = nil }()
-
 	// Ensure subsequent calls to Stop() block until discovery is fully
 	// stopped.
 	d.blocker.Start()
@@ -119,12 +141,22 @@ func (d *Discoverer) Start(advRptCb BleAdvRptFn) error {
 
 // Ensures the discoverer is stopped.  Errors can typically be ignored.
 func (d *Discoverer) Stop() error {
-	ch := d.abortChan
+	initiate := func() error {
+		d.mtx.Lock()
+		defer d.mtx.Unlock()
+
+		if d.abortChan == nil {
+			return nmxutil.NewAlreadyError(
+				"Attempt to stop inactive discoverer")
+		}
 
-	if ch == nil {
-		return nmxutil.NewAlreadyError("Attempt to stop inactive discoverer")
+		close(d.abortChan)
+		return nil
+	}
+
+	if err := initiate(); err != nil {
+		return err
 	}
-	close(ch)
 
 	// Don't return until discovery is fully stopped.
 	d.blocker.Wait(nmxutil.DURATION_FOREVER, nil)

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

Mime
View raw message