-
Notifications
You must be signed in to change notification settings - Fork 0
/
mutex.go
36 lines (31 loc) · 885 Bytes
/
mutex.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package mtxmap
import (
"sync"
"sync/atomic"
"time"
)
// mutexEntity a custom sync.Mutex has attributes lastAccess and count
// lastAccess is last time when that mutex is used
// count total number of goroutines waiting/using that mutex
type mutexEntity struct {
sync.Mutex
lastAccess time.Time
count uint64
}
// lock increments count and locks mutex
func (m *mutexEntity) lock() {
atomic.AddUint64(&m.count, 1)
m.Lock()
}
// unlock set lastAccess, unlocks mutex, and decrement count
func (m *mutexEntity) unlock() {
m.lastAccess = time.Now()
m.Unlock()
atomic.AddUint64(&m.count, ^uint64(0))
}
// isExpire if count is 0 (zero) and lastAccess + ttl is before time.Now returns true
// otherwise return false
func (m *mutexEntity) isExpire(ttl time.Duration) bool {
return atomic.CompareAndSwapUint64(&m.count, 0, 0) &&
m.lastAccess.Add(ttl).Before(time.Now())
}