Skip to content

Commit bc215a0

Browse files
committed
update
1 parent 0e63a05 commit bc215a0

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

container/list/list.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package list
22

33
import (
4+
"errors"
45
"runtime"
56
"sync/atomic"
67
"unsafe"
78
)
89

9-
//thread safe
10+
// thread safe
1011
type List[T any] struct {
1112
head *node[T]
1213
tail *node[T]
@@ -43,19 +44,25 @@ func (l *List[T]) Push(data T) {
4344
atomic.StorePointer((*unsafe.Pointer)(unsafe.Pointer(&l.tail)), unsafe.Pointer(n))
4445
}
4546

46-
//check func is used to check whether the next element can be popped,set nil if don't need it
47-
//return false - when the buf is empty,or the check failed
48-
func (l *List[T]) Pop(check func(d T) bool) (data T, ok bool) {
47+
var ErrPopEmpty = errors.New("pop empty list")
48+
var ErrPopCheckFailed = errors.New("pop list check failed")
49+
50+
// check func is used to check whether the next element can be popped,set nil if don't need it
51+
// if e == ErrPopCheckFailed the data will return but it will not be poped from the list
52+
func (l *List[T]) Pop(check func(d T) bool) (data T, e error) {
4953
for {
5054
oldhead := l.head
5155
if oldhead.next == nil {
56+
e = ErrPopEmpty
5257
return
5358
}
5459
if check != nil && !check(oldhead.next.value) {
60+
data = oldhead.next.value
61+
e = ErrPopCheckFailed
5562
return
5663
}
5764
if atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&l.head)), unsafe.Pointer(oldhead), unsafe.Pointer(oldhead.next)) {
58-
return oldhead.next.value, true
65+
return oldhead.next.value, nil
5966
}
6067
runtime.Gosched()
6168
}

container/ring/ring.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package ring
22

33
import (
4+
"errors"
45
"runtime"
56
"sync/atomic"
67
)
@@ -37,16 +38,22 @@ func (b *Ring[T]) Push(d T) bool {
3738
}
3839
}
3940

41+
var ErrPopEmpty = errors.New("pop empty ring")
42+
var ErrPopCheckFailed = errors.New("pop ring check failed")
43+
4044
// check func is used to check whether the next element can be popped,set nil if don't need it
41-
// return false - when the buf is empty,or the check failed
42-
func (b *Ring[T]) Pop(check func(d T) bool) (data T, ok bool) {
45+
// if e == ErrPopCheckFailed the data will return but it will not be poped from the ring
46+
func (b *Ring[T]) Pop(check func(d T) bool) (data T, e error) {
4347
for {
4448
oldPopTry := atomic.LoadUint64(&b.popTry)
4549
if oldPopTry == atomic.LoadUint64(&b.pushConfirm) {
50+
e = ErrPopEmpty
4651
return
4752
}
4853
d := b.data[oldPopTry%atomic.LoadUint64(&b.length)]
4954
if check != nil && !check(d) {
55+
data = d
56+
e = ErrPopCheckFailed
5057
return
5158
}
5259
if !atomic.CompareAndSwapUint64(&b.popTry, oldPopTry, oldPopTry+1) {
@@ -55,6 +62,6 @@ func (b *Ring[T]) Pop(check func(d T) bool) (data T, ok bool) {
5562
for !atomic.CompareAndSwapUint64(&b.popConfirm, oldPopTry, oldPopTry+1) {
5663
runtime.Gosched()
5764
}
58-
return d, true
65+
return d, nil
5966
}
6067
}

container/stack/stack.go

+12-5
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
package stack
22

33
import (
4+
"errors"
45
"runtime"
56
"sync/atomic"
67
"unsafe"
78
)
89

9-
//thread safe
10+
// thread safe
1011
type Stack[T any] struct {
1112
top *node[T]
1213
}
@@ -31,19 +32,25 @@ func (s *Stack[T]) Push(data T) {
3132
}
3233
}
3334

34-
//check func is used to check whether the next element can be popped,set nil if don't need it
35-
//return false - when the buf is empty,or the check failed
36-
func (s *Stack[T]) Pop(check func(d T) bool) (data T, ok bool) {
35+
var ErrPopEmpty = errors.New("pop empty stack")
36+
var ErrPopCheckFailed = errors.New("pop stack check failed")
37+
38+
// check func is used to check whether the next element can be popped,set nil if don't need it
39+
// if e == ErrPopCheckFailed the data will return but it will not be poped from the stack
40+
func (s *Stack[T]) Pop(check func(d T) bool) (data T, e error) {
3741
for {
3842
oldtop := s.top
3943
if oldtop.pre == nil {
44+
e = ErrPopEmpty
4045
return
4146
}
4247
if check != nil && !check(oldtop.value) {
48+
data = oldtop.value
49+
e = ErrPopCheckFailed
4350
return
4451
}
4552
if atomic.CompareAndSwapPointer((*unsafe.Pointer)(unsafe.Pointer(&s.top)), unsafe.Pointer(oldtop), unsafe.Pointer(oldtop.pre)) {
46-
return oldtop.value, true
53+
return oldtop.value, nil
4754
}
4855
runtime.Gosched()
4956
}

rotatefile/rotatefile.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ func (f *RotateFile) run() {
9292
}
9393
}
9494
write := func() bool {
95-
if tmp, ok := f.caslist.Pop(nil); ok {
95+
if tmp, e := f.caslist.Pop(nil); e == nil {
9696
buf := tmp
9797
if n, e := f.buffile.Write(buf); e != nil {
9898
fmt.Println("[rotatefile.run] write error: " + e.Error())

0 commit comments

Comments
 (0)