From 5326374a2255449d5b3bc4c9dcb7f541310409c2 Mon Sep 17 00:00:00 2001 From: Andy Pan Date: Thu, 24 May 2018 23:43:34 +0800 Subject: [PATCH] Auto stash before merge of "develop" and "origin/develop" --- ants.go | 6 +++--- ants_benchmark_test.go | 6 +++--- ants_test.go | 5 +++-- examples/main.go | 2 +- pool.go | 12 +++++------- pool_func.go | 7 +++---- 6 files changed, 18 insertions(+), 20 deletions(-) diff --git a/ants.go b/ants.go index c306d41..3573b1c 100644 --- a/ants.go +++ b/ants.go @@ -39,9 +39,9 @@ const ( // Init a instance pool when importing ants var defaultPool, _ = NewPool(DefaultPoolSize) -// Push submit a task to pool -func Push(task f) error { - return defaultPool.Push(task) +// Submit submit a task to pool +func Submit(task f) error { + return defaultPool.Submit(task) } // Running returns the number of the currently running goroutines diff --git a/ants_benchmark_test.go b/ants_benchmark_test.go index 0d6e0c6..06fdcd3 100644 --- a/ants_benchmark_test.go +++ b/ants_benchmark_test.go @@ -44,8 +44,9 @@ const ( const RunTimes = 10000000 const loop = 10 -func demoFunc() { +func demoFunc() error { time.Sleep(loop * time.Millisecond) + return nil } func demoPoolFunc(args interface{}) error { @@ -101,10 +102,9 @@ func BenchmarkGoroutine(b *testing.B) { } func BenchmarkAntsPool(b *testing.B) { - b.N = 3 for i := 0; i < b.N; i++ { for j := 0; j < RunTimes; j++ { - ants.Push(demoFunc) + ants.Submit(demoFunc) } b.Logf("running goroutines: %d", ants.Running()) } diff --git a/ants_test.go b/ants_test.go index 451b5b3..71e4493 100644 --- a/ants_test.go +++ b/ants_test.go @@ -36,9 +36,10 @@ func TestDefaultPool(t *testing.T) { var wg sync.WaitGroup for i := 0; i < n; i++ { wg.Add(1) - ants.Push(func() { + ants.Submit(func() error { demoFunc() wg.Done() + return nil }) } wg.Wait() @@ -111,7 +112,7 @@ func TestNoPool(t *testing.T) { // var wg sync.WaitGroup // for i := 0; i < n; i++ { // wg.Add(1) -// p.Push(func() { +// p.Submit(func() { // demoFunc() // //demoFunc() // wg.Done() diff --git a/examples/main.go b/examples/main.go index b1e6602..141029b 100644 --- a/examples/main.go +++ b/examples/main.go @@ -45,7 +45,7 @@ func myFunc(i interface{}) error { // // submit all your tasks to ants pool // for i := 0; i < runTimes; i++ { // wg.Add(1) -// ants.Push(func() { +// ants.Submit(func() { // myFunc() // wg.Done() // }) diff --git a/pool.go b/pool.go index b7b58ef..a080872 100644 --- a/pool.go +++ b/pool.go @@ -30,9 +30,9 @@ import ( type sig struct{} -type f func() +type f func() error -// Pool accept the tasks from client,it will limit the total +// Pool accept the tasks from client,it limits the total // of goroutines to a given number by recycling goroutines. type Pool struct { // capacity of the pool. @@ -48,12 +48,10 @@ type Pool struct { // workers is a slice that store the available workers. workers []*Worker - // workerPool is a pool that saves a set of temporary objects. - workerPool sync.Pool - // release is used to notice the pool to closed itself. release chan sig + // lock for synchronous operation lock sync.Mutex once sync.Once @@ -75,8 +73,8 @@ func NewPool(size int) (*Pool, error) { //------------------------------------------------------------------------- -// Push submit a task to pool -func (p *Pool) Push(task f) error { +// Submit submit a task to pool +func (p *Pool) Submit(task f) error { if len(p.release) > 0 { return ErrPoolClosed } diff --git a/pool_func.go b/pool_func.go index a8cb357..b236cf9 100644 --- a/pool_func.go +++ b/pool_func.go @@ -30,7 +30,7 @@ import ( type pf func(interface{}) error -// PoolWithFunc accept the tasks from client,it will limit the total +// PoolWithFunc accept the tasks from client,it limits the total // of goroutines to a given number by recycling goroutines. type PoolWithFunc struct { // capacity of the pool. @@ -46,14 +46,13 @@ type PoolWithFunc struct { // workers is a slice that store the available workers. workers []*WorkerWithFunc - // workerPool is a pool that saves a set of temporary objects. - //workerPool sync.Pool - // release is used to notice the pool to closed itself. release chan sig + // lock for synchronous operation lock sync.Mutex + // pf is the function for processing tasks poolFunc pf once sync.Once