machine: update ringbuffer to use runtime/volatile.Register8
This avoids the //go:volatile pragma, which will be removed soon. There were no changes to the output of the smoke tests.
Этот коммит содержится в:
родитель
c84c625585
коммит
f2c205a008
1 изменённых файлов: 12 добавлений и 11 удалений
|
@ -1,9 +1,10 @@
|
||||||
package machine
|
package machine
|
||||||
|
|
||||||
const bufferSize = 128
|
import (
|
||||||
|
"runtime/volatile"
|
||||||
|
)
|
||||||
|
|
||||||
//go:volatile
|
const bufferSize = 128
|
||||||
type volatileByte byte
|
|
||||||
|
|
||||||
// RingBuffer is ring buffer implementation inspired by post at
|
// RingBuffer is ring buffer implementation inspired by post at
|
||||||
// https://www.embeddedrelated.com/showthread/comp.arch.embedded/77084-1.php
|
// https://www.embeddedrelated.com/showthread/comp.arch.embedded/77084-1.php
|
||||||
|
@ -12,9 +13,9 @@ type volatileByte byte
|
||||||
// members of a struct are not compiled correctly by TinyGo.
|
// members of a struct are not compiled correctly by TinyGo.
|
||||||
// See https://github.com/tinygo-org/tinygo/issues/151 for details.
|
// See https://github.com/tinygo-org/tinygo/issues/151 for details.
|
||||||
type RingBuffer struct {
|
type RingBuffer struct {
|
||||||
rxbuffer [bufferSize]volatileByte
|
rxbuffer [bufferSize]volatile.Register8
|
||||||
head volatileByte
|
head volatile.Register8
|
||||||
tail volatileByte
|
tail volatile.Register8
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRingBuffer returns a new ring buffer.
|
// NewRingBuffer returns a new ring buffer.
|
||||||
|
@ -24,15 +25,15 @@ func NewRingBuffer() *RingBuffer {
|
||||||
|
|
||||||
// Used returns how many bytes in buffer have been used.
|
// Used returns how many bytes in buffer have been used.
|
||||||
func (rb *RingBuffer) Used() uint8 {
|
func (rb *RingBuffer) Used() uint8 {
|
||||||
return uint8(rb.head - rb.tail)
|
return uint8(rb.head.Get() - rb.tail.Get())
|
||||||
}
|
}
|
||||||
|
|
||||||
// Put stores a byte in the buffer. If the buffer is already
|
// Put stores a byte in the buffer. If the buffer is already
|
||||||
// full, the method will return false.
|
// full, the method will return false.
|
||||||
func (rb *RingBuffer) Put(val byte) bool {
|
func (rb *RingBuffer) Put(val byte) bool {
|
||||||
if rb.Used() != bufferSize {
|
if rb.Used() != bufferSize {
|
||||||
rb.head++
|
rb.head.Set(rb.head.Get() + 1)
|
||||||
rb.rxbuffer[rb.head%bufferSize] = volatileByte(val)
|
rb.rxbuffer[rb.head.Get()%bufferSize].Set(val)
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return false
|
return false
|
||||||
|
@ -42,8 +43,8 @@ func (rb *RingBuffer) Put(val byte) bool {
|
||||||
// the method will return a false as the second value.
|
// the method will return a false as the second value.
|
||||||
func (rb *RingBuffer) Get() (byte, bool) {
|
func (rb *RingBuffer) Get() (byte, bool) {
|
||||||
if rb.Used() != 0 {
|
if rb.Used() != 0 {
|
||||||
rb.tail++
|
rb.tail.Set(rb.tail.Get() + 1)
|
||||||
return byte(rb.rxbuffer[rb.tail%bufferSize]), true
|
return rb.rxbuffer[rb.tail.Get()%bufferSize].Get(), true
|
||||||
}
|
}
|
||||||
return 0, false
|
return 0, false
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче