mirror of
https://github.com/charlienet/go-mixed.git
synced 2025-07-18 00:22:41 +08:00
添加锁
This commit is contained in:
@ -1,6 +1,9 @@
|
|||||||
package store
|
package store
|
||||||
|
|
||||||
import "fmt"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
// 号段
|
// 号段
|
||||||
type Segment struct {
|
type Segment struct {
|
||||||
@ -8,22 +11,35 @@ type Segment struct {
|
|||||||
end int64
|
end int64
|
||||||
current int64
|
current int64
|
||||||
reback bool
|
reback bool
|
||||||
|
mu sync.RWMutex
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Segment) Allot() int64 {
|
func (s *Segment) Allot() int64 {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
s.current++
|
s.current++
|
||||||
return s.current
|
return s.current
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Segment) IsEnding() bool {
|
func (s *Segment) IsEnding() bool {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
return (s.current - s.start) > (s.end - s.current)
|
return (s.current - s.start) > (s.end - s.current)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Segment) IsEmpty() bool {
|
func (s *Segment) IsEmpty() bool {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
return s.current == s.end
|
return s.current == s.end
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Segment) Reback() bool {
|
func (s *Segment) Reback() bool {
|
||||||
|
s.mu.Lock()
|
||||||
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
// println("回旋确认:", s.reback, s.current == (s.start+1))
|
// println("回旋确认:", s.reback, s.current == (s.start+1))
|
||||||
return s.reback && s.current == (s.start+1)
|
return s.reback && s.current == (s.start+1)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user