forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert UnboundedQueue to UnboundedDeque (ava-labs#1878)
Co-authored-by: Stephen Buttolph <[email protected]>
- Loading branch information
1 parent
d747a96
commit d913e13
Showing
11 changed files
with
833 additions
and
616 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package buffer | ||
|
||
import ( | ||
"sync" | ||
|
||
"github.com/ava-labs/avalanchego/utils" | ||
) | ||
|
||
var _ BlockingDeque[int] = &unboundedBlockingDeque[int]{} | ||
|
||
type BlockingDeque[T any] interface { | ||
Deque[T] | ||
|
||
// Close and empty the deque. | ||
Close() | ||
} | ||
|
||
// Returns a new unbounded deque with the given initial size. | ||
// Note that the returned deque is always empty -- [initSize] is just | ||
// a hint to prevent unnecessary resizing. | ||
func NewUnboundedBlockingDeque[T any](initSize int) BlockingDeque[T] { | ||
q := &unboundedBlockingDeque[T]{ | ||
Deque: NewUnboundedDeque[T](initSize), | ||
} | ||
q.cond = sync.NewCond(&q.lock) | ||
return q | ||
} | ||
|
||
type unboundedBlockingDeque[T any] struct { | ||
lock sync.RWMutex | ||
cond *sync.Cond | ||
closed bool | ||
|
||
Deque[T] | ||
} | ||
|
||
// If the deque is closed returns false. | ||
func (q *unboundedBlockingDeque[T]) PushRight(elt T) bool { | ||
q.cond.L.Lock() | ||
defer q.cond.L.Unlock() | ||
|
||
if q.closed { | ||
return false | ||
} | ||
|
||
// Add the item to the queue | ||
q.Deque.PushRight(elt) | ||
|
||
// Signal a waiting thread | ||
q.cond.Signal() | ||
return true | ||
} | ||
|
||
// If the deque is closed returns false. | ||
func (q *unboundedBlockingDeque[T]) PopRight() (T, bool) { | ||
q.cond.L.Lock() | ||
defer q.cond.L.Unlock() | ||
|
||
for { | ||
if q.closed { | ||
return utils.Zero[T](), false | ||
} | ||
if q.Deque.Len() != 0 { | ||
return q.Deque.PopRight() | ||
} | ||
q.cond.Wait() | ||
} | ||
} | ||
|
||
func (q *unboundedBlockingDeque[T]) PeekRight() (T, bool) { | ||
q.lock.RLock() | ||
defer q.lock.RUnlock() | ||
|
||
if q.closed { | ||
return utils.Zero[T](), false | ||
} | ||
return q.Deque.PeekRight() | ||
} | ||
|
||
// If the deque is closed returns false. | ||
func (q *unboundedBlockingDeque[T]) PushLeft(elt T) bool { | ||
q.cond.L.Lock() | ||
defer q.cond.L.Unlock() | ||
|
||
if q.closed { | ||
return false | ||
} | ||
|
||
// Add the item to the queue | ||
q.Deque.PushLeft(elt) | ||
|
||
// Signal a waiting thread | ||
q.cond.Signal() | ||
return true | ||
} | ||
|
||
// If the deque is closed returns false. | ||
func (q *unboundedBlockingDeque[T]) PopLeft() (T, bool) { | ||
q.cond.L.Lock() | ||
defer q.cond.L.Unlock() | ||
|
||
for { | ||
if q.closed { | ||
return utils.Zero[T](), false | ||
} | ||
if q.Deque.Len() != 0 { | ||
return q.Deque.PopLeft() | ||
} | ||
q.cond.Wait() | ||
} | ||
} | ||
|
||
func (q *unboundedBlockingDeque[T]) PeekLeft() (T, bool) { | ||
q.lock.RLock() | ||
defer q.lock.RUnlock() | ||
|
||
if q.closed { | ||
return utils.Zero[T](), false | ||
} | ||
return q.Deque.PeekLeft() | ||
} | ||
|
||
func (q *unboundedBlockingDeque[T]) Len() int { | ||
q.lock.RLock() | ||
defer q.lock.RUnlock() | ||
|
||
if q.closed { | ||
return 0 | ||
} | ||
return q.Deque.Len() | ||
} | ||
|
||
func (q *unboundedBlockingDeque[T]) Close() { | ||
q.cond.L.Lock() | ||
defer q.cond.L.Unlock() | ||
|
||
if q.closed { | ||
return | ||
} | ||
|
||
q.Deque = nil | ||
|
||
// Mark the queue as closed | ||
q.closed = true | ||
q.cond.Broadcast() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Copyright (C) 2019-2022, Ava Labs, Inc. All rights reserved. | ||
// See the file LICENSE for licensing terms. | ||
|
||
package buffer | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestUnboundedBlockingDequePush(t *testing.T) { | ||
require := require.New(t) | ||
|
||
deque := NewUnboundedBlockingDeque[int](2) | ||
|
||
ok := deque.PushRight(1) | ||
require.True(ok) | ||
ok = deque.PushRight(2) | ||
require.True(ok) | ||
|
||
ch, ok := deque.PopLeft() | ||
require.True(ok) | ||
require.Equal(1, ch) | ||
} | ||
|
||
func TestUnboundedBlockingDequePop(t *testing.T) { | ||
require := require.New(t) | ||
|
||
deque := NewUnboundedBlockingDeque[int](2) | ||
|
||
ok := deque.PushRight(1) | ||
require.True(ok) | ||
|
||
ch, ok := deque.PopLeft() | ||
require.True(ok) | ||
require.Equal(1, ch) | ||
|
||
wg := &sync.WaitGroup{} | ||
wg.Add(1) | ||
go func() { | ||
ch, ok := deque.PopLeft() | ||
require.True(ok) | ||
require.Equal(2, ch) | ||
wg.Done() | ||
}() | ||
|
||
ok = deque.PushRight(2) | ||
require.True(ok) | ||
wg.Wait() | ||
} | ||
|
||
func TestUnboundedBlockingDequeClose(t *testing.T) { | ||
require := require.New(t) | ||
|
||
deque := NewUnboundedBlockingDeque[int](2) | ||
|
||
ok := deque.PushLeft(1) | ||
require.True(ok) | ||
|
||
deque.Close() | ||
|
||
_, ok = deque.PopRight() | ||
require.False(ok) | ||
|
||
ok = deque.PushLeft(1) | ||
require.False(ok) | ||
} |
Oops, something went wrong.