GODT-1657: Stable sync (still needs more tests)

This commit is contained in:
James Houlahan
2022-10-01 23:14:42 +02:00
parent 705875cff2
commit 9d69a2e565
34 changed files with 1270 additions and 1099 deletions

49
internal/safe/type.go Normal file
View File

@ -0,0 +1,49 @@
package safe
import "sync"
type Type[T any] struct {
data T
lock sync.RWMutex
}
func NewType[T any](data T) *Type[T] {
return &Type[T]{
data: data,
}
}
func (s *Type[T]) Get(fn func(data T)) {
s.lock.RLock()
defer s.lock.RUnlock()
fn(s.data)
}
func (s *Type[T]) GetErr(fn func(data T) error) error {
s.lock.RLock()
defer s.lock.RUnlock()
return fn(s.data)
}
func (s *Type[T]) Set(data T) {
s.lock.Lock()
defer s.lock.Unlock()
s.data = data
}
func GetType[T, Ret any](s *Type[T], fn func(data T) Ret) Ret {
s.lock.RLock()
defer s.lock.RUnlock()
return fn(s.data)
}
func GetTypeErr[T, Ret any](s *Type[T], fn func(data T) (Ret, error)) (Ret, error) {
s.lock.RLock()
defer s.lock.RUnlock()
return fn(s.data)
}