mirror of https://github.com/panjf2000/ants.git
Replace mutex with spin-lock
This commit is contained in:
parent
09973e8b1b
commit
e73db1d7d2
6
pool.go
6
pool.go
|
@ -46,7 +46,7 @@ type Pool struct {
|
|||
release int32
|
||||
|
||||
// lock for synchronous operation.
|
||||
lock sync.Mutex
|
||||
lock sync.Locker
|
||||
|
||||
// cond for waiting to get a idle worker.
|
||||
cond *sync.Cond
|
||||
|
@ -146,6 +146,7 @@ func NewPool(size int, options ...Option) (*Pool, error) {
|
|||
nonblocking: opts.Nonblocking,
|
||||
maxBlockingTasks: int32(opts.MaxBlockingTasks),
|
||||
panicHandler: opts.PanicHandler,
|
||||
lock: SpinLock(),
|
||||
}
|
||||
} else {
|
||||
p = &Pool{
|
||||
|
@ -154,9 +155,10 @@ func NewPool(size int, options ...Option) (*Pool, error) {
|
|||
nonblocking: opts.Nonblocking,
|
||||
maxBlockingTasks: int32(opts.MaxBlockingTasks),
|
||||
panicHandler: opts.PanicHandler,
|
||||
lock: SpinLock(),
|
||||
}
|
||||
}
|
||||
p.cond = sync.NewCond(&p.lock)
|
||||
p.cond = sync.NewCond(p.lock)
|
||||
|
||||
// Start a goroutine to clean up expired workers periodically.
|
||||
go p.periodicallyPurge()
|
||||
|
|
|
@ -46,7 +46,7 @@ type PoolWithFunc struct {
|
|||
release int32
|
||||
|
||||
// lock for synchronous operation.
|
||||
lock sync.Mutex
|
||||
lock sync.Locker
|
||||
|
||||
// cond for waiting to get a idle worker.
|
||||
cond *sync.Cond
|
||||
|
@ -154,6 +154,7 @@ func NewPoolWithFunc(size int, pf func(interface{}), options ...Option) (*PoolWi
|
|||
nonblocking: opts.Nonblocking,
|
||||
maxBlockingTasks: int32(opts.MaxBlockingTasks),
|
||||
panicHandler: opts.PanicHandler,
|
||||
lock: SpinLock(),
|
||||
}
|
||||
} else {
|
||||
p = &PoolWithFunc{
|
||||
|
@ -163,9 +164,10 @@ func NewPoolWithFunc(size int, pf func(interface{}), options ...Option) (*PoolWi
|
|||
nonblocking: opts.Nonblocking,
|
||||
maxBlockingTasks: int32(opts.MaxBlockingTasks),
|
||||
panicHandler: opts.PanicHandler,
|
||||
lock: SpinLock(),
|
||||
}
|
||||
}
|
||||
p.cond = sync.NewCond(&p.lock)
|
||||
p.cond = sync.NewCond(p.lock)
|
||||
|
||||
// Start a goroutine to clean up expired workers periodically.
|
||||
go p.periodicallyPurge()
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
// Copyright 2019 Andy Pan. All rights reserved.
|
||||
// Use of this source code is governed by an MIT-style
|
||||
// license that can be found in the LICENSE file.
|
||||
|
||||
package ants
|
||||
|
||||
import (
|
||||
"runtime"
|
||||
"sync"
|
||||
"sync/atomic"
|
||||
)
|
||||
|
||||
type spinLock uint32
|
||||
|
||||
func (sl *spinLock) Lock() {
|
||||
for !atomic.CompareAndSwapUint32((*uint32)(sl), 0, 1) {
|
||||
runtime.Gosched()
|
||||
}
|
||||
}
|
||||
|
||||
func (sl *spinLock) Unlock() {
|
||||
atomic.StoreUint32((*uint32)(sl), 0)
|
||||
}
|
||||
|
||||
func SpinLock() sync.Locker {
|
||||
return new(spinLock)
|
||||
}
|
Loading…
Reference in New Issue