Skip to content

Commit

Permalink
src: define NAPI_HAS_THREADS
Browse files Browse the repository at this point in the history
PR-URL: #1283
Reviewed-By: Michael Dawson <midawson@redhat.com
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
  • Loading branch information
toyobayashi authored and mhdawson committed Mar 2, 2023
1 parent 464610b commit 0b53d88
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 6 deletions.
14 changes: 10 additions & 4 deletions napi-inl.h
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 @@ -205,7 +207,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 @@ -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 <typename Getter, typename Setter>
struct AccessorCallbackData {
Expand Down Expand Up @@ -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") {}

Expand Down Expand Up @@ -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<ContextType,DataType,CallJs> class
////////////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -6255,7 +6261,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
@@ -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 @@ -2454,6 +2465,7 @@ class AsyncContext {
napi_async_context _context;
};

#if NAPI_HAS_THREADS
class AsyncWorker {
public:
virtual ~AsyncWorker();
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 0b53d88

Please sign in to comment.