Skip to content
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

src: define NAPI_HAS_THREADS to make TSFN available on Emscripten #1283

Closed
wants to merge 5 commits into from
Closed
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
14 changes: 10 additions & 4 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@

#include <algorithm>
#include <cstring>
#if NAPI_HAS_THREADS
#include <mutex>
#endif // NAPI_HAS_THREADS
#include <type_traits>
#include <utility>

Expand Down Expand Up @@ -201,7 +203,7 @@ struct FinalizeData {
Hint* hint;
};

#if (NAPI_VERSION > 3 && !defined(__wasm32__))
#if (NAPI_VERSION > 3 && NAPI_HAS_THREADS)
template <typename ContextType = void,
typename Finalizer = std::function<void(Env, void*, ContextType*)>,
typename FinalizerDataType = void>
Expand Down Expand Up @@ -295,7 +297,7 @@ napi_value DefaultCallbackWrapper(napi_env env, Napi::Function cb) {
return cb;
}
#endif // NAPI_VERSION > 4
#endif // NAPI_VERSION > 3 && !defined(__wasm32__)
#endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS

template <typename Getter, typename Setter>
struct AccessorCallbackData {
Expand Down Expand Up @@ -4733,6 +4735,8 @@ inline Napi::Env AsyncContext::Env() const {
// AsyncWorker class
////////////////////////////////////////////////////////////////////////////////

#if NAPI_HAS_THREADS

inline AsyncWorker::AsyncWorker(const Function& callback)
: AsyncWorker(callback, "generic") {}

Expand Down Expand Up @@ -4911,7 +4915,9 @@ inline void AsyncWorker::OnWorkComplete(Napi::Env /*env*/, napi_status status) {
}
}

#if (NAPI_VERSION > 3 && !defined(__wasm32__))
#endif // NAPI_HAS_THREADS

#if (NAPI_VERSION > 3 && NAPI_HAS_THREADS)
////////////////////////////////////////////////////////////////////////////////
// TypedThreadSafeFunction<ContextType,DataType,CallJs> class
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -6160,7 +6166,7 @@ inline void AsyncProgressQueueWorker<T>::ExecutionProgress::Send(
const T* data, size_t count) const {
_worker->SendProgress_(data, count);
}
#endif // NAPI_VERSION > 3 && !defined(__wasm32__)
#endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS

////////////////////////////////////////////////////////////////////////////////
// Memory Management class
Expand Down
17 changes: 15 additions & 2 deletions napi.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,22 @@
#ifndef SRC_NAPI_H_
#define SRC_NAPI_H_

#ifndef NAPI_HAS_THREADS
#if !defined(__wasm__) || (defined(__EMSCRIPTEN_PTHREADS__) || \
(defined(__wasi__) && defined(_REENTRANT)))
#define NAPI_HAS_THREADS 1
#else
#define NAPI_HAS_THREADS 0
#endif
#endif

#include <node_api.h>
#include <functional>
#include <initializer_list>
#include <memory>
#if NAPI_HAS_THREADS
#include <mutex>
#endif // NAPI_HAS_THREADS
#include <string>
#include <vector>

Expand Down Expand Up @@ -2435,6 +2446,7 @@ class AsyncContext {
napi_async_context _context;
};

#if NAPI_HAS_THREADS
class AsyncWorker {
public:
virtual ~AsyncWorker();
Expand Down Expand Up @@ -2497,8 +2509,9 @@ class AsyncWorker {
std::string _error;
bool _suppress_destruct;
};
#endif // NAPI_HAS_THREADS

#if (NAPI_VERSION > 3 && !defined(__wasm32__))
#if (NAPI_VERSION > 3 && NAPI_HAS_THREADS)
class ThreadSafeFunction {
public:
// This API may only be called from the main thread.
Expand Down Expand Up @@ -3068,7 +3081,7 @@ class AsyncProgressQueueWorker
void Signal() const;
void SendProgress_(const T* data, size_t count);
};
#endif // NAPI_VERSION > 3 && !defined(__wasm32__)
#endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS

// Memory management.
class MemoryManagement {
Expand Down