|
| 1 | +// For the purpose of this test we use libuv's threading library. When deciding |
| 2 | +// on a threading library for a new project it bears remembering that in the |
| 3 | +// future libuv may introduce API changes which may render it non-ABI-stable, |
| 4 | +// which, in turn, may affect the ABI stability of the project despite its use |
| 5 | +// of N-API. |
| 6 | +#include <uv.h> |
| 7 | +#define NAPI_EXPERIMENTAL |
| 8 | +#include <node_api.h> |
| 9 | +#include "../common.h" |
| 10 | + |
| 11 | +#define ARRAY_LENGTH 10 |
| 12 | + |
| 13 | +static uv_thread_t uv_threads[2]; |
| 14 | +static napi_threadsafe_function ts_fn; |
| 15 | + |
| 16 | +typedef struct { |
| 17 | + napi_threadsafe_function_call_mode block_on_full; |
| 18 | + napi_threadsafe_function_release_mode abort; |
| 19 | + bool start_secondary; |
| 20 | + napi_ref js_finalize_cb; |
| 21 | +} ts_fn_hint; |
| 22 | + |
| 23 | +static ts_fn_hint ts_info; |
| 24 | + |
| 25 | +// Thread data to transmit to JS |
| 26 | +static int ints[ARRAY_LENGTH]; |
| 27 | + |
| 28 | +static void secondary_thread(void* data) { |
| 29 | + napi_threadsafe_function ts_fn = data; |
| 30 | + |
| 31 | + if (napi_release_threadsafe_function(ts_fn, napi_tsfn_release) != napi_ok) { |
| 32 | + napi_fatal_error("secondary_thread", NAPI_AUTO_LENGTH, |
| 33 | + "napi_release_threadsafe_function failed", NAPI_AUTO_LENGTH); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +// Source thread producing the data |
| 38 | +static void data_source_thread(void* data) { |
| 39 | + napi_threadsafe_function ts_fn = data; |
| 40 | + int index; |
| 41 | + void* hint; |
| 42 | + ts_fn_hint *ts_fn_info; |
| 43 | + napi_status status; |
| 44 | + bool queue_was_full = false; |
| 45 | + bool queue_was_closing = false; |
| 46 | + |
| 47 | + if (napi_get_threadsafe_function_context(ts_fn, &hint) != napi_ok) { |
| 48 | + napi_fatal_error("data_source_thread", NAPI_AUTO_LENGTH, |
| 49 | + "napi_get_threadsafe_function_context failed", NAPI_AUTO_LENGTH); |
| 50 | + } |
| 51 | + |
| 52 | + ts_fn_info = (ts_fn_hint *)hint; |
| 53 | + |
| 54 | + if (ts_fn_info != &ts_info) { |
| 55 | + napi_fatal_error("data_source_thread", NAPI_AUTO_LENGTH, |
| 56 | + "thread-safe function hint is not as expected", NAPI_AUTO_LENGTH); |
| 57 | + } |
| 58 | + |
| 59 | + if (ts_fn_info->start_secondary) { |
| 60 | + if (napi_acquire_threadsafe_function(ts_fn) != napi_ok) { |
| 61 | + napi_fatal_error("data_source_thread", NAPI_AUTO_LENGTH, |
| 62 | + "napi_acquire_threadsafe_function failed", NAPI_AUTO_LENGTH); |
| 63 | + } |
| 64 | + |
| 65 | + if (uv_thread_create(&uv_threads[1], secondary_thread, ts_fn) != 0) { |
| 66 | + napi_fatal_error("data_source_thread", NAPI_AUTO_LENGTH, |
| 67 | + "failed to start secondary thread", NAPI_AUTO_LENGTH); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + for (index = ARRAY_LENGTH - 1; index > -1 && !queue_was_closing; index--) { |
| 72 | + status = napi_call_threadsafe_function(ts_fn, &ints[index], |
| 73 | + ts_fn_info->block_on_full); |
| 74 | + switch (status) { |
| 75 | + case napi_queue_full: |
| 76 | + queue_was_full = true; |
| 77 | + index++; |
| 78 | + // fall through |
| 79 | + |
| 80 | + case napi_ok: |
| 81 | + continue; |
| 82 | + |
| 83 | + case napi_closing: |
| 84 | + queue_was_closing = true; |
| 85 | + break; |
| 86 | + |
| 87 | + default: |
| 88 | + napi_fatal_error("data_source_thread", NAPI_AUTO_LENGTH, |
| 89 | + "napi_call_threadsafe_function failed", NAPI_AUTO_LENGTH); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Assert that the enqueuing of a value was refused at least once, if this is |
| 94 | + // a non-blocking test run. |
| 95 | + if (!ts_fn_info->block_on_full && !queue_was_full) { |
| 96 | + napi_fatal_error("data_source_thread", NAPI_AUTO_LENGTH, |
| 97 | + "queue was never full", NAPI_AUTO_LENGTH); |
| 98 | + } |
| 99 | + |
| 100 | + // Assert that the queue was marked as closing at least once, if this is an |
| 101 | + // aborting test run. |
| 102 | + if (ts_fn_info->abort == napi_tsfn_abort && !queue_was_closing) { |
| 103 | + napi_fatal_error("data_source_thread", NAPI_AUTO_LENGTH, |
| 104 | + "queue was never closing", NAPI_AUTO_LENGTH); |
| 105 | + } |
| 106 | + |
| 107 | + if (!queue_was_closing && |
| 108 | + napi_release_threadsafe_function(ts_fn, napi_tsfn_release) != napi_ok) { |
| 109 | + napi_fatal_error("data_source_thread", NAPI_AUTO_LENGTH, |
| 110 | + "napi_release_threadsafe_function failed", NAPI_AUTO_LENGTH); |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +// Getting the data into JS |
| 115 | +static void call_js(napi_env env, napi_value cb, void* hint, void* data) { |
| 116 | + if (!(env == NULL || cb == NULL)) { |
| 117 | + napi_value argv, undefined; |
| 118 | + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, *(int*)data, &argv)); |
| 119 | + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); |
| 120 | + NAPI_CALL_RETURN_VOID(env, napi_call_function(env, undefined, cb, 1, &argv, |
| 121 | + NULL)); |
| 122 | + } |
| 123 | +} |
| 124 | + |
| 125 | +// Cleanup |
| 126 | +static napi_value StopThread(napi_env env, napi_callback_info info) { |
| 127 | + size_t argc = 2; |
| 128 | + napi_value argv[2]; |
| 129 | + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); |
| 130 | + napi_valuetype value_type; |
| 131 | + NAPI_CALL(env, napi_typeof(env, argv[0], &value_type)); |
| 132 | + NAPI_ASSERT(env, value_type == napi_function, |
| 133 | + "StopThread argument is a function"); |
| 134 | + NAPI_ASSERT(env, (ts_fn != NULL), "Existing threadsafe function"); |
| 135 | + NAPI_CALL(env, |
| 136 | + napi_create_reference(env, argv[0], 1, &(ts_info.js_finalize_cb))); |
| 137 | + bool abort; |
| 138 | + NAPI_CALL(env, napi_get_value_bool(env, argv[1], &abort)); |
| 139 | + NAPI_CALL(env, |
| 140 | + napi_release_threadsafe_function(ts_fn, |
| 141 | + abort ? napi_tsfn_abort : napi_tsfn_release)); |
| 142 | + ts_fn = NULL; |
| 143 | + return NULL; |
| 144 | +} |
| 145 | + |
| 146 | +// Join the thread and inform JS that we're done. |
| 147 | +static void join_the_threads(napi_env env, void *data, void *hint) { |
| 148 | + uv_thread_t *the_threads = data; |
| 149 | + ts_fn_hint *the_hint = hint; |
| 150 | + napi_value js_cb, undefined; |
| 151 | + |
| 152 | + uv_thread_join(&the_threads[0]); |
| 153 | + if (the_hint->start_secondary) { |
| 154 | + uv_thread_join(&the_threads[1]); |
| 155 | + } |
| 156 | + |
| 157 | + NAPI_CALL_RETURN_VOID(env, |
| 158 | + napi_get_reference_value(env, the_hint->js_finalize_cb, &js_cb)); |
| 159 | + NAPI_CALL_RETURN_VOID(env, napi_get_undefined(env, &undefined)); |
| 160 | + NAPI_CALL_RETURN_VOID(env, |
| 161 | + napi_call_function(env, undefined, js_cb, 0, NULL, NULL)); |
| 162 | + NAPI_CALL_RETURN_VOID(env, napi_delete_reference(env, |
| 163 | + the_hint->js_finalize_cb)); |
| 164 | +} |
| 165 | + |
| 166 | +static napi_value StartThreadInternal(napi_env env, |
| 167 | + napi_callback_info info, |
| 168 | + napi_threadsafe_function_call_js cb, |
| 169 | + bool block_on_full) { |
| 170 | + size_t argc = 3; |
| 171 | + napi_value argv[3]; |
| 172 | + |
| 173 | + ts_info.block_on_full = |
| 174 | + (block_on_full ? napi_tsfn_blocking : napi_tsfn_nonblocking); |
| 175 | + |
| 176 | + NAPI_ASSERT(env, (ts_fn == NULL), "Existing thread-safe function"); |
| 177 | + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, NULL, NULL)); |
| 178 | + napi_value async_name; |
| 179 | + NAPI_CALL(env, napi_create_string_utf8(env, "N-API Thread-safe Function Test", |
| 180 | + NAPI_AUTO_LENGTH, &async_name)); |
| 181 | + NAPI_CALL(env, napi_create_threadsafe_function(env, argv[0], NULL, async_name, |
| 182 | + 2, 2, uv_threads, join_the_threads, &ts_info, cb, &ts_fn)); |
| 183 | + bool abort; |
| 184 | + NAPI_CALL(env, napi_get_value_bool(env, argv[1], &abort)); |
| 185 | + ts_info.abort = abort ? napi_tsfn_abort : napi_tsfn_release; |
| 186 | + NAPI_CALL(env, napi_get_value_bool(env, argv[2], &(ts_info.start_secondary))); |
| 187 | + |
| 188 | + NAPI_ASSERT(env, |
| 189 | + (uv_thread_create(&uv_threads[0], data_source_thread, ts_fn) == 0), |
| 190 | + "Thread creation"); |
| 191 | + |
| 192 | + return NULL; |
| 193 | +} |
| 194 | + |
| 195 | +static napi_value Unref(napi_env env, napi_callback_info info) { |
| 196 | + NAPI_ASSERT(env, ts_fn != NULL, "No existing thread-safe function"); |
| 197 | + NAPI_CALL(env, napi_unref_threadsafe_function(env, ts_fn)); |
| 198 | + return NULL; |
| 199 | +} |
| 200 | + |
| 201 | +static napi_value Release(napi_env env, napi_callback_info info) { |
| 202 | + NAPI_ASSERT(env, ts_fn != NULL, "No existing thread-safe function"); |
| 203 | + NAPI_CALL(env, napi_release_threadsafe_function(ts_fn, napi_tsfn_release)); |
| 204 | + return NULL; |
| 205 | +} |
| 206 | + |
| 207 | +// Startup |
| 208 | +static napi_value StartThread(napi_env env, napi_callback_info info) { |
| 209 | + return StartThreadInternal(env, info, call_js, true); |
| 210 | +} |
| 211 | + |
| 212 | +static napi_value StartThreadNonblocking(napi_env env, |
| 213 | + napi_callback_info info) { |
| 214 | + return StartThreadInternal(env, info, call_js, false); |
| 215 | +} |
| 216 | + |
| 217 | +static napi_value StartThreadNoNative(napi_env env, napi_callback_info info) { |
| 218 | + return StartThreadInternal(env, info, NULL, true); |
| 219 | +} |
| 220 | + |
| 221 | +// Module init |
| 222 | +static napi_value Init(napi_env env, napi_value exports) { |
| 223 | + size_t index; |
| 224 | + for (index = 0; index < ARRAY_LENGTH; index++) { |
| 225 | + ints[index] = index; |
| 226 | + } |
| 227 | + napi_value js_array_length; |
| 228 | + napi_create_uint32(env, ARRAY_LENGTH, &js_array_length); |
| 229 | + |
| 230 | + napi_property_descriptor properties[] = { |
| 231 | + { |
| 232 | + "ARRAY_LENGTH", |
| 233 | + NULL, |
| 234 | + NULL, |
| 235 | + NULL, |
| 236 | + NULL, |
| 237 | + js_array_length, |
| 238 | + napi_enumerable, |
| 239 | + NULL |
| 240 | + }, |
| 241 | + DECLARE_NAPI_PROPERTY("StartThread", StartThread), |
| 242 | + DECLARE_NAPI_PROPERTY("StartThreadNoNative", StartThreadNoNative), |
| 243 | + DECLARE_NAPI_PROPERTY("StartThreadNonblocking", StartThreadNonblocking), |
| 244 | + DECLARE_NAPI_PROPERTY("StopThread", StopThread), |
| 245 | + DECLARE_NAPI_PROPERTY("Unref", Unref), |
| 246 | + DECLARE_NAPI_PROPERTY("Release", Release), |
| 247 | + }; |
| 248 | + |
| 249 | + NAPI_CALL(env, napi_define_properties(env, exports, |
| 250 | + sizeof(properties)/sizeof(properties[0]), properties)); |
| 251 | + |
| 252 | + return exports; |
| 253 | +} |
| 254 | +NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) |
0 commit comments