go/misc/cgo
Changkun Ou 972e883925 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>
2021-04-06 23:39:42 +00:00
..
errors cmd/cgo: pass end position info for C function arguments. 2021-04-05 19:29:17 +00:00
fortran misc/cgo/fortran: avoid writing to $PWD 2019-11-20 22:37:23 +00:00
gmp
life all: update references to symbols moved from io/ioutil to io 2021-04-05 17:51:15 +00:00
nocgo
stdio all: update references to symbols moved from io/ioutil to io 2021-04-05 17:51:15 +00:00
test runtime/cgo: add Handle for managing (c)go pointers 2021-04-06 23:39:42 +00:00
testasan misc: log 'ok' from 'go run' tests on success 2019-11-25 16:43:27 +00:00
testcarchive all: update references to symbols moved from io/ioutil to io 2021-04-05 17:51:15 +00:00
testcshared all: update references to symbols moved from io/ioutil to io 2021-04-05 17:51:15 +00:00
testgodefs all: update references to symbols moved from io/ioutil to io 2021-04-05 17:51:15 +00:00
testplugin all: update references to symbols moved from io/ioutil to io 2021-04-05 17:51:15 +00:00
testsanitizers all: update references to symbols moved from io/ioutil to io 2021-04-05 17:51:15 +00:00
testshared all: update references to symbols moved from io/ioutil to io 2021-04-05 17:51:15 +00:00
testsigfwd misc: log 'ok' from 'go run' tests on success 2019-11-25 16:43:27 +00:00
testso all: update references to symbols moved from io/ioutil to io 2021-04-05 17:51:15 +00:00
testsovar all: update references to symbols moved from io/ioutil to io 2021-04-05 17:51:15 +00:00
testtls