Page MenuHomePhabricator

Introduce utility function to temporarily attach thread to JVM
ClosedPublic

Authored by marcin on Aug 26 2022, 4:47 AM.
Tags
None
Referenced Files
Unknown Object (File)
Tue, Dec 3, 5:45 AM
Unknown Object (File)
Nov 14 2024, 2:46 PM
Unknown Object (File)
Nov 13 2024, 10:44 PM
Unknown Object (File)
Nov 9 2024, 7:40 AM
Unknown Object (File)
Nov 8 2024, 1:38 PM
Unknown Object (File)
Oct 30 2024, 9:11 AM
Unknown Object (File)
Oct 23 2024, 9:59 AM
Unknown Object (File)
Oct 23 2024, 9:59 AM

Details

Summary

This differential introduces utility function to run a block of code on an auxiliary thread if native api needs to be available. Auxiliary threads are not by default attached to JVM, so we cannot call for instance CommSecureStore or PlatformSpecificTools from them. This function allows to temporarily attach a thread to JVM and execute block of code represented by lambda. Once the block is done thread is detached from JVM. Attaching thread is fast operation according to documentation.

Test Plan

Write a noop function in SQLiteQueryEXecutor that accesses some native Android API. Schedule it on databaseThread in CommCoreModule. Build Android app, run it and see that it crashes with error "Unable to retrieve JNI Environment. Is the thread attached?". Now re-implement this function to use run_with_native_accessible. Build and run the app. It should not crash.

Diff Detail

Repository
rCOMM Comm
Lint
Lint Not Applicable
Unit
Tests Not Applicable

Event Timeline

Update commit message to link to diff

Could you share the documentation that was mentioned?

This revision is now accepted and ready to land.Aug 30 2022, 3:39 AM
In D4957#144795, @tomek wrote:

Could you share the documentation that was mentioned?

Sure, it starts from this line: https://github.com/facebookincubator/fbjni/blob/main/cxx/fbjni/detail/Environment.h#L94 and continues until ThreadScope class definition.