Fix race condition in EventDispatcher

Summary:
`mRCTEventEmitter` is used by 2 different threads. It's assigned on the UI thread and it's accessed on the JavaScript thread. Currently, it can be the case that the UI thread assigns `mRCTEventEmitter` and later the JS thread accesses it but still sees null.

This change fixes the issue by marking the `mRCTEventEmitter` variable as `volatile` to ensure that both threads see the same value for `mRCTEventEmitter`.

**Test plan (required)**

This change is currently used in my team's app. We're no longer seeing a crash in `EventDispatcher`.

Adam Comella
Microsoft Corp.
Closes https://github.com/facebook/react-native/pull/9655

Differential Revision: D3790888

Pulled By: andreicoman11

fbshipit-source-id: 68cdbc74faffb36dc2bca8ad3d4a78929badbe9c
This commit is contained in:
Adam Comella 2016-08-30 04:12:42 -07:00 committed by Facebook Github Bot 4
parent 6cf7900030
commit fd3484481a

View File

@ -95,7 +95,7 @@ public class EventDispatcher implements LifecycleEventListener {
private Event[] mEventsToDispatch = new Event[16];
private int mEventsToDispatchSize = 0;
private @Nullable RCTEventEmitter mRCTEventEmitter;
private volatile @Nullable RCTEventEmitter mRCTEventEmitter;
private final ScheduleDispatchFrameCallback mCurrentFrameCallback;
private short mNextEventTypeId = 0;
private volatile boolean mHasDispatchScheduled = false;