GoCollections/concurrent/Lock.go

26 lines
321 B
Go
Raw Normal View History

2022-03-23 10:15:18 +08:00
package concurrent
type (
Lock interface {
Lock()
Unlock()
}
RWLock interface {
2022-03-23 14:28:41 +08:00
Lock
2022-03-23 10:15:18 +08:00
RLock()
RUnlock()
}
)
2022-03-23 14:28:41 +08:00
func WithLock[R any](lock Lock, f func() R) R {
lock.Lock()
defer lock.Unlock()
return f()
}
func WithRLock[R any](lock RWLock, f func() R) R {
lock.RLock()
defer lock.RUnlock()
return f()
}