]> Cypherpunks repositories - gostls13.git/commit
runtime/cgo: add Handle for managing (c)go pointers
authorChangkun Ou <hi@changkun.de>
Tue, 23 Feb 2021 08:58:14 +0000 (09:58 +0100)
committerIan Lance Taylor <iant@golang.org>
Tue, 6 Apr 2021 23:39:42 +0000 (23:39 +0000)
commit972e8839254d59dc04a1193c4c9633a74595a2e7
tree51a67555636f9e9aebddb52022baba14bdefb95d
parentb084073b53bfc4236d95819a3cc34dcbb4f15392
runtime/cgo: add Handle for managing (c)go pointers

A non-trivial Cgo program may need to use callbacks and interact with
go objects per goroutine. Because of the rules for passing pointers
between Go and C, such a program needs to store handles to associated
Go values. This often causes much extra effort to figure out a way to
correctly deal with: 1) map collision; 2) identifying leaks and 3)
concurrency.

This CL implements a Handle representation in runtime/cgo package, and
related methods such as Value, Delete, etc. which allows Go users can
use a standard way to handle the above difficulties.

In addition, the CL allows a Go value to have multiple handles, and the
NewHandle always returns a different handle compare to the previously
returned handles. In comparison, CL 294670 implements a different
behavior of NewHandle that returns a unique handle when the Go value is
referring to the same object.

Benchmark:
name                      time/op
Handle/non-concurrent-16  487ns ± 1%
Handle/concurrent-16      674ns ± 1%

Fixes #37033

Change-Id: I0eadb9d44332fffef8fb567c745246a49dd6d4c1
Reviewed-on: https://go-review.googlesource.com/c/go/+/295369
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Trust: Cherry Zhang <cherryyz@google.com>
doc/go1.17.html
misc/cgo/test/cgo_test.go
misc/cgo/test/test.go
misc/cgo/test/testx.go
src/cmd/cgo/doc.go
src/cmd/link/internal/ld/lib.go
src/runtime/cgo/handle.go [new file with mode: 0644]
src/runtime/cgo/handle_test.go [new file with mode: 0644]