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 ( ...@@ -24,6 +24,13 @@ import (
"unsafe" "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, // poolDequeue is a lock-free fixed-size single-producer,
// multi-consumer queue. The single producer can both push and pop // multi-consumer queue. The single producer can both push and pop
// from the head, and consumers can pop from the tail. // from the head, and consumers can pop from the tail.
...@@ -199,8 +206,8 @@ func (d *poolDequeue) PopTail() (interface{}, bool) { ...@@ -199,8 +206,8 @@ func (d *poolDequeue) PopTail() (interface{}, bool) {
return val, true return val, true
} }
// NewPoolDequeue new a poolDequeue instance. // NewSPMCLockFreeQ new a SPMCLockFreeQ instance.
func NewPoolDequeue(n int) (*poolDequeue, error) { func NewSPMCLockFreeQ(n int) (SPMCLockFreeQ, error) {
if n&(n-1) != 0 { if n&(n-1) != 0 {
return nil, errors.New("the size of pool must be a power of 2") return nil, errors.New("the size of pool must be a power of 2")
} }
......
...@@ -23,27 +23,25 @@ import ( ...@@ -23,27 +23,25 @@ import (
"sync" "sync"
"sync/atomic" "sync/atomic"
"testing" "testing"
)
import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func TestCreatePoolDequeue(t *testing.T) { 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") 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") 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") assert.EqualError(t, err, "the size of pool must be a power of 2")
_, err = NewPoolDequeue(8) _, err = NewSPMCLockFreeQ(8)
assert.NoError(t, err) assert.NoError(t, err)
} }
func TestPoolDequeue(t *testing.T) { func TestPoolDequeue(t *testing.T) {
const P = 10 const P = 10
var N int = 2e6 var N int = 2e6
d, err := NewPoolDequeue(16) d, err := NewSPMCLockFreeQ(16)
if err != nil { if err != nil {
t.Errorf("create poolDequeue fail") 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