Skip to content

Commit f2b7e93

Browse files
committed
Add ConcurrentStack(stack)
1 parent 275bd1f commit f2b7e93

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

queue.go

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ var (
3838
ErrStackIsFull = errors.New("stack is full")
3939
)
4040

41-
// ConcurrentQueue
41+
// ConcurrentQueue & ConcurrentStack
4242

4343
// ConcurrentQueue ConcurrentQueue inspired by Collection utils
4444
type ConcurrentQueue struct {
@@ -85,6 +85,37 @@ func (q *ConcurrentQueue) Poll() (interface{}, error) {
8585
return q.queue.Poll()
8686
}
8787

88+
// ConcurrentStack
89+
90+
// ConcurrentStack ConcurrentStack inspired by Collection utils
91+
type ConcurrentStack struct {
92+
lock sync.RWMutex
93+
stack Stack
94+
}
95+
96+
// NewConcurrentStack New ConcurrentStack instance from a Stack
97+
func NewConcurrentStack(stack Stack) *ConcurrentStack {
98+
return &ConcurrentStack{
99+
stack: stack,
100+
}
101+
}
102+
103+
// Put Put the val(probably blocking)
104+
func (q *ConcurrentStack) Push(val interface{}) error {
105+
q.lock.Lock()
106+
defer q.lock.Unlock()
107+
108+
return q.stack.Push(val)
109+
}
110+
111+
// Take Take the val(probably blocking)
112+
func (q *ConcurrentStack) Pop() (interface{}, error) {
113+
q.lock.RLock()
114+
defer q.lock.RUnlock()
115+
116+
return q.stack.Pop()
117+
}
118+
88119
// ChannelQueue
89120

90121
// ChannelQueue ChannelQueue inspired by Collection utils

0 commit comments

Comments
 (0)