-
Notifications
You must be signed in to change notification settings - Fork 505
fix(ohos): prevent duplicate initialization of NativeDisplayLink threadsafe function. #3191
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(ohos): prevent duplicate initialization of NativeDisplayLink threadsafe function. #3191
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #3191 +/- ##
==========================================
- Coverage 77.23% 77.12% -0.12%
==========================================
Files 413 413
Lines 21999 21999
Branches 6283 6283
==========================================
- Hits 16991 16966 -25
- Misses 3799 3807 +8
- Partials 1209 1226 +17 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
07e3e8b to
1f2457d
Compare
这里的逻辑有些问题,NativeDisplayLink::InitThreadSafeFunction 只需要执行一次获取到 js_threadsafe_function 就可以,js_threadsafe_function 一旦创建,只有当生命周期结束的时候才会去释放,这里不应该存在多线程并发访问的问题,核心点在于保证 NativeDisplayLink::InitThreadSafeFunction 只执行一次吧 |
|
@kevingpqi123 js_threadsafe_function 创建是和 env 关联的。但是每次 so 的入口函数都会传入一个 env。如果鸿蒙能保证每次初始化传入的 env 都是同一个话,那确实只需要保证创建一次就行了 |
这里 env 的作用就是创建 js_threadsafe_function, 用于创建时绑定 JS 环境,当创建成功后就可以跨线程调用, 后续不再需要创建, napi_call_threadsafe_function 使用的过程中和创建 js_threadsafe_function 时的 env 无关 |
1f2457d to
5ebf49a
Compare
|
@kevingpqi123 重新修改了,并使用真机设备测试了页面切换构造多次执行 so 入口函数的场景。 |
5ebf49a to
cdc4ef9
Compare
|
可以看下这种实现: bool NativeDisplayLink::InitThreadSafeFunction(napi_env env) { 只初始化一次,天然线程安全,不需要 atomic 操作。 |
|
已修改为 std::call_once 实现 |

问题背景:
XComponent组件每创建一个就会调用 so 的入口函数,对应的NativeDisplayLink::InitThreadSafeFunction通过全局变量保存 js_threadsafe_functionVSync回调在独立线程执行,而NativeDisplayLink::InitThreadSafeFunction和析构函数在ArkTS(UI)线程执行