MCPcopy Index your code
hub / github.com/nodejs/node-addon-api / DataSourceThread

Function DataSourceThread

test/threadsafe_function/threadsafe_function.cc:44–126  ·  view source on GitHub ↗

Source thread producing the data

Source from the content-addressed store, hash-verified

42
43// Source thread producing the data
44static void DataSourceThread() {
45 ThreadSafeFunctionInfo* info = s_tsfn.GetContext();
46
47 if (info->startSecondary) {
48 if (s_tsfn.Acquire() != napi_ok) {
49 Error::Fatal("DataSourceThread", "ThreadSafeFunction.Acquire() failed");
50 }
51 threads[1] = std::thread(SecondaryThread);
52 }
53
54 bool queueWasFull = false;
55 bool queueWasClosing = false;
56
57 for (int index = ARRAY_LENGTH - 1; index > -1 && !queueWasClosing; index--) {
58 napi_status status = napi_generic_failure;
59
60 auto callback = [](Env env, Function jsCallback, int* data) {
61 jsCallback.Call({Number::New(env, *data)});
62 };
63
64 auto noArgCallback = [](Env env, Function jsCallback) {
65 jsCallback.Call({Number::New(env, 42)});
66 };
67
68 switch (info->type) {
69 case ThreadSafeFunctionInfo::DEFAULT:
70 status = s_tsfn.BlockingCall();
71 break;
72 case ThreadSafeFunctionInfo::BLOCKING:
73 status = s_tsfn.BlockingCall(&ints[index], callback);
74 break;
75 case ThreadSafeFunctionInfo::NON_BLOCKING:
76 status = s_tsfn.NonBlockingCall(&ints[index], callback);
77 break;
78 case ThreadSafeFunctionInfo::NON_BLOCKING_DEFAULT:
79 status = s_tsfn.NonBlockingCall();
80 break;
81
82 case ThreadSafeFunctionInfo::NON_BLOCKING_SINGLE_ARG:
83 status = s_tsfn.NonBlockingCall(noArgCallback);
84 break;
85 }
86
87 if (info->abort && (info->type == ThreadSafeFunctionInfo::BLOCKING ||
88 info->type == ThreadSafeFunctionInfo::DEFAULT)) {
89 // Let's make this thread really busy to give the main thread a chance to
90 // abort / close.
91 std::unique_lock<std::mutex> lk(info->protect);
92 while (!info->closeCalledFromJs) {
93 info->signal.wait(lk);
94 }
95 }
96
97 switch (status) {
98 case napi_queue_full:
99 queueWasFull = true;
100 index++;
101 // fall through

Callers

nothing calls this directly

Calls 6

AcquireMethod · 0.80
CallMethod · 0.80
BlockingCallMethod · 0.80
NonBlockingCallMethod · 0.80
GetContextMethod · 0.45
ReleaseMethod · 0.45

Tested by

no test coverage detected