GoCollections/concurrent/Lock.go
2023-04-19 21:08:12 +08:00

34 lines
515 B
Go

/*
* 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.
*/
package concurrent
type (
Lock interface {
Lock()
TryLock() bool
Unlock()
}
RWLock interface {
Lock
RLock()
TryRLock() bool
RUnlock()
}
)
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()
}