From 9a507b54d712d0140b074aab31f8d0485d354d6b Mon Sep 17 00:00:00 2001 From: John Date: Sat, 22 Jun 2019 11:45:58 +0800 Subject: [PATCH] add Remove/Clear functions for gmlock --- g/os/gmlock/gmlock.go | 5 +++++ g/os/gmlock/gmlock_locker.go | 13 ++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/g/os/gmlock/gmlock.go b/g/os/gmlock/gmlock.go index dcfb0c557..a6baa38ee 100644 --- a/g/os/gmlock/gmlock.go +++ b/g/os/gmlock/gmlock.go @@ -82,3 +82,8 @@ func TryLockFunc(key string, f func()) bool { func TryRLockFunc(key string, f func()) bool { return locker.TryRLockFunc(key, f) } + +// Remove removes mutex with given . +func Remove(key string) { + locker.Remove(key) +} diff --git a/g/os/gmlock/gmlock_locker.go b/g/os/gmlock/gmlock_locker.go index 8041fd270..ce00c132d 100644 --- a/g/os/gmlock/gmlock_locker.go +++ b/g/os/gmlock/gmlock_locker.go @@ -12,7 +12,8 @@ import ( ) // Memory locker. -// Note that there's no cache expire mechanism for attribute map . +// Note that there's no cache expire mechanism for mutex in locker. +// You need remove certain mutex manually when you do not want use it any more. type Locker struct { m *gmap.StrAnyMap } @@ -113,6 +114,16 @@ func (l *Locker) TryRLockFunc(key string, f func()) bool { return false } +// Remove removes mutex with given from locker. +func (l *Locker) Remove(key string) { + l.m.Remove(key) +} + +// Clear removes all mutexes from locker. +func (l *Locker) Clear() { + l.m.Clear() +} + // getOrNewMutex returns the mutex of given if it exists, // or else creates and returns a new one. func (l *Locker) getOrNewMutex(key string) *gmutex.Mutex {