From 0f9b9d873bf08485208220a4ed63cb546ffe77ae Mon Sep 17 00:00:00 2001 From: Yurii Soldak Date: Wed, 20 Oct 2021 00:08:20 +0200 Subject: [PATCH] wifinina: avoid busy wait --- wifinina/tcp.go | 13 ++++++------- wifinina/timer.go | 28 ---------------------------- wifinina/wifinina.go | 19 +++++++++++-------- 3 files changed, 17 insertions(+), 43 deletions(-) delete mode 100644 wifinina/timer.go diff --git a/wifinina/tcp.go b/wifinina/tcp.go index 1f09ee0..fb747e4 100644 --- a/wifinina/tcp.go +++ b/wifinina/tcp.go @@ -87,7 +87,8 @@ func (drv *Driver) connectSocket(addr, portStr string, mode uint8) error { } // FIXME: this 4 second timeout is simply mimicking the Arduino driver - for t := newTimer(4 * time.Second); !t.Expired(); { + start := time.Now() + for time.Since(start) < 4*time.Second { connected, err := drv.IsConnected() if err != nil { return err @@ -95,7 +96,7 @@ func (drv *Driver) connectSocket(addr, portStr string, mode uint8) error { if connected { return nil } - wait(1 * time.Millisecond) + time.Sleep(1 * time.Millisecond) } return ErrConnectionTimeout @@ -272,15 +273,13 @@ func (drv *Driver) stop() error { return nil } drv.dev.StopClient(drv.sock) - for t := newTimer(5 * time.Second); !t.Expired(); { + start := time.Now() + for time.Since(start) < 5*time.Second { st, _ := drv.status() if st == TCPStateClosed { break } - // FIXME: without the time.Sleep below this blocks until TCPStateClosed, - // however with it got goroutine stack overflows; not sure if this is still - // an issue so should investigate further - //time.Sleep(1 * time.Millisecond) + time.Sleep(1 * time.Millisecond) } drv.sock = NoSocketAvail return nil diff --git a/wifinina/timer.go b/wifinina/timer.go deleted file mode 100644 index ca58e82..0000000 --- a/wifinina/timer.go +++ /dev/null @@ -1,28 +0,0 @@ -package wifinina - -import "time" - -func wait(duration time.Duration) { - newTimer(duration).WaitUntilExpired() -} - -type timer struct { - start int64 - interval int64 -} - -func newTimer(interval time.Duration) timer { - return timer{ - start: time.Now().UnixNano(), - interval: int64(interval), - } -} - -func (t timer) Expired() bool { - return time.Now().UnixNano() > (t.start + t.interval) -} - -func (t timer) WaitUntilExpired() { - for !t.Expired() { - } -} diff --git a/wifinina/wifinina.go b/wifinina/wifinina.go index 290f1b0..6875629 100644 --- a/wifinina/wifinina.go +++ b/wifinina/wifinina.go @@ -379,7 +379,7 @@ func (d *Device) CheckDataSent(sock uint8) (bool, error) { if sent > 0 { return true, nil } - wait(100 * time.Microsecond) + time.Sleep(100 * time.Microsecond) } return false, lastErr } @@ -1023,8 +1023,7 @@ func (d *Device) checkStartCmd() (bool, error) { } func (d *Device) waitForChipSelect() (err error) { - err = d.waitForChipReady() - if err == nil { + if err = d.waitForChipReady(); err == nil { err = d.spiChipSelect() } return @@ -1034,12 +1033,14 @@ func (d *Device) waitForChipReady() error { if _debug { println("waitForChipReady()\r") } - for t := newTimer(10 * time.Second); !(d.ACK.Get() == false); { - if t.Expired() { - return ErrTimeoutChipReady + start := time.Now() + for time.Since(start) < 10*time.Second { + if !d.ACK.Get() { + return nil } + time.Sleep(1 * time.Millisecond) } - return nil + return ErrTimeoutChipReady } func (d *Device) spiChipSelect() error { @@ -1047,10 +1048,12 @@ func (d *Device) spiChipSelect() error { println("spiChipSelect()\r") } d.CS.Low() - for t := newTimer(5 * time.Millisecond); !t.Expired(); { + start := time.Now() + for time.Since(start) < 5*time.Millisecond { if d.ACK.Get() { return nil } + time.Sleep(100 * time.Microsecond) } return ErrTimeoutChipSelect }