-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfixed_stack.go
290 lines (261 loc) · 7.78 KB
/
fixed_stack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
// ©Hayabusa Cloud Co., Ltd. 2023. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package sox
import (
"errors"
"io"
"math"
"sync/atomic"
)
// Stack is the interface that wraps Push and Pop operations on a Stack
type Stack[ItemType any] interface {
// Push inserts an element at the of the Stack
// It returns io.ErrClosesPipe if the Stack is closed.
// If the Stack is full and the Nonblocking option is set as true,
// an ErrTemporarilyUnavailable will be returned.
// If the Stack is full and the Nonblocking option is set as false,
// the Push operation blocks until the Stack became not full
Push(item ItemType) error
// Pop removes and returns the element at the top of the Stack
// When the Stack is empty,
// It returns the zero-value and io.EOF if the Stack is already closed.
// It returns the zero-value and ErrTemporarilyUnavailable if the Stack is set as Nonblocking.
// If the stack is not set as Nonblocking, it blocks until any element has been Pushed.
Pop() (item ItemType, err error)
// Close closes the Stack. The return value is always nil.
Close() error
}
const (
defaultFixedStackCapacity = math.MaxInt16
)
// NewFixedStack creates and returns a fixed capacity Stack with the given options
func NewFixedStack[ItemType any](opts ...func(options *FixedStackOptions)) (Stack[ItemType], error) {
o := &FixedStackOptions{
Capacity: defaultFixedStackCapacity,
Concurrent: true,
Nonblocking: false,
}
for _, f := range opts {
f(o)
}
if o.Capacity < 1 || o.Capacity >= (1<<30) {
return nil, errors.New("invalid fixed stack capacity")
}
o.Capacity |= o.Capacity >> 1
o.Capacity |= o.Capacity >> 2
o.Capacity |= o.Capacity >> 4
o.Capacity |= o.Capacity >> 8
o.Capacity |= o.Capacity >> 16
s := make([]ItemType, o.Capacity)
if o.Concurrent {
stack := newFixedStackConcurrent[ItemType](s, o)
return stack, nil
}
stack := newFixedStack[ItemType](s, o)
return stack, nil
}
// FixedStackOptions holds optional parameters for Stack implementations
type FixedStackOptions struct {
// Capacity specifies the capacity of Stack. The default Capacity is 32K
Capacity uint32
// Concurrent specifies whether the Stack works in Concurrent mode or not
// Concurrent should be set as true,
// if there are multiple goroutines doing Push or multiple goroutines doing Pop
Concurrent bool
// Nonblocking specifies whether the Push or Pop operations will NOT block
// even if it is temporarily unavailable or not
Nonblocking bool
}
type fixedStack[T any] struct {
_ noCopy
*FixedStackOptions
stack []T
top atomic.Uint32
}
func newFixedStack[T any](stack []T, opt *FixedStackOptions) *fixedStack[T] {
return &fixedStack[T]{
FixedStackOptions: opt,
stack: stack,
top: atomic.Uint32{},
}
}
// Push adds an item to the Stack.
// If the stack is full, it will either return an error or wait if nonblocking is false.
// If the stack is closed, it returns io.ErrClosedPipe.
func (s *fixedStack[T]) Push(item T) error {
sw := NewSpinWaitWithLevel(SpinWaitLevelPending)
for {
top := s.top.Load()
if top&fixedStackStatusClosed == fixedStackStatusClosed {
return io.ErrClosedPipe
}
if top&fixedStackTopValueMask >= s.Capacity {
if s.Nonblocking {
return ErrTemporarilyUnavailable
}
sw.Once()
continue
}
s.stack[top] = item
if !s.top.CompareAndSwap(top, top+1) {
sw.Once()
continue
}
break
}
return nil
}
// Pop removes and returns the top item from the Stack.
// If the stack is empty, it will either return an error or wait if nonblocking is false.
// If the stack is closed, it returns io.EOF.
func (s *fixedStack[T]) Pop() (item T, err error) {
sw := NewSpinWaitWithLevel(SpinWaitLevelPending)
for {
top := s.top.Load()
if top&fixedStackTopValueMask <= 0 {
if top&fixedStackStatusClosed == fixedStackStatusClosed {
return item, io.EOF
}
if s.Nonblocking {
return item, ErrTemporarilyUnavailable
}
sw.Once()
continue
}
item = s.stack[s.top.Add(math.MaxUint32)&fixedStackTopValueMask]
break
}
return item, nil
}
// Close closes the Stack
func (s *fixedStack[T]) Close() error {
sw := NewSpinWaitWithLevel(SpinWaitLevelPending)
for {
top := s.top.Load()
if top&fixedStackStatusClosed == fixedStackStatusClosed {
return nil
}
if swapped := s.top.CompareAndSwap(top, top|fixedStackStatusClosed); !swapped {
sw.Once()
continue
}
break
}
return nil
}
const (
fixedStackStatusWriting = 1 << 31
fixedStackStatusClosed = 1 << 30
fixedStackStatusMask = fixedStackStatusWriting | fixedStackStatusClosed
fixedStackTopValueMask = (1 << 30) - 1
)
type fixedStackConcurrent[T any] struct {
_ noCopy
*FixedStackOptions
stack []T
top atomic.Uint32
}
func newFixedStackConcurrent[T any](stack []T, opt *FixedStackOptions) *fixedStackConcurrent[T] {
return &fixedStackConcurrent[T]{
FixedStackOptions: opt,
stack: stack,
top: atomic.Uint32{},
}
}
// Push adds an item to the Stack.
//
// If the stack is closed, it returns io.ErrClosedPipe.
// If the stack is full and nonblocking is set to true,
// it will return ErrTemporarilyUnavailable.
//
// The Stack implementation is thread-safe for concurrent calls to Push and Pop methods.
func (s *fixedStackConcurrent[T]) Push(item T) error {
sw := NewSpinWaitWithLevel(SpinWaitLevelPending)
for {
top := s.top.Load()
if top&fixedStackStatusWriting == fixedStackStatusWriting {
sw.Once()
continue
}
if top&fixedStackStatusClosed == fixedStackStatusClosed {
return io.ErrClosedPipe
}
if top&fixedStackTopValueMask >= s.Capacity {
if s.Nonblocking {
return ErrTemporarilyUnavailable
}
sw.Once()
continue
}
newTop := fixedStackStatusWriting | (top&fixedStackTopValueMask + 1)
if !s.top.CompareAndSwap(top, newTop) {
sw.Once()
continue
}
s.stack[top&fixedStackTopValueMask] = item
s.top.Store(newTop&(fixedStackStatusMask^fixedStackStatusWriting) | newTop&fixedStackTopValueMask)
break
}
return nil
}
// Pop removes and returns the top item from the Stack.
//
// If the Stack is empty and nonblocking is set to true, it will return a zero value
// and ErrTemporarilyUnavailable. If the Stack is closed, it returns a zero value
// and io.EOF. If the Stack is not empty, it removes the top item and returns it.
//
// The Stack implementation is thread-safe for concurrent calls to Push and Pop methods.
func (s *fixedStackConcurrent[T]) Pop() (item T, err error) {
sw := NewSpinWaitWithLevel(SpinWaitLevelPending)
for {
top := s.top.Load()
if top&fixedStackTopValueMask <= 0 {
if top&fixedStackStatusClosed == fixedStackStatusClosed {
return item, io.EOF
}
if s.Nonblocking {
return item, ErrTemporarilyUnavailable
}
sw.Once()
continue
}
if top&fixedStackStatusWriting == fixedStackStatusWriting {
sw.Once()
continue
}
newTop := fixedStackStatusWriting | (top&fixedStackTopValueMask - 1)
if !s.top.CompareAndSwap(top, newTop) {
sw.Once()
continue
}
item = s.stack[newTop&fixedStackTopValueMask]
s.top.Store(newTop&(fixedStackStatusMask^fixedStackStatusWriting) | newTop&fixedStackTopValueMask)
break
}
return item, nil
}
// Close closes the Stack.
//
// If the Stack is already closed, it returns nil.
// If there is a writer currently writing to the Stack,
// it will wait until the writer finishes before closing the stack.
func (s *fixedStackConcurrent[T]) Close() error {
sw := NewSpinWaitWithLevel(SpinWaitLevelPending)
for {
top := s.top.Load()
if top&fixedStackStatusClosed == fixedStackStatusClosed {
return nil
}
if top&fixedStackStatusWriting == fixedStackStatusWriting {
sw.Once()
continue
}
if swapped := s.top.CompareAndSwap(top, top|fixedStackStatusClosed); !swapped {
sw.Once()
continue
}
break
}
return nil
}