33#include < RNReanimated/Scheduler.h>
44#include < RNReanimated/ShareableValue.h>
55#include < RNReanimated/RuntimeManager.h>
6+ #include < RNReanimated/RuntimeDecorator.h>
7+ #include < RNReanimated/ErrorHandler.h>
8+ #include " MakeJSIRuntime.h"
69
710#define MAX_THREAD_COUNT 2
811
912namespace mrousavy {
1013namespace multithreading {
1114
12- static ThreadPool pool (MAX_THREAD_COUNT );
15+ static ThreadPool pool (1 );
16+ static std::unique_ptr<reanimated::RuntimeManager> manager;
1317
1418// reanimated::RuntimeManager manager;
1519
1620void install (jsi::Runtime& runtime) {
21+ // Quickly setup the runtime - this is executed in parallel, and _might_ introduce race conditions if spawnThread is called before this finishes.
22+ pool.enqueue ([]() {
23+ auto runtime = makeJSIRuntime ();
24+ reanimated::RuntimeDecorator::decorateRuntime (*runtime, " CUSTOM_THREAD" );
25+ manager = std::make_unique<reanimated::RuntimeManager>(std::move (runtime),
26+ std::shared_ptr<reanimated::ErrorHandler>(),
27+ std::shared_ptr<reanimated::Scheduler>());
28+ });
29+
1730 // spawnThread(run: () => T): Promise<T>
1831 auto spawnThread = jsi::Function::createFromHostFunction (runtime,
1932 jsi::PropNameID::forAscii (runtime, " spawnThread" ),
@@ -37,10 +50,11 @@ void install(jsi::Runtime& runtime) {
3750 .call (runtime, jsi::JSError (runtime, message).value ());
3851 };
3952 // TODO: Get correct RuntimeManager instance
40- auto run = reanimated::ShareableValue::adapt (runtime, arguments[0 ], nullptr );
53+ auto run = reanimated::ShareableValue::adapt (runtime, arguments[0 ], manager. get () );
4154
42- pool.enqueue ([&resolver, &rejecter, run](jsi::Runtime& runtime ) {
55+ pool.enqueue ([&resolver, &rejecter, run]() {
4356 try {
57+ auto & runtime = *manager->runtime ;
4458 auto func = run->getValue (runtime).asObject (runtime).asFunction (runtime);
4559 auto result = func.callWithThis (runtime, func);
4660
0 commit comments