|
| 1 | +#include "napi.h" |
| 2 | +#include <thread> |
| 3 | +#include <cstdlib> |
| 4 | +#include <condition_variable> |
| 5 | +#include <mutex> |
| 6 | + |
| 7 | +#if (NAPI_VERSION > 3) |
| 8 | + |
| 9 | +using namespace Napi; |
| 10 | + |
| 11 | +namespace { |
| 12 | + |
| 13 | +struct TestData { |
| 14 | + |
| 15 | + TestData(Promise::Deferred&& deferred) : deferred(std::move(deferred)) {}; |
| 16 | + |
| 17 | + // Native Promise returned to JavaScript |
| 18 | + Promise::Deferred deferred; |
| 19 | + |
| 20 | + // List of threads created for test. This list only ever accessed via main |
| 21 | + // thread. |
| 22 | + std::vector<std::thread> threads = {}; |
| 23 | + |
| 24 | + ThreadSafeFunction tsfn = ThreadSafeFunction(); |
| 25 | +}; |
| 26 | + |
| 27 | +void FinalizerCallback(Napi::Env env, TestData* finalizeData){ |
| 28 | + for (size_t i = 0; i < finalizeData->threads.size(); ++i) { |
| 29 | + finalizeData->threads[i].join(); |
| 30 | + } |
| 31 | + finalizeData->deferred.Resolve(Boolean::New(env,true)); |
| 32 | + delete finalizeData; |
| 33 | +} |
| 34 | + |
| 35 | +/** |
| 36 | + * See threadsafe_function_sum.js for descriptions of the tests in this file |
| 37 | + */ |
| 38 | + |
| 39 | +void entryWithTSFN(ThreadSafeFunction tsfn, int threadId) { |
| 40 | + std::this_thread::sleep_for(std::chrono::milliseconds(std::rand() % 100 + 1)); |
| 41 | + tsfn.BlockingCall( [=](Napi::Env env, Function callback) { |
| 42 | + callback.Call( { Number::New(env, static_cast<double>(threadId))}); |
| 43 | + }); |
| 44 | + tsfn.Release(); |
| 45 | +} |
| 46 | + |
| 47 | +static Value TestWithTSFN(const CallbackInfo& info) { |
| 48 | + int threadCount = info[0].As<Number>().Int32Value(); |
| 49 | + Function cb = info[1].As<Function>(); |
| 50 | + |
| 51 | + // We pass the test data to the Finalizer for cleanup. The finalizer is |
| 52 | + // responsible for deleting this data as well. |
| 53 | + TestData *testData = new TestData(Promise::Deferred::New(info.Env())); |
| 54 | + |
| 55 | + ThreadSafeFunction tsfn = ThreadSafeFunction::New( |
| 56 | + info.Env(), cb, "Test", 0, threadCount, |
| 57 | + std::function<decltype(FinalizerCallback)>(FinalizerCallback), testData); |
| 58 | + |
| 59 | + for (int i = 0; i < threadCount; ++i) { |
| 60 | + // A copy of the ThreadSafeFunction will go to the thread entry point |
| 61 | + testData->threads.push_back( std::thread(entryWithTSFN, tsfn, i) ); |
| 62 | + } |
| 63 | + |
| 64 | + return testData->deferred.Promise(); |
| 65 | +} |
| 66 | + |
| 67 | +// Task instance created for each new std::thread |
| 68 | +class DelayedTSFNTask { |
| 69 | +public: |
| 70 | + // Each instance has its own tsfn |
| 71 | + ThreadSafeFunction tsfn; |
| 72 | + |
| 73 | + // Thread-safety |
| 74 | + std::mutex mtx; |
| 75 | + std::condition_variable cv; |
| 76 | + |
| 77 | + // Entry point for std::thread |
| 78 | + void entryDelayedTSFN(int threadId) { |
| 79 | + std::unique_lock<std::mutex> lk(mtx); |
| 80 | + cv.wait(lk); |
| 81 | + tsfn.BlockingCall([=](Napi::Env env, Function callback) { |
| 82 | + callback.Call({Number::New(env, static_cast<double>(threadId))}); |
| 83 | + }); |
| 84 | + tsfn.Release(); |
| 85 | + }; |
| 86 | +}; |
| 87 | + |
| 88 | +struct TestDataDelayed { |
| 89 | + |
| 90 | + TestDataDelayed(Promise::Deferred &&deferred) |
| 91 | + : deferred(std::move(deferred)){}; |
| 92 | + ~TestDataDelayed() { taskInsts.clear(); }; |
| 93 | + // Native Promise returned to JavaScript |
| 94 | + Promise::Deferred deferred; |
| 95 | + |
| 96 | + // List of threads created for test. This list only ever accessed via main |
| 97 | + // thread. |
| 98 | + std::vector<std::thread> threads = {}; |
| 99 | + |
| 100 | + // List of DelayedTSFNThread instances |
| 101 | + std::vector<std::unique_ptr<DelayedTSFNTask>> taskInsts = {}; |
| 102 | + |
| 103 | + ThreadSafeFunction tsfn = ThreadSafeFunction(); |
| 104 | +}; |
| 105 | + |
| 106 | +void FinalizerCallbackDelayed(Napi::Env env, TestDataDelayed *finalizeData) { |
| 107 | + for (size_t i = 0; i < finalizeData->threads.size(); ++i) { |
| 108 | + finalizeData->threads[i].join(); |
| 109 | + } |
| 110 | + finalizeData->deferred.Resolve(Boolean::New(env, true)); |
| 111 | + delete finalizeData; |
| 112 | +} |
| 113 | + |
| 114 | +static Value TestDelayedTSFN(const CallbackInfo &info) { |
| 115 | + int threadCount = info[0].As<Number>().Int32Value(); |
| 116 | + Function cb = info[1].As<Function>(); |
| 117 | + |
| 118 | + TestDataDelayed *testData = |
| 119 | + new TestDataDelayed(Promise::Deferred::New(info.Env())); |
| 120 | + |
| 121 | + testData->tsfn = |
| 122 | + ThreadSafeFunction::New(info.Env(), cb, "Test", 0, threadCount, |
| 123 | + std::function<decltype(FinalizerCallbackDelayed)>( |
| 124 | + FinalizerCallbackDelayed), |
| 125 | + testData); |
| 126 | + |
| 127 | + for (int i = 0; i < threadCount; ++i) { |
| 128 | + testData->taskInsts.push_back( |
| 129 | + std::unique_ptr<DelayedTSFNTask>(new DelayedTSFNTask())); |
| 130 | + testData->threads.push_back(std::thread(&DelayedTSFNTask::entryDelayedTSFN, |
| 131 | + testData->taskInsts.back().get(), |
| 132 | + i)); |
| 133 | + } |
| 134 | + std::this_thread::sleep_for(std::chrono::milliseconds(std::rand() % 100 + 1)); |
| 135 | + |
| 136 | + for (auto &task : testData->taskInsts) { |
| 137 | + std::lock_guard<std::mutex> lk(task->mtx); |
| 138 | + task->tsfn = testData->tsfn; |
| 139 | + task->cv.notify_all(); |
| 140 | + } |
| 141 | + |
| 142 | + return testData->deferred.Promise(); |
| 143 | +} |
| 144 | + |
| 145 | +void entryAcquire(ThreadSafeFunction tsfn, int threadId) { |
| 146 | + tsfn.Acquire(); |
| 147 | + std::this_thread::sleep_for(std::chrono::milliseconds(std::rand() % 100 + 1)); |
| 148 | + tsfn.BlockingCall( [=](Napi::Env env, Function callback) { |
| 149 | + callback.Call( { Number::New(env, static_cast<double>(threadId))}); |
| 150 | + }); |
| 151 | + tsfn.Release(); |
| 152 | +} |
| 153 | + |
| 154 | +static Value CreateThread(const CallbackInfo& info) { |
| 155 | + TestData* testData = static_cast<TestData*>(info.Data()); |
| 156 | + ThreadSafeFunction tsfn = testData->tsfn; |
| 157 | + int threadId = testData->threads.size(); |
| 158 | + // A copy of the ThreadSafeFunction will go to the thread entry point |
| 159 | + testData->threads.push_back( std::thread(entryAcquire, tsfn, threadId) ); |
| 160 | + return Number::New(info.Env(), threadId); |
| 161 | +} |
| 162 | + |
| 163 | +static Value StopThreads(const CallbackInfo& info) { |
| 164 | + TestData* testData = static_cast<TestData*>(info.Data()); |
| 165 | + ThreadSafeFunction tsfn = testData->tsfn; |
| 166 | + tsfn.Release(); |
| 167 | + return info.Env().Undefined(); |
| 168 | +} |
| 169 | + |
| 170 | +static Value TestAcquire(const CallbackInfo& info) { |
| 171 | + Function cb = info[0].As<Function>(); |
| 172 | + Napi::Env env = info.Env(); |
| 173 | + |
| 174 | + // We pass the test data to the Finalizer for cleanup. The finalizer is |
| 175 | + // responsible for deleting this data as well. |
| 176 | + TestData *testData = new TestData(Promise::Deferred::New(info.Env())); |
| 177 | + |
| 178 | + testData->tsfn = ThreadSafeFunction::New( |
| 179 | + env, cb, "Test", 0, 1, |
| 180 | + std::function<decltype(FinalizerCallback)>(FinalizerCallback), testData); |
| 181 | + |
| 182 | + Object result = Object::New(env); |
| 183 | + result["createThread"] = Function::New( env, CreateThread, "createThread", testData); |
| 184 | + result["stopThreads"] = Function::New( env, StopThreads, "stopThreads", testData); |
| 185 | + result["promise"] = testData->deferred.Promise(); |
| 186 | + |
| 187 | + return result; |
| 188 | +} |
| 189 | +} |
| 190 | + |
| 191 | +Object InitThreadSafeFunctionSum(Env env) { |
| 192 | + Object exports = Object::New(env); |
| 193 | + exports["testDelayedTSFN"] = Function::New(env, TestDelayedTSFN); |
| 194 | + exports["testWithTSFN"] = Function::New(env, TestWithTSFN); |
| 195 | + exports["testAcquire"] = Function::New(env, TestAcquire); |
| 196 | + return exports; |
| 197 | +} |
| 198 | + |
| 199 | +#endif |
0 commit comments