Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
G
getty
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
getty
Commits
4b32c61e
Commit
4b32c61e
authored
Jun 08, 2019
by
AlexStocks
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Imp: use just 1 task queue
parent
f8a04498
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
13 additions
and
21 deletions
+13
-21
task_pool.go
task_pool.go
+13
-21
No files found.
task_pool.go
View file @
4b32c61e
...
...
@@ -3,7 +3,6 @@ package getty
import
(
"fmt"
"sync"
"sync/atomic"
)
import
(
...
...
@@ -35,11 +34,10 @@ type task struct {
// task pool: manage task ts
type
taskPool
struct
{
idx
uint32
qLen
int32
// task queue length
size
int32
// task queue pool size
qArray
[]
chan
task
wg
sync
.
WaitGroup
qLen
int32
// task queue length
size
int32
// task queue pool size
Q
chan
task
wg
sync
.
WaitGroup
once
sync
.
Once
done
chan
struct
{}
...
...
@@ -48,10 +46,10 @@ type taskPool struct {
// build a task pool
func
newTaskPool
(
poolSize
int32
,
taskQLen
int32
)
*
taskPool
{
return
&
taskPool
{
size
:
poolSize
,
qLen
:
taskQLen
,
qArray
:
make
([]
chan
task
,
poolSize
),
done
:
make
(
chan
struct
{}),
size
:
poolSize
,
qLen
:
taskQLen
,
Q
:
make
(
chan
task
,
taskQLen
),
done
:
make
(
chan
struct
{}),
}
}
...
...
@@ -66,7 +64,6 @@ func (p *taskPool) start() {
}
for
i
:=
int32
(
0
);
i
<
p
.
size
;
i
++
{
p
.
qArray
[
i
]
=
make
(
chan
task
,
p
.
qLen
)
p
.
wg
.
Add
(
1
)
taskID
:=
i
go
p
.
run
(
int
(
taskID
))
...
...
@@ -85,14 +82,14 @@ func (p *taskPool) run(id int) {
for
{
select
{
case
<-
p
.
done
:
if
0
<
len
(
p
.
qArray
[
id
]
)
{
if
0
<
len
(
p
.
Q
)
{
log
.
Warn
(
"[getty][task_pool] task %d exit now while its task length is %d greater than 0"
,
id
,
len
(
p
.
qArray
[
id
]
))
id
,
len
(
p
.
Q
))
}
log
.
Info
(
"[getty][task_pool] task %d exit now"
,
id
)
return
case
t
,
ok
=
<-
p
.
qArray
[
id
]
:
case
t
,
ok
=
<-
p
.
Q
:
if
ok
{
t
.
session
.
listener
.
OnMessage
(
t
.
session
,
t
.
pkg
)
}
...
...
@@ -102,13 +99,10 @@ func (p *taskPool) run(id int) {
// add task
func
(
p
*
taskPool
)
AddTask
(
t
task
)
{
//id := randID() % uint64(p.size)
id
:=
atomic
.
AddUint32
(
&
p
.
idx
,
1
)
%
uint32
(
p
.
size
)
select
{
case
<-
p
.
done
:
return
case
p
.
qArray
[
id
]
<-
t
:
case
p
.
Q
<-
t
:
}
}
...
...
@@ -138,7 +132,5 @@ func (p *taskPool) isClosed() bool {
func
(
p
*
taskPool
)
close
()
{
p
.
stop
()
p
.
wg
.
Wait
()
for
i
:=
range
p
.
qArray
{
close
(
p
.
qArray
[
i
])
}
close
(
p
.
Q
)
}
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