File tree Expand file tree Collapse file tree 1 file changed +32
-1
lines changed
Expand file tree Collapse file tree 1 file changed +32
-1
lines changed Original file line number Diff line number Diff line change 3838 ErrStackIsFull = errors .New ("stack is full" )
3939)
4040
41- // ConcurrentQueue
41+ // ConcurrentQueue & ConcurrentStack
4242
4343// ConcurrentQueue ConcurrentQueue inspired by Collection utils
4444type 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
You can’t perform that action at this time.
0 commit comments