Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
gostnops
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
wei.xuan
gostnops
Commits
ca4cfcb0
Unverified
Commit
ca4cfcb0
authored
Nov 14, 2020
by
Xin.Zh
Committed by
GitHub
Nov 14, 2020
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #34 from wenxuwan/master
add interface SPMCLockFreeQ
parents
218e6a07
919d98af
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
14 additions
and
9 deletions
+14
-9
poolqueue.go
container/queue/poolqueue.go
+9
-2
poolqueue_test.go
container/queue/poolqueue_test.go
+5
-7
No files found.
container/queue/poolqueue.go
View file @
ca4cfcb0
...
...
@@ -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
}
// New
PoolDequeue new a poolDequeue
instance.
func
New
PoolDequeue
(
n
int
)
(
*
poolDequeue
,
error
)
{
// New
SPMCLockFreeQ new a SPMCLockFreeQ
instance.
func
New
SPMCLockFreeQ
(
n
int
)
(
SPMCLockFreeQ
,
error
)
{
if
n
&
(
n
-
1
)
!=
0
{
return
nil
,
errors
.
New
(
"the size of pool must be a power of 2"
)
}
...
...
container/queue/poolqueue_test.go
View file @
ca4cfcb0
...
...
@@ -23,27 +23,25 @@ import (
"sync"
"sync/atomic"
"testing"
)
import
(
"github.com/stretchr/testify/assert"
)
func
TestCreatePoolDequeue
(
t
*
testing
.
T
)
{
_
,
err
:=
New
PoolDequeue
(
15
)
_
,
err
:=
New
SPMCLockFreeQ
(
15
)
assert
.
EqualError
(
t
,
err
,
"the size of pool must be a power of 2"
)
_
,
err
=
New
PoolDequeue
(
18
)
_
,
err
=
New
SPMCLockFreeQ
(
18
)
assert
.
EqualError
(
t
,
err
,
"the size of pool must be a power of 2"
)
_
,
err
=
New
PoolDequeue
(
24
)
_
,
err
=
New
SPMCLockFreeQ
(
24
)
assert
.
EqualError
(
t
,
err
,
"the size of pool must be a power of 2"
)
_
,
err
=
New
PoolDequeue
(
8
)
_
,
err
=
New
SPMCLockFreeQ
(
8
)
assert
.
NoError
(
t
,
err
)
}
func
TestPoolDequeue
(
t
*
testing
.
T
)
{
const
P
=
10
var
N
int
=
2e6
d
,
err
:=
New
PoolDequeue
(
16
)
d
,
err
:=
New
SPMCLockFreeQ
(
16
)
if
err
!=
nil
{
t
.
Errorf
(
"create poolDequeue fail"
)
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment