From c49e33b0c3c5412e7776760fed1b8d61a4dade75 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Wed, 10 Sep 2025 20:53:44 -0400 Subject: [PATCH] sync: Make Monitor.BorrowReturn actually function as intended --- sync/monitor.go | 16 ++++++++-------- sync/monitor_test.go | 4 ++-- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/sync/monitor.go b/sync/monitor.go index 7c5804d..37bc6d0 100644 --- a/sync/monitor.go +++ b/sync/monitor.go @@ -39,12 +39,12 @@ func (this *Monitor[T]) Borrow () (T, func ()) { // 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)) { +// defer done(&value) +func (this *Monitor[T]) BorrowReturn () (T, func (*T)) { this.mutex.Lock() - return this.value, func (value T) { + return this.value, func (value *T) { 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: // // value, done := monitor.BorrowReturn() -// defer done(value) -func (this *RWMonitor[T]) BorrowReturn () (T, func (T)) { +// defer done(&value) +func (this *RWMonitor[T]) BorrowReturn () (T, func (*T)) { this.mutex.Lock() - return this.value, func (value T) { + return this.value, func (value *T) { defer this.mutex.Unlock() - this.value = value + this.value = *value } } diff --git a/sync/monitor_test.go b/sync/monitor_test.go index 1ca4c15..bafd012 100644 --- a/sync/monitor_test.go +++ b/sync/monitor_test.go @@ -13,8 +13,8 @@ func TestMonitor (test *testing.T) { } () func () { value, done := mon.BorrowReturn() + defer done(&value) value += 3 - defer done(value) } () func () { value, done := mon.Borrow() @@ -56,8 +56,8 @@ func TestRWMonitor (test *testing.T) { } () func () { value, done := mon.BorrowReturn() + defer done(&value) value += 3 - defer done(value) } () func () { value, done := mon.RBorrow()