OSDN Git Service

Do not synchronize boolean reads/writes
authorHugo Benichi <hugobenichi@google.com>
Tue, 4 Oct 2016 02:24:12 +0000 (11:24 +0900)
committerLorenzo Colitti <lorenzo@google.com>
Thu, 26 Jan 2017 09:50:34 +0000 (18:50 +0900)
commiteaa786958ffc92449a224eaa565d17d88bced615
treebc5ee963d51b75220d8326feea5ec63807e4b7f7
parent3098e4cc95a2262df63e8e3dd9af42bf334a687d
Do not synchronize boolean reads/writes

This patch removes the synchronization around the private variable
mRunning inside of IpReachabilityMonitor and instead qualifeis the field
as volatile.

Synchronization is not needed for reads/writes on native fields or
object references because they are already guaranteed to be atomic.

Synchronization here was used for enforcing memory visibility across
concurrent threads indirectly through monitor acquire/release.
The volatile keyword achieves this in a more explicit way.

Also, this patch changes the way that probeAll() copies the
IpReachabilityMonitor's mIpWatchList by temporary holding mIpWatchList
keys into an ArrayList instead of a more expensive HashSet. Since Java
HashSet are just degenerated HashMaps, and that key iteration order is
based on key hash, the iteration order over this temporary collection
will be consistent for the same mIpWatchList.

Test: refactoring CL. Existing unit tests still pass.

(cherry picked from commit b0f1186c034c4df9eb54ed29944d16ce6d7ade56)

Change-Id: I48d2b4d837a459150cd431b400ec01b87b48c014
services/net/java/android/net/ip/IpReachabilityMonitor.java