[mirror] Go on Mobile https://golang.org/x/mobile
Go to file
Hyang-Ah (Hana) Kim c533dca65f bind/java: manage Java object lifetime based on reference count.
The gobind framework is supposed to use reference counting to
keep track of objects (e.g. pointer to a Go struct, interface
values) crossing the language boundary. This change fixes two bugs:

1) no reference counting on Java object: Previously, the lifetime
of a Java object was manages in the following way.

 a. The Java object is pinned in an internal map (javaObjs) when it's
constructed.
 b. When Go receives the reference to the Java object, it creates a
proxy object and sets a finalizer on it. The finalizer signals Java
to unpin the Java object (remove from the javaObjs map).
 c. The javaObjs map is also used to identify the Java object when
Go asks to invoke a method on it later.

When the same Java object is sent to Java more than once, and the
finalizer (b) runs after the first use, the second use of the Java
object can cause the crash described in golang/go#10933.

This change fixes the bug by reference counting the Java object.
Java side pins the Java object and increments the refcount whenever it
sees the object sent to Go (in Seq.writeRef). When the Go proxy
object's finalizer runs, the refcount is decremented. When the refcount
becomes 0, the object gets unpined.

2) race in Go object lifetime management: Pinning on a Go object
has been done when the Go object is sent to Java but the Go object
is not in the pinned object map yet. (bind/seq.WriteGoRef).
Unpinning the object occurs when Java finds there are no proxy objects
on its side. For this, Java maintains a reference count map (goObjs).
When the refcount becomes zero, Java notifies Go so the object is
unpinned. Here is a race case:

 a. Java has a proxy object for a Go object.
 b. Go is preparing for sending the same Go object. seq.WriteGoRef
notices the corresponding entry in the pinned object map already,
and returns. The remaining work for sending the object continues.
 c. The proxy object in Java finalizes and triggers deletion of the
object from the pinned object map.
 d. The remaining work for (b) completes and Java creates a new proxy
object. When a method is called for the Go object, the Go object is
already removed from the object map on Go side and maybe already GC'd.

This change fixes it by converting the pinned object map to reference
counter map maintained in Go. The counter increments for each
seq.WriteGoRef call. The finalizer of the proxy object in Java causes
a decrement of the counter.

Fixes golang/go#10933.

Renables the skipped testJavaRefGC.

Change-Id: I0992e002b1050b6183689e5ab821e058adbb420f
Reviewed-on: https://go-review.googlesource.com/10638
Reviewed-by: David Crawshaw <crawshaw@golang.org>
2015-06-04 16:53:15 +00:00
app app: fix bug on linux/x11 2015-06-03 14:42:55 +00:00
audio mobile/audio: document WAV header size and its format 2015-05-08 23:40:00 +00:00
bind bind/java: manage Java object lifetime based on reference count. 2015-06-04 16:53:15 +00:00
build build/androidtest.bash: script to run all tests in a repository on an 2014-12-18 00:48:16 +00:00
cmd cmd/gomobile: set tmpdir when calling cmd/dist 2015-05-28 21:03:30 +00:00
event mobile/app: identify touches by ID to enable multitouch 2015-04-29 16:53:16 +00:00
example mobile/audio: determine the format and sample rate from the WAV format 2015-05-08 15:51:42 +00:00
f32 mobile: add import comments. 2014-12-10 01:59:04 +00:00
font mobile: add missing canonical import paths 2015-05-05 03:13:45 +00:00
geom app: introduce Config and start registration 2015-05-06 17:32:53 +00:00
gl gl: batch calls onto a dedicated context thread 2015-06-01 15:35:03 +00:00
sensor mobile: add missing canonical import paths 2015-05-05 03:13:45 +00:00
sprite sprite/glsprite: add very basic package docs 2014-12-12 14:44:04 +00:00
testdata example/sprite: make example lives 2014-12-22 13:10:48 +00:00
.gitattributes mobile: copying .gitattributes to all subrepositories 2014-12-23 23:23:25 +00:00
.gitignore convert .hgignore to .gitignore. 2014-12-08 10:43:03 +11:00
AUTHORS go.empty: prototype for new subrepository 2012-01-25 14:45:13 -05:00
CONTRIBUTING.md doc: add CONTRIBUTING.md 2015-02-18 23:42:31 +00:00
CONTRIBUTORS go.empty: prototype for new subrepository 2012-01-25 14:45:13 -05:00
Dockerfile Dockerfile: update go version to 3f4de49. 2015-04-20 17:34:49 +00:00
LICENSE LICENSE: add 2012-03-17 15:20:58 +11:00
PATENTS go.empty: add PATENTS file to the subrepo. 2012-04-16 11:24:04 +10:00
README.md mobile: add instructions about filing an issue and discussing FRs 2015-05-11 21:43:06 +00:00
codereview.cfg mobile: add codereview.cfg 2015-03-18 17:03:44 +00:00

README.md

Go support for Mobile devices

The Go mobile repository holds packages and build tools for using Go on Android.

This is early work and installing the build system requires Go 1.5. Follow the build instructions on godoc.org/golang.org/x/mobile/cmd/gomobile to install the gomobile command and build the basic example.

--

APIs are currently very limited, but under active development. Package documentation serves as a starting point:

Contributions to Go are appreciated. See https://golang.org/doc/contribute.html.