Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
14203b8fa3
|
|||
| 95e2899e8a | |||
| b568f51676 |
@@ -4,7 +4,10 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"os"
|
||||
"runtime"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -13,79 +16,88 @@ type StarBuffer struct {
|
||||
io.Writer
|
||||
io.Closer
|
||||
datas []byte
|
||||
pStart int
|
||||
pEnd int
|
||||
cap int
|
||||
isClose bool
|
||||
isEnd bool
|
||||
pStart uint64
|
||||
pEnd uint64
|
||||
cap uint64
|
||||
isClose atomic.Value
|
||||
rmu sync.Mutex
|
||||
wmu sync.Mutex
|
||||
}
|
||||
|
||||
func NewStarBuffer(cap int) *StarBuffer {
|
||||
func NewStarBuffer(cap uint64) *StarBuffer {
|
||||
rtnBuffer := new(StarBuffer)
|
||||
rtnBuffer.cap = cap
|
||||
rtnBuffer.datas = make([]byte, cap)
|
||||
rtnBuffer.isClose.Store(false)
|
||||
return rtnBuffer
|
||||
}
|
||||
|
||||
func (star *StarBuffer) Free() int {
|
||||
func (star *StarBuffer) Free() uint64 {
|
||||
return star.cap - star.Len()
|
||||
}
|
||||
|
||||
func (star *StarBuffer) Cap() int {
|
||||
func (star *StarBuffer) Cap() uint64 {
|
||||
return star.cap
|
||||
}
|
||||
|
||||
func (star *StarBuffer) Len() int {
|
||||
length := star.pEnd - star.pStart
|
||||
if length < 0 {
|
||||
return star.cap + length - 1
|
||||
func (star *StarBuffer) Len() uint64 {
|
||||
if star.pEnd >= star.pStart {
|
||||
return star.pEnd - star.pStart
|
||||
}
|
||||
return length
|
||||
return star.pEnd - star.pStart + star.cap
|
||||
}
|
||||
|
||||
func (star *StarBuffer) getByte() (byte, error) {
|
||||
if star.isClose || (star.isEnd && star.Len() == 0) {
|
||||
if star.isClose.Load().(bool) {
|
||||
return 0, io.EOF
|
||||
}
|
||||
if star.Len() == 0 {
|
||||
return 0, errors.New("no byte available now")
|
||||
return 0, os.ErrNotExist
|
||||
}
|
||||
data := star.datas[star.pStart]
|
||||
star.pStart++
|
||||
if star.pStart == star.cap {
|
||||
star.pStart = 0
|
||||
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
|
||||
}
|
||||
return data, nil
|
||||
}
|
||||
|
||||
func (star *StarBuffer) putByte(data byte) error {
|
||||
if star.isClose || star.isEnd {
|
||||
if star.isClose.Load().(bool) {
|
||||
return io.EOF
|
||||
}
|
||||
kariEnd := star.pEnd + 1
|
||||
nowPtr := star.pEnd
|
||||
kariEnd := nowPtr + 1
|
||||
if kariEnd == star.cap {
|
||||
kariEnd = 0
|
||||
}
|
||||
if kariEnd == star.pStart {
|
||||
if kariEnd == atomic.LoadUint64(&star.pStart) {
|
||||
for {
|
||||
time.Sleep(time.Microsecond)
|
||||
if kariEnd != star.pStart {
|
||||
runtime.Gosched()
|
||||
if kariEnd != atomic.LoadUint64(&star.pStart) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
star.datas[star.pEnd] = data
|
||||
star.pEnd = kariEnd
|
||||
star.datas[nowPtr] = data
|
||||
if ok := atomic.CompareAndSwapUint64(&star.pEnd, nowPtr, kariEnd); !ok {
|
||||
return os.ErrInvalid
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (star *StarBuffer) Close() error {
|
||||
star.isClose = true
|
||||
star.isClose.Store(true)
|
||||
return nil
|
||||
}
|
||||
func (star *StarBuffer) Read(buf []byte) (int, error) {
|
||||
if star.isClose {
|
||||
if star.isClose.Load().(bool) {
|
||||
return 0, io.EOF
|
||||
}
|
||||
if buf == nil {
|
||||
@@ -100,6 +112,10 @@ func (star *StarBuffer) Read(buf []byte) (int, error) {
|
||||
if err == io.EOF {
|
||||
return sum, err
|
||||
}
|
||||
if err == os.ErrNotExist {
|
||||
i--
|
||||
continue
|
||||
}
|
||||
return sum, nil
|
||||
}
|
||||
buf[i] = data
|
||||
@@ -109,8 +125,7 @@ func (star *StarBuffer) Read(buf []byte) (int, error) {
|
||||
}
|
||||
|
||||
func (star *StarBuffer) Write(bts []byte) (int, error) {
|
||||
if bts == nil || star.isClose {
|
||||
star.isEnd = true
|
||||
if bts == nil || star.isClose.Load().(bool) {
|
||||
return 0, io.EOF
|
||||
}
|
||||
star.wmu.Lock()
|
||||
|
||||
@@ -0,0 +1,61 @@
|
||||
package stario
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"sync/atomic"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
func Test_Circle(t *testing.T) {
|
||||
buf := NewStarBuffer(2048)
|
||||
go func() {
|
||||
for {
|
||||
//fmt.Println("write start")
|
||||
buf.Write([]byte("中华人民共和国\n"))
|
||||
//fmt.Println("write success")
|
||||
time.Sleep(time.Millisecond * 50)
|
||||
}
|
||||
}()
|
||||
cpp := ""
|
||||
go func() {
|
||||
time.Sleep(time.Second * 3)
|
||||
for {
|
||||
cache := make([]byte, 64)
|
||||
ints, err := buf.Read(cache)
|
||||
if err != nil {
|
||||
fmt.Println("read error", err)
|
||||
return
|
||||
}
|
||||
if ints != 0 {
|
||||
cpp += string(cache[:ints])
|
||||
}
|
||||
}
|
||||
}()
|
||||
time.Sleep(time.Second * 13)
|
||||
fmt.Println(cpp)
|
||||
}
|
||||
|
||||
func Test_Circle_Speed(t *testing.T) {
|
||||
buf := NewStarBuffer(1048976)
|
||||
count := uint64(0)
|
||||
for i := 1; i <= 10; i++ {
|
||||
go func() {
|
||||
for {
|
||||
buf.putByte('a')
|
||||
}
|
||||
}()
|
||||
}
|
||||
for i := 1; i <= 10; i++ {
|
||||
go func() {
|
||||
for {
|
||||
_,err:=buf.getByte()
|
||||
if err == nil {
|
||||
atomic.AddUint64(&count, 1)
|
||||
}
|
||||
}
|
||||
}()
|
||||
}
|
||||
time.Sleep(time.Second * 10)
|
||||
fmt.Println(count)
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
package stario
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"time"
|
||||
)
|
||||
|
||||
var ERR_TIMEOUT = errors.New("TIME OUT")
|
||||
|
||||
func WaitUntilTimeout(tm time.Duration, fn func(chan struct{}) error) error {
|
||||
var err error
|
||||
finished := make(chan struct{})
|
||||
imout := make(chan struct{})
|
||||
go func() {
|
||||
err = fn(imout)
|
||||
finished <- struct{}{}
|
||||
}()
|
||||
select {
|
||||
case <-finished:
|
||||
return err
|
||||
case <-time.After(tm):
|
||||
close(imout)
|
||||
return ERR_TIMEOUT
|
||||
}
|
||||
}
|
||||
|
||||
func WaitUntilFinished(fn func() error) <-chan error {
|
||||
finished := make(chan error)
|
||||
go func() {
|
||||
err := fn()
|
||||
finished <- err
|
||||
}()
|
||||
return finished
|
||||
}
|
||||
|
||||
func WaitUntilTimeoutFinished(tm time.Duration, fn func(chan struct{}) error) <-chan error {
|
||||
var err error
|
||||
finished := make(chan struct{})
|
||||
result := make(chan error)
|
||||
imout := make(chan struct{})
|
||||
go func() {
|
||||
err = fn(imout)
|
||||
finished <- struct{}{}
|
||||
}()
|
||||
go func() {
|
||||
select {
|
||||
case <-finished:
|
||||
result <- err
|
||||
case <-time.After(tm):
|
||||
close(imout)
|
||||
result <- ERR_TIMEOUT
|
||||
}
|
||||
}()
|
||||
return result
|
||||
}
|
||||
@@ -23,30 +23,103 @@ func PasswdWithMask(hint string, defaultVal string, mask string) InputMsg {
|
||||
return passwd(hint, defaultVal, mask)
|
||||
}
|
||||
|
||||
func passwd(hint string, defaultVal string, mask string) InputMsg {
|
||||
var ioBuf []byte
|
||||
func MessageBoxRaw(hint string, defaultVal string) InputMsg {
|
||||
return messageBox(hint, defaultVal)
|
||||
}
|
||||
|
||||
func messageBox(hint string, defaultVal string) InputMsg {
|
||||
var ioBuf []rune
|
||||
if hint != "" {
|
||||
fmt.Print(hint)
|
||||
}
|
||||
state, err := terminal.MakeRaw(int(os.Stdin.Fd()))
|
||||
if strings.Index(hint, "\n") >= 0 {
|
||||
hint = strings.TrimSpace(hint[strings.LastIndex(hint, "\n"):])
|
||||
}
|
||||
fd := int(os.Stdin.Fd())
|
||||
state, err := terminal.MakeRaw(fd)
|
||||
if err != nil {
|
||||
return InputMsg{"", err}
|
||||
}
|
||||
defer terminal.Restore(0, state)
|
||||
defer fmt.Println()
|
||||
defer terminal.Restore(fd, state)
|
||||
inputReader := bufio.NewReader(os.Stdin)
|
||||
for {
|
||||
b, err := inputReader.ReadByte()
|
||||
b, _, err := inputReader.ReadRune()
|
||||
if err != nil {
|
||||
return InputMsg{"", err}
|
||||
}
|
||||
if b == 0x0d {
|
||||
fmt.Println()
|
||||
return InputMsg{strings.TrimSpace(string(ioBuf)), nil}
|
||||
strValue := strings.TrimSpace(string(ioBuf))
|
||||
if len(strValue) == 0 {
|
||||
strValue = defaultVal
|
||||
}
|
||||
if mask != "" {
|
||||
return InputMsg{strValue, nil}
|
||||
}
|
||||
if b == 0x08 || b == 0x7F {
|
||||
if len(ioBuf) > 0 {
|
||||
ioBuf = ioBuf[:len(ioBuf)-1]
|
||||
}
|
||||
fmt.Print("\r")
|
||||
for i := 0; i < len(ioBuf)+2+len(hint); i++ {
|
||||
fmt.Print(" ")
|
||||
}
|
||||
} else {
|
||||
ioBuf = append(ioBuf, b)
|
||||
}
|
||||
fmt.Print("\r")
|
||||
if hint != "" {
|
||||
fmt.Print(hint)
|
||||
}
|
||||
fmt.Print(string(ioBuf))
|
||||
}
|
||||
}
|
||||
|
||||
func passwd(hint string, defaultVal string, mask string) InputMsg {
|
||||
var ioBuf []rune
|
||||
if hint != "" {
|
||||
fmt.Print(hint)
|
||||
}
|
||||
if strings.Index(hint, "\n") >= 0 {
|
||||
hint = strings.TrimSpace(hint[strings.LastIndex(hint, "\n"):])
|
||||
}
|
||||
fd := int(os.Stdin.Fd())
|
||||
state, err := terminal.MakeRaw(fd)
|
||||
if err != nil {
|
||||
return InputMsg{"", err}
|
||||
}
|
||||
defer fmt.Println()
|
||||
defer terminal.Restore(fd, state)
|
||||
inputReader := bufio.NewReader(os.Stdin)
|
||||
for {
|
||||
b, _, err := inputReader.ReadRune()
|
||||
if err != nil {
|
||||
return InputMsg{"", err}
|
||||
}
|
||||
if b == 0x0d {
|
||||
strValue := strings.TrimSpace(string(ioBuf))
|
||||
if len(strValue) == 0 {
|
||||
strValue = defaultVal
|
||||
}
|
||||
return InputMsg{strValue, nil}
|
||||
}
|
||||
if b == 0x08 || b == 0x7F {
|
||||
if len(ioBuf) > 0 {
|
||||
ioBuf = ioBuf[:len(ioBuf)-1]
|
||||
}
|
||||
fmt.Print("\r")
|
||||
for i := 0; i < len(ioBuf)+2+len(hint); i++ {
|
||||
fmt.Print(" ")
|
||||
}
|
||||
} else {
|
||||
ioBuf = append(ioBuf, b)
|
||||
}
|
||||
fmt.Print("\r")
|
||||
if hint != "" {
|
||||
fmt.Print(hint)
|
||||
}
|
||||
for i := 0; i < len(ioBuf); i++ {
|
||||
fmt.Print(mask)
|
||||
}
|
||||
ioBuf = append(ioBuf, b)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -59,7 +132,11 @@ func MessageBox(hint string, defaultVal string) InputMsg {
|
||||
if err != nil {
|
||||
return InputMsg{"", err}
|
||||
}
|
||||
return InputMsg{strings.TrimSpace(str), nil}
|
||||
str = strings.TrimSpace(str)
|
||||
if len(str) == 0 {
|
||||
str = defaultVal
|
||||
}
|
||||
return InputMsg{str, nil}
|
||||
}
|
||||
|
||||
func (im InputMsg) String() (string, error) {
|
||||
@@ -148,6 +225,7 @@ func (im InputMsg) MustFloat32() float32 {
|
||||
}
|
||||
|
||||
func YesNo(hint string, defaults bool) bool {
|
||||
for {
|
||||
res := strings.ToUpper(MessageBox(hint, "").MustString())
|
||||
if res == "" {
|
||||
return defaults
|
||||
@@ -157,38 +235,36 @@ func YesNo(hint string, defaults bool) bool {
|
||||
return true
|
||||
} else if res == "N" {
|
||||
return false
|
||||
} else {
|
||||
return defaults
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func StopUntil(hint string, trigger string, repeat bool) error {
|
||||
pressLen := len(trigger)
|
||||
pressLen := len([]rune(trigger))
|
||||
if trigger == "" {
|
||||
pressLen = 1
|
||||
} else {
|
||||
pressLen = len(trigger)
|
||||
}
|
||||
state, err := terminal.MakeRaw(int(os.Stdin.Fd()))
|
||||
fd := int(os.Stdin.Fd())
|
||||
if hint != "" {
|
||||
fmt.Print(hint)
|
||||
}
|
||||
state, err := terminal.MakeRaw(fd)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer terminal.Restore(0, state)
|
||||
defer terminal.Restore(fd, state)
|
||||
inputReader := bufio.NewReader(os.Stdin)
|
||||
//ioBuf := make([]byte, pressLen)
|
||||
if hint != "" && !repeat {
|
||||
fmt.Print(hint)
|
||||
}
|
||||
i := 0
|
||||
for {
|
||||
b, err := inputReader.ReadByte()
|
||||
b, _, err := inputReader.ReadRune()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if trigger == "" {
|
||||
break
|
||||
}
|
||||
if b == trigger[i] {
|
||||
if b == []rune(trigger)[i] {
|
||||
i++
|
||||
if i == pressLen {
|
||||
break
|
||||
@@ -197,7 +273,7 @@ func StopUntil(hint string, trigger string, repeat bool) error {
|
||||
}
|
||||
i = 0
|
||||
if hint != "" && repeat {
|
||||
fmt.Print("\n")
|
||||
fmt.Print("\r\n")
|
||||
fmt.Print(hint)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
package stario
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
"time"
|
||||
)
|
||||
|
||||
type WaitGroup struct {
|
||||
wg *sync.WaitGroup
|
||||
maxCount uint32
|
||||
allCount uint32
|
||||
}
|
||||
|
||||
func NewWaitGroup(maxCount int) WaitGroup {
|
||||
return WaitGroup{wg: &sync.WaitGroup{}, maxCount: uint32(maxCount)}
|
||||
}
|
||||
|
||||
func (swg *WaitGroup) Add(delta int) {
|
||||
var Udelta uint32
|
||||
if delta < 0 {
|
||||
Udelta = uint32(-delta - 1)
|
||||
} else {
|
||||
Udelta = uint32(delta)
|
||||
}
|
||||
for {
|
||||
allC := atomic.LoadUint32(&swg.allCount)
|
||||
if atomic.LoadUint32(&swg.maxCount) == 0 || atomic.LoadUint32(&swg.maxCount) >= allC+uint32(delta) {
|
||||
if delta < 0 {
|
||||
atomic.AddUint32(&swg.allCount, ^uint32(Udelta))
|
||||
} else {
|
||||
atomic.AddUint32(&swg.allCount, uint32(Udelta))
|
||||
}
|
||||
break
|
||||
}
|
||||
time.Sleep(time.Microsecond)
|
||||
}
|
||||
swg.wg.Add(delta)
|
||||
}
|
||||
|
||||
func (swg *WaitGroup) Done() {
|
||||
swg.Add(-1)
|
||||
}
|
||||
|
||||
func (swg *WaitGroup) Wait() {
|
||||
swg.wg.Wait()
|
||||
}
|
||||
|
||||
func (swg *WaitGroup) GetMaxWaitNum() int {
|
||||
return int(atomic.LoadUint32(&swg.maxCount))
|
||||
}
|
||||
|
||||
func (swg *WaitGroup) SetMaxWaitNum(num int) {
|
||||
atomic.AddUint32(&swg.maxCount, uint32(num))
|
||||
}
|
||||
Reference in New Issue
Block a user