Unverified Commit ca4cfcb0 authored by Xin.Zh's avatar Xin.Zh Committed by GitHub

Merge pull request #34 from wenxuwan/master

add interface SPMCLockFreeQ
parents 218e6a07 919d98af
......@@ -24,6 +24,13 @@ import (
"unsafe"
)
// SPMCLockFreeQ is a lock-free queue.
type SPMCLockFreeQ interface {
PushHead(val interface{}) bool
PopHead() (interface{}, bool)
PopTail() (interface{}, bool)
}
// poolDequeue is a lock-free fixed-size single-producer,
// multi-consumer queue. The single producer can both push and pop
// from the head, and consumers can pop from the tail.
......@@ -199,8 +206,8 @@ func (d *poolDequeue) PopTail() (interface{}, bool) {
return val, true
}
// NewPoolDequeue new a poolDequeue instance.
func NewPoolDequeue(n int) (*poolDequeue, error) {
// NewSPMCLockFreeQ new a SPMCLockFreeQ instance.
func NewSPMCLockFreeQ(n int) (SPMCLockFreeQ, error) {
if n&(n-1) != 0 {
return nil, errors.New("the size of pool must be a power of 2")
}
......
......@@ -23,27 +23,25 @@ import (
"sync"
"sync/atomic"
"testing"
)
import (
"github.com/stretchr/testify/assert"
)
func TestCreatePoolDequeue(t *testing.T) {
_, err := NewPoolDequeue(15)
_, err := NewSPMCLockFreeQ(15)
assert.EqualError(t, err, "the size of pool must be a power of 2")
_, err = NewPoolDequeue(18)
_, err = NewSPMCLockFreeQ(18)
assert.EqualError(t, err, "the size of pool must be a power of 2")
_, err = NewPoolDequeue(24)
_, err = NewSPMCLockFreeQ(24)
assert.EqualError(t, err, "the size of pool must be a power of 2")
_, err = NewPoolDequeue(8)
_, err = NewSPMCLockFreeQ(8)
assert.NoError(t, err)
}
func TestPoolDequeue(t *testing.T) {
const P = 10
var N int = 2e6
d, err := NewPoolDequeue(16)
d, err := NewSPMCLockFreeQ(16)
if err != nil {
t.Errorf("create poolDequeue fail")
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment