From 3503aa43b48bcd5c379928439dfdbfb2503ea2c0 Mon Sep 17 00:00:00 2001 From: John Date: Wed, 23 Jan 2019 13:30:46 +0800 Subject: [PATCH] gtimer, gmlock updates --- g/os/gmlock/gmlock_unit_lock_test.go | 36 +++++++++++++++------------ g/os/gmlock/gmlock_unit_rlock_test.go | 18 ++++++++------ g/os/gtimer/gtimer_timer.go | 12 ++++----- 3 files changed, 36 insertions(+), 30 deletions(-) diff --git a/g/os/gmlock/gmlock_unit_lock_test.go b/g/os/gmlock/gmlock_unit_lock_test.go index b190ed7a6..ced3a201a 100644 --- a/g/os/gmlock/gmlock_unit_lock_test.go +++ b/g/os/gmlock/gmlock_unit_lock_test.go @@ -16,20 +16,22 @@ import ( func TestLocker_Lock_Unlock(t *testing.T) { gtest.Case(t, func() { + key := "test1" array := garray.New(0, 0) go func() { - gmlock.Lock("test") + gmlock.Lock(key) array.Append(1) time.Sleep(100*time.Millisecond) array.Append(1) - gmlock.Unlock("test") + gmlock.Unlock(key) }() go func() { - gmlock.Lock("test") + time.Sleep(10*time.Millisecond) + gmlock.Lock(key) array.Append(1) time.Sleep(200*time.Millisecond) array.Append(1) - gmlock.Unlock("test") + gmlock.Unlock(key) }() time.Sleep(50*time.Millisecond) gtest.Assert(array.Len(), 1) @@ -44,48 +46,50 @@ func TestLocker_Lock_Unlock(t *testing.T) { func TestLocker_Lock_Expire(t *testing.T) { gtest.Case(t, func() { + key := "test2" array := garray.New(0, 0) go func() { - gmlock.Lock("test", 50*time.Millisecond) + gmlock.Lock(key, 100*time.Millisecond) array.Append(1) }() go func() { time.Sleep(10*time.Millisecond) - gmlock.Lock("test") + gmlock.Lock(key) time.Sleep(100*time.Millisecond) array.Append(1) - gmlock.Unlock("test") + gmlock.Unlock(key) }() - time.Sleep(80*time.Millisecond) + time.Sleep(150*time.Millisecond) gtest.Assert(array.Len(), 1) - time.Sleep(80*time.Millisecond) + time.Sleep(250*time.Millisecond) gtest.Assert(array.Len(), 2) }) } func TestLocker_TryLock_Expire(t *testing.T) { gtest.Case(t, func() { + key := "test3" array := garray.New(0, 0) go func() { - gmlock.Lock("test", 200*time.Millisecond) + gmlock.Lock(key, 200*time.Millisecond) array.Append(1) }() go func() { - time.Sleep(50*time.Millisecond) - if !gmlock.TryLock("test") { + time.Sleep(100*time.Millisecond) + if !gmlock.TryLock(key) { array.Append(1) } else { - gmlock.Unlock("test") + gmlock.Unlock(key) } }() go func() { time.Sleep(300*time.Millisecond) - if gmlock.TryLock("test") { + if gmlock.TryLock(key) { array.Append(1) - gmlock.Unlock("test") + gmlock.Unlock(key) } }() - time.Sleep(20*time.Millisecond) + time.Sleep(50*time.Millisecond) gtest.Assert(array.Len(), 1) time.Sleep(80*time.Millisecond) gtest.Assert(array.Len(), 2) diff --git a/g/os/gmlock/gmlock_unit_rlock_test.go b/g/os/gmlock/gmlock_unit_rlock_test.go index a5eb25615..ffd210868 100644 --- a/g/os/gmlock/gmlock_unit_rlock_test.go +++ b/g/os/gmlock/gmlock_unit_rlock_test.go @@ -16,19 +16,20 @@ import ( func TestLocker_RLock1(t *testing.T) { gtest.Case(t, func() { + key := "test100" array := garray.New(0, 0) go func() { - gmlock.RLock("test") + gmlock.RLock(key) array.Append(1) time.Sleep(50*time.Millisecond) array.Append(1) - gmlock.RUnlock("test") + gmlock.RUnlock(key) }() go func() { time.Sleep(10*time.Millisecond) - gmlock.Lock("test") + gmlock.Lock(key) array.Append(1) - gmlock.Unlock("test") + gmlock.Unlock(key) }() time.Sleep(20*time.Millisecond) gtest.Assert(array.Len(), 1) @@ -39,18 +40,19 @@ func TestLocker_RLock1(t *testing.T) { func TestLocker_RLock2(t *testing.T) { gtest.Case(t, func() { + key := "test200" array := garray.New(0, 0) go func() { - gmlock.Lock("test") + gmlock.Lock(key) array.Append(1) time.Sleep(100*time.Millisecond) - gmlock.Unlock("test") + gmlock.Unlock(key) }() go func() { time.Sleep(10*time.Millisecond) - gmlock.RLock("test") + gmlock.RLock(key) array.Append(1) - gmlock.RUnlock("test") + gmlock.RUnlock(key) }() time.Sleep(20*time.Millisecond) diff --git a/g/os/gtimer/gtimer_timer.go b/g/os/gtimer/gtimer_timer.go index feaaa9aef..9d7d01753 100644 --- a/g/os/gtimer/gtimer_timer.go +++ b/g/os/gtimer/gtimer_timer.go @@ -80,27 +80,27 @@ func (t *Timer) newWheel(level int, slot int, interval time.Duration) *wheel { // 添加循环任务 func (t *Timer) Add(interval time.Duration, job JobFunc) *Entry { - return t.doAddEntry(interval, job, false, gDEFAULT_TIMES, STATUS_READY, nil) + return t.doAddEntry(interval, job, false, gDEFAULT_TIMES, STATUS_READY) } // 添加定时任务 func (t *Timer) AddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry { - return t.doAddEntry(interval, job, singleton, times, STATUS_READY, nil) + return t.doAddEntry(interval, job, singleton, times, status) } // 添加单例运行循环任务 func (t *Timer) AddSingleton(interval time.Duration, job JobFunc) *Entry { - return t.doAddEntry(interval, job, true, gDEFAULT_TIMES, STATUS_READY, nil) + return t.doAddEntry(interval, job, true, gDEFAULT_TIMES, STATUS_READY) } // 添加只运行一次的循环任务 func (t *Timer) AddOnce(interval time.Duration, job JobFunc) *Entry { - return t.doAddEntry(interval, job, true, 1, STATUS_READY, nil) + return t.doAddEntry(interval, job, true, 1, STATUS_READY) } // 添加运行指定次数的循环任务。 func (t *Timer) AddTimes(interval time.Duration, times int, job JobFunc) *Entry { - return t.doAddEntry(interval, job, true, times, STATUS_READY, nil) + return t.doAddEntry(interval, job, true, times, STATUS_READY) } // 延迟添加循环任务。 @@ -154,7 +154,7 @@ func (t *Timer) Close() { } // 添加定时任务 -func (t *Timer) doAddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int, parent *Entry) *Entry { +func (t *Timer) doAddEntry(interval time.Duration, job JobFunc, singleton bool, times int, status int) *Entry { return t.wheels[t.getLevelByIntervalMs(interval.Nanoseconds()/1e6)].addEntry(interval, job, singleton, times, status) }