You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
stario/circle.go

144 lines
2.5 KiB
Go

4 years ago
package stario
import (
"errors"
"fmt"
"io"
3 years ago
"os"
"runtime"
4 years ago
"sync"
3 years ago
"sync/atomic"
4 years ago
"time"
)
type StarBuffer struct {
io.Reader
io.Writer
io.Closer
datas []byte
3 years ago
pStart uint64
pEnd uint64
cap uint64
isClose atomic.Value
4 years ago
rmu sync.Mutex
wmu sync.Mutex
}
3 years ago
func NewStarBuffer(cap uint64) *StarBuffer {
4 years ago
rtnBuffer := new(StarBuffer)
rtnBuffer.cap = cap
rtnBuffer.datas = make([]byte, cap)
3 years ago
rtnBuffer.isClose.Store(false)
4 years ago
return rtnBuffer
}
3 years ago
func (star *StarBuffer) Free() uint64 {
4 years ago
return star.cap - star.Len()
}
3 years ago
func (star *StarBuffer) Cap() uint64 {
4 years ago
return star.cap
}
3 years ago
func (star *StarBuffer) Len() uint64 {
if star.pEnd >= star.pStart {
return star.pEnd - star.pStart
4 years ago
}
3 years ago
return star.pEnd - star.pStart + star.cap
4 years ago
}
func (star *StarBuffer) getByte() (byte, error) {
3 years ago
if star.isClose.Load().(bool) {
4 years ago
return 0, io.EOF
}
if star.Len() == 0 {
3 years ago
return 0, os.ErrNotExist
4 years ago
}
3 years ago
nowPtr := star.pStart
nextPtr := star.pStart + 1
if nextPtr >= star.cap {
nextPtr = 0
}
data := star.datas[nowPtr]
ok := atomic.CompareAndSwapUint64(&star.pStart, nowPtr, nextPtr)
if !ok {
return 0, os.ErrInvalid
4 years ago
}
return data, nil
}
func (star *StarBuffer) putByte(data byte) error {
3 years ago
if star.isClose.Load().(bool) {
4 years ago
return io.EOF
}
3 years ago
nowPtr := star.pEnd
kariEnd := nowPtr + 1
4 years ago
if kariEnd == star.cap {
kariEnd = 0
}
3 years ago
if kariEnd == atomic.LoadUint64(&star.pStart) {
4 years ago
for {
time.Sleep(time.Microsecond)
3 years ago
runtime.Gosched()
if kariEnd != atomic.LoadUint64(&star.pStart) {
4 years ago
break
}
}
}
3 years ago
star.datas[nowPtr] = data
if ok := atomic.CompareAndSwapUint64(&star.pEnd, nowPtr, kariEnd); !ok {
return os.ErrInvalid
}
4 years ago
return nil
}
3 years ago
4 years ago
func (star *StarBuffer) Close() error {
3 years ago
star.isClose.Store(true)
4 years ago
return nil
}
func (star *StarBuffer) Read(buf []byte) (int, error) {
3 years ago
if star.isClose.Load().(bool) {
4 years ago
return 0, io.EOF
}
if buf == nil {
return 0, errors.New("buffer is nil")
}
star.rmu.Lock()
defer star.rmu.Unlock()
var sum int = 0
for i := 0; i < len(buf); i++ {
data, err := star.getByte()
if err != nil {
if err == io.EOF {
return sum, err
}
3 years ago
if err == os.ErrNotExist {
i--
continue
}
4 years ago
return sum, nil
}
buf[i] = data
sum++
}
return sum, nil
}
func (star *StarBuffer) Write(bts []byte) (int, error) {
3 years ago
if bts == nil || star.isClose.Load().(bool) {
4 years ago
return 0, io.EOF
}
star.wmu.Lock()
defer star.wmu.Unlock()
var sum = 0
for i := 0; i < len(bts); i++ {
err := star.putByte(bts[i])
if err != nil {
fmt.Println("Write bts err:", err)
return sum, err
}
sum++
}
return sum, nil
}