Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions packages/webgpu/android/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ find_package(fbjni REQUIRED CONFIG)

add_library(${PACKAGE_NAME} SHARED
./cpp/cpp-adapter.cpp
./cpp/platform/ThreadUtils.cpp
../cpp/rnwgpu/api/GPU.cpp
../cpp/rnwgpu/api/GPUAdapter.cpp
../cpp/rnwgpu/api/GPUSupportedLimits.cpp
Expand All @@ -49,8 +48,6 @@ add_library(${PACKAGE_NAME} SHARED
../cpp/jsi/RNFHybridObject.cpp
../cpp/jsi/RNFRuntimeCache.cpp
../cpp/jsi/RNFWorkletRuntimeRegistry.cpp
../cpp/threading/Dispatcher.cpp
../cpp/threading/ThreadPool.cpp
)

target_include_directories(
Expand All @@ -72,8 +69,6 @@ target_include_directories(
../cpp/rnwgpu/api/descriptors
../cpp/jsi
../cpp/webgpu
../cpp/threading
../cpp/platform

${libfbjni_include_DIRS}
)
Expand Down
41 changes: 0 additions & 41 deletions packages/webgpu/android/cpp/platform/ThreadUtils.cpp

This file was deleted.

33 changes: 0 additions & 33 deletions packages/webgpu/apple/platform/ThreadUtils.cpp

This file was deleted.

75 changes: 28 additions & 47 deletions packages/webgpu/cpp/jsi/RNFJSIConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,6 @@
#include "RNFPromise.h"
#include "RNFWorkletRuntimeRegistry.h"

#include "Dispatcher.h"
#include "ThreadPool.h"

#if __has_include(<cxxabi.h>)
#include <cxxabi.h>
#endif
Expand Down Expand Up @@ -210,55 +207,39 @@ template <typename TResult> struct JSIConverter<std::future<TResult>> {
}
static jsi::Value toJSI(jsi::Runtime& runtime, std::future<TResult>&& arg) {
auto sharedFuture = std::make_shared<std::future<TResult>>(std::move(arg));
std::shared_ptr<Dispatcher> strongDispatcher = Dispatcher::getRuntimeGlobalDispatcher(runtime);
std::weak_ptr<Dispatcher> weakDispatcher = strongDispatcher;

return Promise::createPromise(runtime, [sharedFuture = std::move(sharedFuture), weakDispatcher](jsi::Runtime& runtime,
return Promise::createPromise(runtime, [sharedFuture = std::move(sharedFuture)](jsi::Runtime& runtime,
std::shared_ptr<Promise> promise) {
// Spawn new async thread to synchronously wait for the `future<T>` to complete
std::shared_ptr<ThreadPool> pool = ThreadPool::getSharedPool();
pool->run([promise, &runtime, weakDispatcher, sharedFuture]() {
// synchronously wait until the `future<T>` completes. we are running on a background task here.
try {
// wait until the future completes.
sharedFuture->wait();

std::shared_ptr<Dispatcher> dispatcher = weakDispatcher.lock();
if (!dispatcher) {
throw std::runtime_error("Tried resolving Promise on JS Thread, but the `Dispatcher` has already been destroyed!");
return;
if constexpr (std::is_same_v<TResult, void>) {
// it's returning void, just return undefined to JS
sharedFuture->get();
promise->resolve(jsi::Value::undefined());
} else {
// it's returning a custom type, convert it to a jsi::Value
TResult result = sharedFuture->get();
jsi::Value jsResult = JSIConverter<TResult>::toJSI(runtime, result);
promise->resolve(std::move(jsResult));
}
} catch (const std::exception& exception) {
// the async function threw an error, reject the promise
std::string what = exception.what();
promise->reject(what);
} catch (...) {
// the async function threw a non-std error, try getting it
#if __has_include(<cxxabi.h>)
std::string name = __cxxabiv1::__cxa_current_exception_type()->name();
#else
std::string name = "<unknown>";
#endif
promise->reject("Unknown non-std exception: " + name);
}

dispatcher->runAsync([&runtime, promise, sharedFuture]() mutable {
try {
if constexpr (std::is_same_v<TResult, void>) {
// it's returning void, just return undefined to JS
sharedFuture->get();
promise->resolve(jsi::Value::undefined());
} else {
// it's returning a custom type, convert it to a jsi::Value
TResult result = sharedFuture->get();
jsi::Value jsResult = JSIConverter<TResult>::toJSI(runtime, result);
promise->resolve(std::move(jsResult));
}
} catch (const std::exception& exception) {
// the async function threw an error, reject the promise
std::string what = exception.what();
promise->reject(what);
} catch (...) {
// the async function threw a non-std error, try getting it
#if __has_include(<cxxabi.h>)
std::string name = __cxxabiv1::__cxa_current_exception_type()->name();
#else
std::string name = "<unknown>";
#endif
promise->reject("Unknown non-std exception: " + name);
}

// This lambda owns the promise shared pointer, and we need to call its
// destructor correctly here - otherwise it might be called
// from the threadPool thread.
promise = nullptr;
});
});
// This lambda owns the promise shared pointer, and we need to call its
// destructor correctly here - ensuring it's properly handled.
promise = nullptr;
});
}
};
Expand Down
30 changes: 0 additions & 30 deletions packages/webgpu/cpp/platform/ThreadUtils.h

This file was deleted.

8 changes: 0 additions & 8 deletions packages/webgpu/cpp/rnwgpu/RNWebGPUManager.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "RNWebGPUManager.h"

#include "CallInvokerDispatcher.h"
#include "Dispatcher.h"
#include "GPU.h"
#include "RNWebGPU.h"

Expand All @@ -24,12 +22,6 @@ RNWebGPUManager::RNWebGPUManager(
: _jsRuntime(jsRuntime), _jsCallInvoker(jsCallInvoker),
_platformContext(platformContext) {

// Installs the global Dispatcher mechanism into this Runtime.
// This allows creating Promises and calling back to JS.
auto dispatcher =
std::make_shared<margelo::CallInvokerDispatcher>(_jsCallInvoker);
margelo::Dispatcher::installRuntimeGlobalDispatcher(*_jsRuntime, dispatcher);

auto gpu = std::make_shared<GPU>();
auto rnWebGPU = std::make_shared<RNWebGPU>(gpu, _platformContext);
_gpu = gpu->get();
Expand Down
3 changes: 1 addition & 2 deletions packages/webgpu/cpp/rnwgpu/api/GPUDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,6 @@ std::unordered_set<std::string> GPUDevice::getFeatures() {
}

std::future<std::shared_ptr<GPUDeviceLostInfo>> GPUDevice::getLost() {
return std::async(std::launch::async,
[=]() { return m_lostSharedFuture->get(); });
return m_lostPromise->get_future();
}
} // namespace rnwgpu
8 changes: 1 addition & 7 deletions packages/webgpu/cpp/rnwgpu/api/GPUDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ class GPUDevice : public m::HybridObject {
_label(label) {
m_lostPromise =
std::make_shared<std::promise<std::shared_ptr<GPUDeviceLostInfo>>>();

auto sharedFuture = m_lostPromise->get_future().share();
m_lostSharedFuture = std::make_shared<
std::shared_future<std::shared_ptr<GPUDeviceLostInfo>>>(sharedFuture);
}

public:
Expand Down Expand Up @@ -158,8 +154,6 @@ class GPUDevice : public m::HybridObject {
std::string _label;
std::shared_ptr<std::promise<std::shared_ptr<GPUDeviceLostInfo>>>
m_lostPromise;
std::shared_ptr<std::shared_future<std::shared_ptr<GPUDeviceLostInfo>>>
m_lostSharedFuture;
};

} // namespace rnwgpu
} // namespace rnwgpu
37 changes: 0 additions & 37 deletions packages/webgpu/cpp/threading/CallInvokerDispatcher.h

This file was deleted.

54 changes: 0 additions & 54 deletions packages/webgpu/cpp/threading/Dispatcher.cpp

This file was deleted.

Loading
Loading