Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
14203b8fa3
|
|||
| 95e2899e8a | |||
| b568f51676 |
@@ -4,7 +4,10 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
"os"
|
||||||
|
"runtime"
|
||||||
"sync"
|
"sync"
|
||||||
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -13,79 +16,88 @@ type StarBuffer struct {
|
|||||||
io.Writer
|
io.Writer
|
||||||
io.Closer
|
io.Closer
|
||||||
datas []byte
|
datas []byte
|
||||||
pStart int
|
pStart uint64
|
||||||
pEnd int
|
pEnd uint64
|
||||||
cap int
|
cap uint64
|
||||||
isClose bool
|
isClose atomic.Value
|
||||||
isEnd bool
|
|
||||||
rmu sync.Mutex
|
rmu sync.Mutex
|
||||||
wmu sync.Mutex
|
wmu sync.Mutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewStarBuffer(cap int) *StarBuffer {
|
func NewStarBuffer(cap uint64) *StarBuffer {
|
||||||
rtnBuffer := new(StarBuffer)
|
rtnBuffer := new(StarBuffer)
|
||||||
rtnBuffer.cap = cap
|
rtnBuffer.cap = cap
|
||||||
rtnBuffer.datas = make([]byte, cap)
|
rtnBuffer.datas = make([]byte, cap)
|
||||||
|
rtnBuffer.isClose.Store(false)
|
||||||
return rtnBuffer
|
return rtnBuffer
|
||||||
}
|
}
|
||||||
|
|
||||||
func (star *StarBuffer) Free() int {
|
func (star *StarBuffer) Free() uint64 {
|
||||||
return star.cap - star.Len()
|
return star.cap - star.Len()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (star *StarBuffer) Cap() int {
|
func (star *StarBuffer) Cap() uint64 {
|
||||||
return star.cap
|
return star.cap
|
||||||
}
|
}
|
||||||
|
|
||||||
func (star *StarBuffer) Len() int {
|
func (star *StarBuffer) Len() uint64 {
|
||||||
length := star.pEnd - star.pStart
|
if star.pEnd >= star.pStart {
|
||||||
if length < 0 {
|
return star.pEnd - star.pStart
|
||||||
return star.cap + length - 1
|
|
||||||
}
|
}
|
||||||
return length
|
return star.pEnd - star.pStart + star.cap
|
||||||
}
|
}
|
||||||
|
|
||||||
func (star *StarBuffer) getByte() (byte, error) {
|
func (star *StarBuffer) getByte() (byte, error) {
|
||||||
if star.isClose || (star.isEnd && star.Len() == 0) {
|
if star.isClose.Load().(bool) {
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
if star.Len() == 0 {
|
if star.Len() == 0 {
|
||||||
return 0, errors.New("no byte available now")
|
return 0, os.ErrNotExist
|
||||||
}
|
}
|
||||||
data := star.datas[star.pStart]
|
nowPtr := star.pStart
|
||||||
star.pStart++
|
nextPtr := star.pStart + 1
|
||||||
if star.pStart == star.cap {
|
if nextPtr >= star.cap {
|
||||||
star.pStart = 0
|
nextPtr = 0
|
||||||
|
}
|
||||||
|
data := star.datas[nowPtr]
|
||||||
|
ok := atomic.CompareAndSwapUint64(&star.pStart, nowPtr, nextPtr)
|
||||||
|
if !ok {
|
||||||
|
return 0, os.ErrInvalid
|
||||||
}
|
}
|
||||||
return data, nil
|
return data, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (star *StarBuffer) putByte(data byte) error {
|
func (star *StarBuffer) putByte(data byte) error {
|
||||||
if star.isClose || star.isEnd {
|
if star.isClose.Load().(bool) {
|
||||||
return io.EOF
|
return io.EOF
|
||||||
}
|
}
|
||||||
kariEnd := star.pEnd + 1
|
nowPtr := star.pEnd
|
||||||
|
kariEnd := nowPtr + 1
|
||||||
if kariEnd == star.cap {
|
if kariEnd == star.cap {
|
||||||
kariEnd = 0
|
kariEnd = 0
|
||||||
}
|
}
|
||||||
if kariEnd == star.pStart {
|
if kariEnd == atomic.LoadUint64(&star.pStart) {
|
||||||
for {
|
for {
|
||||||
time.Sleep(time.Microsecond)
|
time.Sleep(time.Microsecond)
|
||||||
if kariEnd != star.pStart {
|
runtime.Gosched()
|
||||||
|
if kariEnd != atomic.LoadUint64(&star.pStart) {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
star.datas[star.pEnd] = data
|
star.datas[nowPtr] = data
|
||||||
star.pEnd = kariEnd
|
if ok := atomic.CompareAndSwapUint64(&star.pEnd, nowPtr, kariEnd); !ok {
|
||||||
|
return os.ErrInvalid
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (star *StarBuffer) Close() error {
|
func (star *StarBuffer) Close() error {
|
||||||
star.isClose = true
|
star.isClose.Store(true)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
func (star *StarBuffer) Read(buf []byte) (int, error) {
|
func (star *StarBuffer) Read(buf []byte) (int, error) {
|
||||||
if star.isClose {
|
if star.isClose.Load().(bool) {
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
if buf == nil {
|
if buf == nil {
|
||||||
@@ -100,6 +112,10 @@ func (star *StarBuffer) Read(buf []byte) (int, error) {
|
|||||||
if err == io.EOF {
|
if err == io.EOF {
|
||||||
return sum, err
|
return sum, err
|
||||||
}
|
}
|
||||||
|
if err == os.ErrNotExist {
|
||||||
|
i--
|
||||||
|
continue
|
||||||
|
}
|
||||||
return sum, nil
|
return sum, nil
|
||||||
}
|
}
|
||||||
buf[i] = data
|
buf[i] = data
|
||||||
@@ -109,8 +125,7 @@ func (star *StarBuffer) Read(buf []byte) (int, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (star *StarBuffer) Write(bts []byte) (int, error) {
|
func (star *StarBuffer) Write(bts []byte) (int, error) {
|
||||||
if bts == nil || star.isClose {
|
if bts == nil || star.isClose.Load().(bool) {
|
||||||
star.isEnd = true
|
|
||||||
return 0, io.EOF
|
return 0, io.EOF
|
||||||
}
|
}
|
||||||
star.wmu.Lock()
|
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)
|
return passwd(hint, defaultVal, mask)
|
||||||
}
|
}
|
||||||
|
|
||||||
func passwd(hint string, defaultVal string, mask string) InputMsg {
|
func MessageBoxRaw(hint string, defaultVal string) InputMsg {
|
||||||
var ioBuf []byte
|
return messageBox(hint, defaultVal)
|
||||||
|
}
|
||||||
|
|
||||||
|
func messageBox(hint string, defaultVal string) InputMsg {
|
||||||
|
var ioBuf []rune
|
||||||
if hint != "" {
|
if hint != "" {
|
||||||
fmt.Print(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 {
|
if err != nil {
|
||||||
return InputMsg{"", err}
|
return InputMsg{"", err}
|
||||||
}
|
}
|
||||||
defer terminal.Restore(0, state)
|
defer fmt.Println()
|
||||||
|
defer terminal.Restore(fd, state)
|
||||||
inputReader := bufio.NewReader(os.Stdin)
|
inputReader := bufio.NewReader(os.Stdin)
|
||||||
for {
|
for {
|
||||||
b, err := inputReader.ReadByte()
|
b, _, err := inputReader.ReadRune()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return InputMsg{"", err}
|
return InputMsg{"", err}
|
||||||
}
|
}
|
||||||
if b == 0x0d {
|
if b == 0x0d {
|
||||||
fmt.Println()
|
strValue := strings.TrimSpace(string(ioBuf))
|
||||||
return InputMsg{strings.TrimSpace(string(ioBuf)), nil}
|
if len(strValue) == 0 {
|
||||||
|
strValue = defaultVal
|
||||||
|
}
|
||||||
|
return InputMsg{strValue, nil}
|
||||||
}
|
}
|
||||||
if mask != "" {
|
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)
|
fmt.Print(mask)
|
||||||
}
|
}
|
||||||
ioBuf = append(ioBuf, b)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -59,7 +132,11 @@ func MessageBox(hint string, defaultVal string) InputMsg {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return InputMsg{"", err}
|
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) {
|
func (im InputMsg) String() (string, error) {
|
||||||
@@ -148,47 +225,46 @@ func (im InputMsg) MustFloat32() float32 {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func YesNo(hint string, defaults bool) bool {
|
func YesNo(hint string, defaults bool) bool {
|
||||||
res := strings.ToUpper(MessageBox(hint, "").MustString())
|
for {
|
||||||
if res == "" {
|
res := strings.ToUpper(MessageBox(hint, "").MustString())
|
||||||
return defaults
|
if res == "" {
|
||||||
}
|
return defaults
|
||||||
res = res[0:1]
|
}
|
||||||
if res == "Y" {
|
res = res[0:1]
|
||||||
return true
|
if res == "Y" {
|
||||||
} else if res == "N" {
|
return true
|
||||||
return false
|
} else if res == "N" {
|
||||||
} else {
|
return false
|
||||||
return defaults
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func StopUntil(hint string, trigger string, repeat bool) error {
|
func StopUntil(hint string, trigger string, repeat bool) error {
|
||||||
pressLen := len(trigger)
|
pressLen := len([]rune(trigger))
|
||||||
if trigger == "" {
|
if trigger == "" {
|
||||||
pressLen = 1
|
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 {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
defer terminal.Restore(0, state)
|
defer terminal.Restore(fd, state)
|
||||||
inputReader := bufio.NewReader(os.Stdin)
|
inputReader := bufio.NewReader(os.Stdin)
|
||||||
//ioBuf := make([]byte, pressLen)
|
//ioBuf := make([]byte, pressLen)
|
||||||
if hint != "" && !repeat {
|
|
||||||
fmt.Print(hint)
|
|
||||||
}
|
|
||||||
i := 0
|
i := 0
|
||||||
for {
|
for {
|
||||||
b, err := inputReader.ReadByte()
|
b, _, err := inputReader.ReadRune()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
if trigger == "" {
|
if trigger == "" {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
if b == trigger[i] {
|
if b == []rune(trigger)[i] {
|
||||||
i++
|
i++
|
||||||
if i == pressLen {
|
if i == pressLen {
|
||||||
break
|
break
|
||||||
@@ -197,7 +273,7 @@ func StopUntil(hint string, trigger string, repeat bool) error {
|
|||||||
}
|
}
|
||||||
i = 0
|
i = 0
|
||||||
if hint != "" && repeat {
|
if hint != "" && repeat {
|
||||||
fmt.Print("\n")
|
fmt.Print("\r\n")
|
||||||
fmt.Print(hint)
|
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