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 2 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: 9 additions & 5 deletions napi-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,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 +295,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 @@ -4649,7 +4649,7 @@ inline Value EscapableHandleScope::Escape(napi_value escapee) {
return Value(_env, result);
}

#if (NAPI_VERSION > 2)
#if (NAPI_VERSION > 2 && !defined(__wasm__))
////////////////////////////////////////////////////////////////////////////////
// CallbackScope class
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -4733,6 +4733,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 +4913,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 +6164,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
19 changes: 16 additions & 3 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
toyobayashi marked this conversation as resolved.
Show resolved Hide resolved
#include <string>
#include <vector>

Expand Down Expand Up @@ -2394,7 +2405,7 @@ class EscapableHandleScope {
napi_escapable_handle_scope _scope;
};

#if (NAPI_VERSION > 2)
#if (NAPI_VERSION > 2 && !defined(__wasm__))
class CallbackScope {
public:
CallbackScope(napi_env env, napi_callback_scope scope);
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