1
0
mirror of https://github.com/MontFerret/ferret.git synced 2024-12-14 11:23:02 +02:00
ferret/pkg/drivers/common/atomic.go
Tim Voronov a5cbdb435c
Bugfix/#332 nav deadlock (#333)
* Added failing e2e test

* Fixed deadlock on navigation

* Removed filter for e2e tests

* Updated method name in LazyValue struct

* Custom atomic value

* Fixed linting issue

* Updated comments
2019-07-16 10:34:13 -04:00

53 lines
1.1 KiB
Go

package common
import (
"github.com/MontFerret/ferret/pkg/runtime/core"
"sync"
)
type (
// AtomicValueWriter represents an atomic value writer
AtomicValueWriter func(current core.Value) (core.Value, error)
// AtomicValue represents an atomic value
AtomicValue struct {
mu sync.Mutex
value core.Value
}
)
func NewAtomicValue(value core.Value) *AtomicValue {
av := new(AtomicValue)
av.value = value
return av
}
// Read returns an underlying value.
// @returns (Value) - Underlying value
func (av *AtomicValue) Read() core.Value {
av.mu.Lock()
defer av.mu.Unlock()
return av.value
}
// Write sets a new underlying value.
// If writer fails, the operations gets terminated and an underlying value remains.
// @param (AtomicValueWriter) - Writer function that receives a current value and returns new one.
// @returns (Error) - Error if write operation failed
func (av *AtomicValue) Write(writer AtomicValueWriter) error {
av.mu.Lock()
defer av.mu.Unlock()
next, err := writer(av.value)
if err != nil {
return err
}
av.value = next
return nil
}