GoCollections/concurrent/Lock.go

32 lines
480 B
Go
Raw Normal View History

2022-11-25 18:19:33 +08:00
/*
* Copyright (c) 2022 tursom. All rights reserved.
* Use of this source code is governed by a GPL-3
* license that can be found in the LICENSE file.
*/
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()
}