Compare commits

...

2 Commits

Author SHA1 Message Date
eebe3d2179 sync: Add monitor tests 2024-12-29 02:32:27 -05:00
6e902df516 sync: Add Monitor.BorrowReturn 2024-12-29 02:32:13 -05:00
2 changed files with 75 additions and 2 deletions

View File

@ -25,7 +25,7 @@ func (this *Monitor[T]) Set (value T) {
// Borrow borrows the value from the Monitor, and returns a function that must
// immediately be deferred, like this:
//
// value, done := Monitor.Borrow()
// value, done := monitor.Borrow()
// defer done()
//
// From the time Borrow is called to the time the done function is called, it is
@ -35,6 +35,19 @@ func (this *Monitor[T]) Borrow () (T, func ()) {
return this.value, this.mutex.Unlock
}
// BorrowReturn is like borrow, but returns a "done" function that takes in an
// updated value. The intended use of this function is like this:
//
// value, done := monitor.BorrowReturn()
// defer done(value)
func (this *Monitor[T]) BorrowReturn () (T, func (T)) {
this.mutex.Lock()
return this.value, func (value T) {
defer this.mutex.Unlock()
this.value = value
}
}
// RWMonitor guards separate read/write access to a value.
type RWMonitor[T any] struct {
value T
@ -59,7 +72,7 @@ func (this *RWMonitor[T]) Set (value T) {
// Borrow borrows the value from the Monitor for write access, and returns a
// function that must immediately be deferred, like this:
//
// value, done := Monitor.Borrow()
// value, done := monitor.Borrow()
// defer done()
//
// From the time Borrow is called to the time the done function is called, it is
@ -69,6 +82,19 @@ func (this *RWMonitor[T]) Borrow () (T, func ()) {
return this.value, this.mutex.Unlock
}
// BorrowReturn is like borrow, but returns a "done" function that takes in an
// updated value. The intended use of this function is like this:
//
// value, done := monitor.BorrowReturn()
// defer done(value)
func (this *RWMonitor[T]) BorrowReturn () (T, func (T)) {
this.mutex.Lock()
return this.value, func (value T) {
defer this.mutex.Unlock()
this.value = value
}
}
// RBorrow is like Borrow, but returns the item for read access only. Do not
// under any circumstances modify anything returned by this method.
func (this *RWMonitor[T]) RBorrow () (T, func ()) {

47
sync/monitor_test.go Normal file
View File

@ -0,0 +1,47 @@
package usync
import "testing"
import "math/rand"
func TestMonitor (test *testing.T) {
mon := NewMonitor(9)
func () {
value, done := mon.Borrow()
defer done()
test.Log(value)
if value != 9 { test.Fatal("not equal") }
} ()
func () {
value, done := mon.BorrowReturn()
value += 3
defer done(value)
} ()
func () {
value, done := mon.Borrow()
defer done()
test.Log(value)
if value != 12 { test.Fatal("not equal") }
} ()
mon.Set(11)
func () {
value, done := mon.Borrow()
defer done()
test.Log(value)
if value != 11 { test.Fatal("not equal") }
} ()
}
func TestMonitorConcurrent (test *testing.T) {
mon := NewMonitor(map[int] int { })
for index := 0; index < 16; index ++ {
go func () {
for index := 0; index < 8192; index ++ {
func () {
value, done := mon.Borrow()
defer done()
value[rand.Int()] = rand.Int()
} ()
}
} ()
}
}