sync: Make Monitor.BorrowReturn actually function as intended
This commit is contained in:
parent
afcc57aa70
commit
c49e33b0c3
@ -39,12 +39,12 @@ func (this *Monitor[T]) Borrow () (T, func ()) {
|
|||||||
// updated value. The intended use of this function is like this:
|
// updated value. The intended use of this function is like this:
|
||||||
//
|
//
|
||||||
// value, done := monitor.BorrowReturn()
|
// value, done := monitor.BorrowReturn()
|
||||||
// defer done(value)
|
// defer done(&value)
|
||||||
func (this *Monitor[T]) BorrowReturn () (T, func (T)) {
|
func (this *Monitor[T]) BorrowReturn () (T, func (*T)) {
|
||||||
this.mutex.Lock()
|
this.mutex.Lock()
|
||||||
return this.value, func (value T) {
|
return this.value, func (value *T) {
|
||||||
defer this.mutex.Unlock()
|
defer this.mutex.Unlock()
|
||||||
this.value = value
|
this.value = *value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -86,12 +86,12 @@ func (this *RWMonitor[T]) Borrow () (T, func ()) {
|
|||||||
// updated value. The intended use of this function is like this:
|
// updated value. The intended use of this function is like this:
|
||||||
//
|
//
|
||||||
// value, done := monitor.BorrowReturn()
|
// value, done := monitor.BorrowReturn()
|
||||||
// defer done(value)
|
// defer done(&value)
|
||||||
func (this *RWMonitor[T]) BorrowReturn () (T, func (T)) {
|
func (this *RWMonitor[T]) BorrowReturn () (T, func (*T)) {
|
||||||
this.mutex.Lock()
|
this.mutex.Lock()
|
||||||
return this.value, func (value T) {
|
return this.value, func (value *T) {
|
||||||
defer this.mutex.Unlock()
|
defer this.mutex.Unlock()
|
||||||
this.value = value
|
this.value = *value
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -13,8 +13,8 @@ func TestMonitor (test *testing.T) {
|
|||||||
} ()
|
} ()
|
||||||
func () {
|
func () {
|
||||||
value, done := mon.BorrowReturn()
|
value, done := mon.BorrowReturn()
|
||||||
|
defer done(&value)
|
||||||
value += 3
|
value += 3
|
||||||
defer done(value)
|
|
||||||
} ()
|
} ()
|
||||||
func () {
|
func () {
|
||||||
value, done := mon.Borrow()
|
value, done := mon.Borrow()
|
||||||
@ -56,8 +56,8 @@ func TestRWMonitor (test *testing.T) {
|
|||||||
} ()
|
} ()
|
||||||
func () {
|
func () {
|
||||||
value, done := mon.BorrowReturn()
|
value, done := mon.BorrowReturn()
|
||||||
|
defer done(&value)
|
||||||
value += 3
|
value += 3
|
||||||
defer done(value)
|
|
||||||
} ()
|
} ()
|
||||||
func () {
|
func () {
|
||||||
value, done := mon.RBorrow()
|
value, done := mon.RBorrow()
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user