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
7c9883a3
Commit
7c9883a3
authored
Aug 18, 2019
by
xujianhai666
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
use disposed int32
parent
ee6d7d0e
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
25 deletions
+26
-25
queue.go
container/gxqueue/queue.go
+14
-14
queue_test.go
container/gxqueue/queue_test.go
+12
-11
No files found.
container/gxqueue/queue.go
View file @
7c9883a3
...
...
@@ -140,7 +140,7 @@ type Queue struct {
waiters
waiters
items
items
lock
sync
.
Mutex
disposed
bool
disposed
int32
}
// New is a constructor for a new threadsafe queue.
...
...
@@ -159,7 +159,7 @@ func (q *Queue) Put(items ...interface{}) error {
q
.
lock
.
Lock
()
defer
q
.
lock
.
Unlock
()
if
q
.
disposed
{
if
atomic
.
LoadInt32
(
&
q
.
disposed
)
==
1
{
return
ErrDisposed
}
...
...
@@ -205,7 +205,7 @@ func (q *Queue) Poll(number int64, timeout time.Duration) ([]interface{}, error)
q
.
lock
.
Lock
()
if
q
.
disposed
{
if
atomic
.
LoadInt32
(
&
q
.
disposed
)
==
1
{
q
.
lock
.
Unlock
()
return
nil
,
ErrDisposed
}
...
...
@@ -224,7 +224,7 @@ func (q *Queue) Poll(number int64, timeout time.Duration) ([]interface{}, error)
select
{
case
<-
sema
.
ready
:
// we are now inside the put's lock
if
q
.
disposed
{
if
atomic
.
LoadInt32
(
&
q
.
disposed
)
==
1
{
return
nil
,
ErrDisposed
}
items
=
q
.
items
.
get
(
number
)
...
...
@@ -258,7 +258,7 @@ func (q *Queue) Peek() (interface{}, error) {
q
.
lock
.
Lock
()
defer
q
.
lock
.
Unlock
()
if
q
.
disposed
{
if
atomic
.
LoadInt32
(
&
q
.
disposed
)
==
1
{
return
nil
,
ErrDisposed
}
...
...
@@ -270,17 +270,17 @@ func (q *Queue) Peek() (interface{}, error) {
return
peekItem
,
nil
}
//
TakeUntil take
s a function and returns a list of items that
//
GetUntil get
s a function and returns a list of items that
// match the checker until the checker returns false. This does not
// wait if there are no items in the queue.
func
(
q
*
Queue
)
Take
Until
(
checker
func
(
item
interface
{})
bool
)
([]
interface
{},
error
)
{
func
(
q
*
Queue
)
Get
Until
(
checker
func
(
item
interface
{})
bool
)
([]
interface
{},
error
)
{
if
checker
==
nil
{
return
nil
,
nil
}
q
.
lock
.
Lock
()
if
q
.
disposed
{
if
atomic
.
LoadInt32
(
&
q
.
disposed
)
==
1
{
q
.
lock
.
Unlock
()
return
nil
,
ErrDisposed
}
...
...
@@ -312,7 +312,7 @@ func (q *Queue) Disposed() bool {
q
.
lock
.
Lock
()
defer
q
.
lock
.
Unlock
()
return
q
.
disposed
return
atomic
.
LoadInt32
(
&
q
.
disposed
)
==
1
}
// Dispose will dispose of this queue and returns
...
...
@@ -322,7 +322,7 @@ func (q *Queue) Dispose() []interface{} {
q
.
lock
.
Lock
()
defer
q
.
lock
.
Unlock
()
q
.
disposed
=
true
atomic
.
StoreInt32
(
&
q
.
disposed
,
1
)
for
_
,
waiter
:=
range
q
.
waiters
{
waiter
.
response
.
Add
(
1
)
select
{
...
...
@@ -352,9 +352,9 @@ func ExecuteInParallel(q *Queue, fn func(interface{})) {
q
.
lock
.
Lock
()
// so no one touches anything in the middle
// of this process
todo
,
done
:=
uint64
(
len
(
q
.
items
)),
int64
(
-
1
)
length
,
count
:=
uint64
(
len
(
q
.
items
)),
int64
(
-
1
)
// this is important or we might face an infinite loop
if
todo
==
0
{
if
length
==
0
{
return
}
...
...
@@ -370,8 +370,8 @@ func ExecuteInParallel(q *Queue, fn func(interface{})) {
for
i
:=
0
;
i
<
numCPU
;
i
++
{
go
func
()
{
for
{
index
:=
atomic
.
AddInt64
(
&
done
,
1
)
if
index
>=
int64
(
todo
)
{
index
:=
atomic
.
AddInt64
(
&
count
,
1
)
if
index
>=
int64
(
length
)
{
wg
.
Done
()
break
}
...
...
container/gxqueue/queue_test.go
View file @
7c9883a3
...
...
@@ -181,6 +181,7 @@ func TestGetEmpty(t *testing.T) {
q
:=
New
(
10
)
go
func
()
{
time
.
Sleep
(
time
.
Second
)
q
.
Put
(
`a`
)
}()
...
...
@@ -223,7 +224,7 @@ func TestMultipleGetEmpty(t *testing.T) {
if
assert
.
Len
(
t
,
results
[
0
],
1
)
&&
assert
.
Len
(
t
,
results
[
1
],
1
)
{
assert
.
True
(
t
,
(
results
[
0
][
0
]
==
`a`
&&
results
[
1
][
0
]
==
`b`
)
||
(
results
[
0
][
0
]
==
`b`
&&
results
[
1
][
0
]
==
`a`
),
(
results
[
0
][
0
]
==
`b`
&&
results
[
1
][
0
]
==
`a`
),
`The array should be a, b or b, a`
)
}
}
...
...
@@ -370,10 +371,10 @@ func TestPeekOnDisposedQueue(t *testing.T) {
assert
.
IsType
(
t
,
ErrDisposed
,
err
)
}
func
Test
Take
Until
(
t
*
testing
.
T
)
{
func
Test
Get
Until
(
t
*
testing
.
T
)
{
q
:=
New
(
10
)
q
.
Put
(
`a`
,
`b`
,
`c`
)
result
,
err
:=
q
.
Take
Until
(
func
(
item
interface
{})
bool
{
result
,
err
:=
q
.
Get
Until
(
func
(
item
interface
{})
bool
{
return
item
!=
`c`
})
...
...
@@ -385,9 +386,9 @@ func TestTakeUntil(t *testing.T) {
assert
.
Equal
(
t
,
expected
,
result
)
}
func
Test
Take
UntilEmptyQueue
(
t
*
testing
.
T
)
{
func
Test
Get
UntilEmptyQueue
(
t
*
testing
.
T
)
{
q
:=
New
(
10
)
result
,
err
:=
q
.
Take
Until
(
func
(
item
interface
{})
bool
{
result
,
err
:=
q
.
Get
Until
(
func
(
item
interface
{})
bool
{
return
item
!=
`c`
})
...
...
@@ -399,10 +400,10 @@ func TestTakeUntilEmptyQueue(t *testing.T) {
assert
.
Equal
(
t
,
expected
,
result
)
}
func
Test
Take
UntilThenGet
(
t
*
testing
.
T
)
{
func
Test
Get
UntilThenGet
(
t
*
testing
.
T
)
{
q
:=
New
(
10
)
q
.
Put
(
`a`
,
`b`
,
`c`
)
takeItems
,
_
:=
q
.
Take
Until
(
func
(
item
interface
{})
bool
{
takeItems
,
_
:=
q
.
Get
Until
(
func
(
item
interface
{})
bool
{
return
item
!=
`c`
})
...
...
@@ -411,10 +412,10 @@ func TestTakeUntilThenGet(t *testing.T) {
assert
.
Equal
(
t
,
[]
interface
{}{
`c`
},
restItems
)
}
func
Test
Take
UntilNoMatches
(
t
*
testing
.
T
)
{
func
Test
Get
UntilNoMatches
(
t
*
testing
.
T
)
{
q
:=
New
(
10
)
q
.
Put
(
`a`
,
`b`
,
`c`
)
takeItems
,
_
:=
q
.
Take
Until
(
func
(
item
interface
{})
bool
{
takeItems
,
_
:=
q
.
Get
Until
(
func
(
item
interface
{})
bool
{
return
item
!=
`a`
})
...
...
@@ -423,10 +424,10 @@ func TestTakeUntilNoMatches(t *testing.T) {
assert
.
Equal
(
t
,
[]
interface
{}{
`a`
,
`b`
,
`c`
},
restItems
)
}
func
Test
Take
UntilOnDisposedQueue
(
t
*
testing
.
T
)
{
func
Test
Get
UntilOnDisposedQueue
(
t
*
testing
.
T
)
{
q
:=
New
(
10
)
q
.
Dispose
()
result
,
err
:=
q
.
Take
Until
(
func
(
item
interface
{})
bool
{
result
,
err
:=
q
.
Get
Until
(
func
(
item
interface
{})
bool
{
return
true
})
...
...
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