Index: third_party/mojo/src/mojo/public/go/bindings/async_waiter.go |
diff --git a/third_party/mojo/src/mojo/public/go/bindings/async_waiter.go b/third_party/mojo/src/mojo/public/go/bindings/async_waiter.go |
index ebe42cc57aa329ce4f50617c6cb40b234b794e1b..bcd4a7cd3e2223944949d96a114d31237245896c 100644 |
--- a/third_party/mojo/src/mojo/public/go/bindings/async_waiter.go |
+++ b/third_party/mojo/src/mojo/public/go/bindings/async_waiter.go |
@@ -6,6 +6,7 @@ package bindings |
import ( |
"fmt" |
+ "runtime" |
"sync" |
"sync/atomic" |
@@ -86,7 +87,7 @@ type asyncWaiterWorker struct { |
isNotified *int32 |
waitChan <-chan waitRequest // should have a non-empty buffer |
cancelChan <-chan AsyncWaitId // should have a non-empty buffer |
- ids Counter // is incremented each |AsyncWait()| call |
+ ids uint64 // is incremented each |AsyncWait()| call |
} |
// removeHandle removes handle at provided index without sending response by |
@@ -143,7 +144,8 @@ func (w *asyncWaiterWorker) processIncomingRequests() { |
w.signals = append(w.signals, request.signals) |
w.responses = append(w.responses, request.responseChan) |
- id := AsyncWaitId(w.ids.Next()) |
+ w.ids++ |
+ id := AsyncWaitId(w.ids) |
w.asyncWaitIds = append(w.asyncWaitIds, id) |
request.idChan <- id |
case AsyncWaitId := <-w.cancelChan: |
@@ -211,6 +213,15 @@ type asyncWaiterImpl struct { |
cancelChan chan<- AsyncWaitId // should have a non-empty buffer |
} |
+func finalizeWorker(worker *asyncWaiterWorker) { |
+ // Close waking handle on worker side. |
+ worker.handles[0].Close() |
+} |
+ |
+func finalizeAsyncWaiter(waiter *asyncWaiterImpl) { |
+ waiter.wakingHandle.Close() |
+} |
+ |
// newAsyncWaiter creates an asyncWaiterImpl and starts its worker goroutine. |
func newAsyncWaiter() *asyncWaiterImpl { |
result, h0, h1 := system.GetCore().CreateMessagePipe(nil) |
@@ -220,7 +231,7 @@ func newAsyncWaiter() *asyncWaiterImpl { |
waitChan := make(chan waitRequest, 10) |
cancelChan := make(chan AsyncWaitId, 10) |
isNotified := new(int32) |
- worker := asyncWaiterWorker{ |
+ worker := &asyncWaiterWorker{ |
[]system.Handle{h1}, |
[]system.MojoHandleSignals{system.MOJO_HANDLE_SIGNAL_READABLE}, |
[]AsyncWaitId{0}, |
@@ -228,15 +239,18 @@ func newAsyncWaiter() *asyncWaiterImpl { |
isNotified, |
waitChan, |
cancelChan, |
- Counter{}, |
+ 0, |
} |
+ runtime.SetFinalizer(worker, finalizeWorker) |
go worker.runLoop() |
- return &asyncWaiterImpl{ |
+ waiter := &asyncWaiterImpl{ |
wakingHandle: h0, |
isWorkerNotified: isNotified, |
waitChan: waitChan, |
cancelChan: cancelChan, |
} |
+ runtime.SetFinalizer(waiter, finalizeAsyncWaiter) |
+ return waiter |
} |
// wakeWorker wakes the worker from |WaitMany()| call. This should be called |