Commit Graph
8 Commits
Author SHA1 Message Date
cd3d48677e fix: Synchronize C++ Promise<T> state-inspection accessors (#1398)
* fix(core): synchronize C++ `Promise<T>` state-inspection accessors

`resolve()`/`reject()` write `_state` under `_mutex`, but `isPending`/
`isResolved`/`isRejected`/`getResult`/`getError` read it lock-free.
`JSIConverter<shared_ptr<Promise<T>>>::toJSI` runs those reads on the JS thread
the instant a hybrid method returns, racing an off-JS-thread `resolve()`
(`Promise::async` / `Promise.parallel` / a main-thread resolve). Data race on the
`std::variant _state`, flagged by ThreadSanitizer.

Make `_mutex` mutable and acquire it in the inspection accessors. The lock-free
bodies move to private overloads that take a `unique_lock` (the caller proves it
holds the lock), so `getResult()`/`isPending()`-style call sites stay intact
without recursive self-locking. `resolve`/`reject` set the state and drain the
listeners under a single lock via `takeResolvedListeners(lock)`/
`takeRejectedListeners(lock)`, then fire them outside the lock; the `NITRO_DEBUG`
state assert runs just before the lock. Same treatment for `Promise<void>`.

* refactor(core): address review feedback on Promise synchronization

- Remove takeResolvedListenersAndClearAllListeners / takeRejectedListenersAndClearAllListeners helpers; inline as std::move + clearAllListeners
- Restore readable if/else structure in addOnXxxListener, using lock.unlock() before invoking the callback instead of nested RAII blocks with early returns
- Capture result pointer inside the lock in resolve() to avoid a second lock acquisition

* refactor(core): drop listeners outside lock by moving instead of clearing

* Update packages/react-native-nitro-modules/cpp/core/Promise.hpp

Co-authored-by: Marc Rousavy <me@mrousavy.com>

* refactor(core): remove redundant inline comments from listener methods

* fix(core): fix rebase artifact in resolve(const TResult&) — remove duplicate declaration and stale clearAllListeners

* refactor(core): remove remaining inline comment from addOnRejectedListener

* refactor(core): restore inline comments in addOnXxxListener per reviewer

---------

Co-authored-by: Miklós Fazekas <boga@MBPM5P.local>
Co-authored-by: Marc Rousavy <me@mrousavy.com>
2026-06-22 15:13:29 +02:00
Marc RousavyandGitHub ab2a05085f fix: Some code cleanups (atomic<bool>, move assignment, const) (#1269)
* fix: Make more stuff `const`

* fix: Add move assignment operator for `WeakReference`

* fix: Use `atomic<bool>` for `ReferenceState`
2026-03-18 12:09:26 +01:00
Marc RousavyandGitHub 1a61db1e0b chore: Move Promise<void> implementation to .cpp (#982)
* chore: Move `Promise<void>` implementation to .cpp

* tidy up

* Update Promise.hpp

* fix: Specify `<void>` template

* fix: Avoid inline
2025-10-24 10:31:49 +02:00
Marc RousavyandGitHub 8832bc5225 feat: Add new Promise<T> implementation for C++ (#343)
* feat: Create `Promise.hpp`

* feat: Add `JSIConverter+Exception`

* feat: Base for `Promise` and `Exception`

* fix: Implement Exception conversion

* Implement `Promise::awaitFuture`

* Revive `+Future`

* fix: Speed up creating `ThreadPool`

* fix: Make `+Future` also just use `+Promise` impl

* fix: Thread pool has 10 threads now.

* feat: Allow `ThreadPool` to grow dynamically

* chore: Format C++
2024-11-19 00:01:46 +01:00
Marc Rousavy 2bc02c0f6f A ton of improvements around jsi::Runtime safe code. 2024-06-20 16:20:13 +02:00
Marc Rousavy 84b995e4eb Rename to .hpp 2024-06-20 10:41:09 +02:00
Marc Rousavy 0da06a7efb perf: Use get (assert) instead of as for faster perf! 2024-06-19 20:01:35 +02:00
Marc Rousavy ee1b3bd2cb Add core HybridObject infra 2024-06-19 19:57:47 +02:00