From 0b53d885f50c83cd7cbcf505d3dcc89cf8c3d59f Mon Sep 17 00:00:00 2001 From: toyobayashi Date: Wed, 8 Feb 2023 22:09:07 +0800 Subject: [PATCH] src: define `NAPI_HAS_THREADS` PR-URL: https://github.com/nodejs/node-addon-api/pull/1283 Reviewed-By: Michael Dawson --- napi-inl.h | 14 ++++++++++---- napi.h | 17 +++++++++++++++-- 2 files changed, 25 insertions(+), 6 deletions(-) diff --git a/napi-inl.h b/napi-inl.h index a800dd171..db0530231 100644 --- a/napi-inl.h +++ b/napi-inl.h @@ -12,7 +12,9 @@ #include #include +#if NAPI_HAS_THREADS #include +#endif // NAPI_HAS_THREADS #include #include @@ -205,7 +207,7 @@ struct FinalizeData { Hint* hint; }; -#if (NAPI_VERSION > 3 && !defined(__wasm32__)) +#if (NAPI_VERSION > 3 && NAPI_HAS_THREADS) template , typename FinalizerDataType = void> @@ -299,7 +301,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 struct AccessorCallbackData { @@ -4828,6 +4830,8 @@ inline Napi::Env AsyncContext::Env() const { // AsyncWorker class //////////////////////////////////////////////////////////////////////////////// +#if NAPI_HAS_THREADS + inline AsyncWorker::AsyncWorker(const Function& callback) : AsyncWorker(callback, "generic") {} @@ -5006,7 +5010,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 class //////////////////////////////////////////////////////////////////////////////// @@ -6255,7 +6261,7 @@ inline void AsyncProgressQueueWorker::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 diff --git a/napi.h b/napi.h index 82ba6aad0..a7d54027d 100644 --- a/napi.h +++ b/napi.h @@ -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 #include #include #include +#if NAPI_HAS_THREADS #include +#endif // NAPI_HAS_THREADS #include #include @@ -2454,6 +2465,7 @@ class AsyncContext { napi_async_context _context; }; +#if NAPI_HAS_THREADS class AsyncWorker { public: virtual ~AsyncWorker(); @@ -2516,8 +2528,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. @@ -3087,7 +3100,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 {