Compare commits
No commits in common. "eebe3d217961dc60375b49a9b1767f6116c1a55a" and "b9369570ae5a60c739763ca80ea95c68336d76e5" have entirely different histories.
eebe3d2179
...
b9369570ae
@ -25,7 +25,7 @@ func (this *Monitor[T]) Set (value T) {
|
|||||||
// Borrow borrows the value from the Monitor, and returns a function that must
|
// Borrow borrows the value from the Monitor, and returns a function that must
|
||||||
// immediately be deferred, like this:
|
// immediately be deferred, like this:
|
||||||
//
|
//
|
||||||
// value, done := monitor.Borrow()
|
// value, done := Monitor.Borrow()
|
||||||
// defer done()
|
// defer done()
|
||||||
//
|
//
|
||||||
// From the time Borrow is called to the time the done function is called, it is
|
// From the time Borrow is called to the time the done function is called, it is
|
||||||
@ -35,19 +35,6 @@ func (this *Monitor[T]) Borrow () (T, func ()) {
|
|||||||
return this.value, this.mutex.Unlock
|
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.
|
// RWMonitor guards separate read/write access to a value.
|
||||||
type RWMonitor[T any] struct {
|
type RWMonitor[T any] struct {
|
||||||
value T
|
value T
|
||||||
@ -72,7 +59,7 @@ func (this *RWMonitor[T]) Set (value T) {
|
|||||||
// Borrow borrows the value from the Monitor for write access, and returns a
|
// Borrow borrows the value from the Monitor for write access, and returns a
|
||||||
// function that must immediately be deferred, like this:
|
// function that must immediately be deferred, like this:
|
||||||
//
|
//
|
||||||
// value, done := monitor.Borrow()
|
// value, done := Monitor.Borrow()
|
||||||
// defer done()
|
// defer done()
|
||||||
//
|
//
|
||||||
// From the time Borrow is called to the time the done function is called, it is
|
// From the time Borrow is called to the time the done function is called, it is
|
||||||
@ -82,19 +69,6 @@ func (this *RWMonitor[T]) Borrow () (T, func ()) {
|
|||||||
return this.value, this.mutex.Unlock
|
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
|
// RBorrow is like Borrow, but returns the item for read access only. Do not
|
||||||
// under any circumstances modify anything returned by this method.
|
// under any circumstances modify anything returned by this method.
|
||||||
func (this *RWMonitor[T]) RBorrow () (T, func ()) {
|
func (this *RWMonitor[T]) RBorrow () (T, func ()) {
|
||||||
|
@ -1,47 +0,0 @@
|
|||||||
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()
|
|
||||||
} ()
|
|
||||||
}
|
|
||||||
} ()
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user