wifinina: avoid busy wait

This commit is contained in:
Yurii Soldak
2021-10-20 00:08:20 +02:00
committed by Ron Evans
parent 114e24870e
commit 0f9b9d873b
3 changed files with 17 additions and 43 deletions
+6 -7
View File
@@ -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 // 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() connected, err := drv.IsConnected()
if err != nil { if err != nil {
return err return err
@@ -95,7 +96,7 @@ func (drv *Driver) connectSocket(addr, portStr string, mode uint8) error {
if connected { if connected {
return nil return nil
} }
wait(1 * time.Millisecond) time.Sleep(1 * time.Millisecond)
} }
return ErrConnectionTimeout return ErrConnectionTimeout
@@ -272,15 +273,13 @@ func (drv *Driver) stop() error {
return nil return nil
} }
drv.dev.StopClient(drv.sock) 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() st, _ := drv.status()
if st == TCPStateClosed { if st == TCPStateClosed {
break break
} }
// FIXME: without the time.Sleep below this blocks until TCPStateClosed, time.Sleep(1 * time.Millisecond)
// however with it got goroutine stack overflows; not sure if this is still
// an issue so should investigate further
//time.Sleep(1 * time.Millisecond)
} }
drv.sock = NoSocketAvail drv.sock = NoSocketAvail
return nil return nil
-28
View File
@@ -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() {
}
}
+11 -8
View File
@@ -379,7 +379,7 @@ func (d *Device) CheckDataSent(sock uint8) (bool, error) {
if sent > 0 { if sent > 0 {
return true, nil return true, nil
} }
wait(100 * time.Microsecond) time.Sleep(100 * time.Microsecond)
} }
return false, lastErr return false, lastErr
} }
@@ -1023,8 +1023,7 @@ func (d *Device) checkStartCmd() (bool, error) {
} }
func (d *Device) waitForChipSelect() (err error) { func (d *Device) waitForChipSelect() (err error) {
err = d.waitForChipReady() if err = d.waitForChipReady(); err == nil {
if err == nil {
err = d.spiChipSelect() err = d.spiChipSelect()
} }
return return
@@ -1034,12 +1033,14 @@ func (d *Device) waitForChipReady() error {
if _debug { if _debug {
println("waitForChipReady()\r") println("waitForChipReady()\r")
} }
for t := newTimer(10 * time.Second); !(d.ACK.Get() == false); { start := time.Now()
if t.Expired() { for time.Since(start) < 10*time.Second {
return ErrTimeoutChipReady if !d.ACK.Get() {
return nil
} }
time.Sleep(1 * time.Millisecond)
} }
return nil return ErrTimeoutChipReady
} }
func (d *Device) spiChipSelect() error { func (d *Device) spiChipSelect() error {
@@ -1047,10 +1048,12 @@ func (d *Device) spiChipSelect() error {
println("spiChipSelect()\r") println("spiChipSelect()\r")
} }
d.CS.Low() 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() { if d.ACK.Get() {
return nil return nil
} }
time.Sleep(100 * time.Microsecond)
} }
return ErrTimeoutChipSelect return ErrTimeoutChipSelect
} }