Skip to content

Commit 0b53d88

Browse files
toyobayashimhdawson
authored andcommitted
src: define NAPI_HAS_THREADS
PR-URL: #1283 Reviewed-By: Michael Dawson <[email protected] Reviewed-By: Chengzhong Wu <[email protected]>
1 parent 464610b commit 0b53d88

File tree

2 files changed

+25
-6
lines changed

2 files changed

+25
-6
lines changed

napi-inl.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212

1313
#include <algorithm>
1414
#include <cstring>
15+
#if NAPI_HAS_THREADS
1516
#include <mutex>
17+
#endif // NAPI_HAS_THREADS
1618
#include <type_traits>
1719
#include <utility>
1820

@@ -205,7 +207,7 @@ struct FinalizeData {
205207
Hint* hint;
206208
};
207209

208-
#if (NAPI_VERSION > 3 && !defined(__wasm32__))
210+
#if (NAPI_VERSION > 3 && NAPI_HAS_THREADS)
209211
template <typename ContextType = void,
210212
typename Finalizer = std::function<void(Env, void*, ContextType*)>,
211213
typename FinalizerDataType = void>
@@ -299,7 +301,7 @@ napi_value DefaultCallbackWrapper(napi_env env, Napi::Function cb) {
299301
return cb;
300302
}
301303
#endif // NAPI_VERSION > 4
302-
#endif // NAPI_VERSION > 3 && !defined(__wasm32__)
304+
#endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS
303305

304306
template <typename Getter, typename Setter>
305307
struct AccessorCallbackData {
@@ -4828,6 +4830,8 @@ inline Napi::Env AsyncContext::Env() const {
48284830
// AsyncWorker class
48294831
////////////////////////////////////////////////////////////////////////////////
48304832

4833+
#if NAPI_HAS_THREADS
4834+
48314835
inline AsyncWorker::AsyncWorker(const Function& callback)
48324836
: AsyncWorker(callback, "generic") {}
48334837

@@ -5006,7 +5010,9 @@ inline void AsyncWorker::OnWorkComplete(Napi::Env /*env*/, napi_status status) {
50065010
}
50075011
}
50085012

5009-
#if (NAPI_VERSION > 3 && !defined(__wasm32__))
5013+
#endif // NAPI_HAS_THREADS
5014+
5015+
#if (NAPI_VERSION > 3 && NAPI_HAS_THREADS)
50105016
////////////////////////////////////////////////////////////////////////////////
50115017
// TypedThreadSafeFunction<ContextType,DataType,CallJs> class
50125018
////////////////////////////////////////////////////////////////////////////////
@@ -6255,7 +6261,7 @@ inline void AsyncProgressQueueWorker<T>::ExecutionProgress::Send(
62556261
const T* data, size_t count) const {
62566262
_worker->SendProgress_(data, count);
62576263
}
6258-
#endif // NAPI_VERSION > 3 && !defined(__wasm32__)
6264+
#endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS
62596265

62606266
////////////////////////////////////////////////////////////////////////////////
62616267
// Memory Management class

napi.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,22 @@
11
#ifndef SRC_NAPI_H_
22
#define SRC_NAPI_H_
33

4+
#ifndef NAPI_HAS_THREADS
5+
#if !defined(__wasm__) || (defined(__EMSCRIPTEN_PTHREADS__) || \
6+
(defined(__wasi__) && defined(_REENTRANT)))
7+
#define NAPI_HAS_THREADS 1
8+
#else
9+
#define NAPI_HAS_THREADS 0
10+
#endif
11+
#endif
12+
413
#include <node_api.h>
514
#include <functional>
615
#include <initializer_list>
716
#include <memory>
17+
#if NAPI_HAS_THREADS
818
#include <mutex>
19+
#endif // NAPI_HAS_THREADS
920
#include <string>
1021
#include <vector>
1122

@@ -2454,6 +2465,7 @@ class AsyncContext {
24542465
napi_async_context _context;
24552466
};
24562467

2468+
#if NAPI_HAS_THREADS
24572469
class AsyncWorker {
24582470
public:
24592471
virtual ~AsyncWorker();
@@ -2516,8 +2528,9 @@ class AsyncWorker {
25162528
std::string _error;
25172529
bool _suppress_destruct;
25182530
};
2531+
#endif // NAPI_HAS_THREADS
25192532

2520-
#if (NAPI_VERSION > 3 && !defined(__wasm32__))
2533+
#if (NAPI_VERSION > 3 && NAPI_HAS_THREADS)
25212534
class ThreadSafeFunction {
25222535
public:
25232536
// This API may only be called from the main thread.
@@ -3087,7 +3100,7 @@ class AsyncProgressQueueWorker
30873100
void Signal() const;
30883101
void SendProgress_(const T* data, size_t count);
30893102
};
3090-
#endif // NAPI_VERSION > 3 && !defined(__wasm32__)
3103+
#endif // NAPI_VERSION > 3 && NAPI_HAS_THREADS
30913104

30923105
// Memory management.
30933106
class MemoryManagement {

0 commit comments

Comments
 (0)