From 6ee6c007c5c4ce5a2037b0702a3cc924b26b3073 Mon Sep 17 00:00:00 2001 From: John Date: Sat, 22 Jun 2019 14:42:27 +0800 Subject: [PATCH] update gmutex and its benchmark test cases --- g/os/gmutex/gmutex.go | 41 ++++++++++++++++---------------- g/os/gmutex/gmutex_bench_test.go | 32 ++++++++++++++++++++----- 2 files changed, 46 insertions(+), 27 deletions(-) diff --git a/g/os/gmutex/gmutex.go b/g/os/gmutex/gmutex.go index 5535fc5de..7acc97df9 100644 --- a/g/os/gmutex/gmutex.go +++ b/g/os/gmutex/gmutex.go @@ -120,7 +120,6 @@ func (m *Mutex) RUnlock() { if n == 1 && m.writer.Val() > 0 { // No readers blocked, then the writers can take place. m.writing <- struct{}{} - return } } @@ -162,6 +161,26 @@ func (m *Mutex) IsRLocked() bool { return m.state.Val() > 0 } +// LockFunc locks the mutex for writing with given callback function . +// If there's a write/reading lock the mutex, it will blocks until the lock is released. +// +// It releases the lock after is executed. +func (m *Mutex) LockFunc(f func()) { + m.Lock() + defer m.Unlock() + f() +} + +// RLockFunc locks the mutex for reading with given callback function . +// If there's a writing lock the mutex, it will blocks until the lock is released. +// +// It releases the lock after is executed. +func (m *Mutex) RLockFunc(f func()) { + m.RLock() + defer m.RUnlock() + f() +} + // TryLockFunc tries locking the mutex for writing with given callback function . // it returns true if success, or if there's a write/reading lock on the mutex, // it returns false. @@ -188,23 +207,3 @@ func (m *Mutex) TryRLockFunc(f func()) bool { } return false } - -// LockFunc locks the mutex for writing with given callback function . -// If there's a write/reading lock the mutex, it will blocks until the lock is released. -// -// It releases the lock after is executed. -func (m *Mutex) LockFunc(f func()) { - m.Lock() - defer m.Unlock() - f() -} - -// RLockFunc locks the mutex for reading with given callback function . -// If there's a writing lock the mutex, it will blocks until the lock is released. -// -// It releases the lock after is executed. -func (m *Mutex) RLockFunc(f func()) { - m.RLock() - defer m.RUnlock() - f() -} diff --git a/g/os/gmutex/gmutex_bench_test.go b/g/os/gmutex/gmutex_bench_test.go index 47b100f43..9b7473bd2 100644 --- a/g/os/gmutex/gmutex_bench_test.go +++ b/g/os/gmutex/gmutex_bench_test.go @@ -14,21 +14,29 @@ import ( ) var ( - mu = sync.RWMutex{} - gmu = gmutex.New() + mu = sync.Mutex{} + rwmu = sync.RWMutex{} + gmu = gmutex.New() ) -func Benchmark_Sync_LockUnlock(b *testing.B) { +func Benchmark_Mutex_LockUnlock(b *testing.B) { for i := 0; i < b.N; i++ { mu.Lock() mu.Unlock() } } -func Benchmark_Sync_RLockRUnlock(b *testing.B) { +func Benchmark_RWMutex_LockUnlock(b *testing.B) { for i := 0; i < b.N; i++ { - mu.RLock() - mu.RUnlock() + rwmu.Lock() + rwmu.Unlock() + } +} + +func Benchmark_RWMutex_RLockRUnlock(b *testing.B) { + for i := 0; i < b.N; i++ { + rwmu.RLock() + rwmu.RUnlock() } } @@ -39,9 +47,21 @@ func Benchmark_GMutex_LockUnlock(b *testing.B) { } } +func Benchmark_GMutex_TryLock(b *testing.B) { + for i := 0; i < b.N; i++ { + gmu.TryLock() + } +} + func Benchmark_GMutex_RLockRUnlock(b *testing.B) { for i := 0; i < b.N; i++ { gmu.RLock() gmu.RUnlock() } } + +func Benchmark_GMutex_TryRLock(b *testing.B) { + for i := 0; i < b.N; i++ { + gmu.TryRLock() + } +}