emnapi 0.31.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (44) hide show
  1. package/CMakeLists.txt +160 -0
  2. package/LICENSE +21 -0
  3. package/README.md +875 -0
  4. package/cmake/wasm32.cmake +32 -0
  5. package/dist/library_napi.js +4784 -0
  6. package/include/common.h +26 -0
  7. package/include/emnapi.h +81 -0
  8. package/include/js_native_api.h +560 -0
  9. package/include/js_native_api_types.h +159 -0
  10. package/include/napi-inl.deprecated.h +186 -0
  11. package/include/napi-inl.h +6299 -0
  12. package/include/napi.h +3127 -0
  13. package/include/node_api.h +226 -0
  14. package/include/node_api_types.h +56 -0
  15. package/include/uv/threadpool.h +41 -0
  16. package/include/uv/unix.h +21 -0
  17. package/include/uv.h +134 -0
  18. package/index.d.ts +4 -0
  19. package/index.js +22 -0
  20. package/lib/wasm32/libdlmalloc.a +0 -0
  21. package/lib/wasm32/libemmalloc.a +0 -0
  22. package/lib/wasm32/libemnapi.a +0 -0
  23. package/lib/wasm32-emscripten/libemnapi-mt.a +0 -0
  24. package/lib/wasm32-emscripten/libemnapi.a +0 -0
  25. package/lib/wasm32-emscripten.txt +5 -0
  26. package/lib/wasm32-wasi/libemnapi.a +0 -0
  27. package/lib/wasm32-wasi.txt +4 -0
  28. package/lib/wasm32.txt +4 -0
  29. package/package.json +43 -0
  30. package/src/emnapi.c +1344 -0
  31. package/src/malloc/dlmalloc/dlmalloc.c +92 -0
  32. package/src/malloc/dlmalloc/malloc.c +6395 -0
  33. package/src/malloc/emmalloc/emmalloc.c +1551 -0
  34. package/src/malloc/memcpy.c +136 -0
  35. package/src/malloc/memset.c +98 -0
  36. package/src/malloc/sbrk.c +29 -0
  37. package/src/uv/queue.h +108 -0
  38. package/src/uv/threadpool.c +408 -0
  39. package/src/uv/unix/async.c +206 -0
  40. package/src/uv/unix/core.c +35 -0
  41. package/src/uv/unix/loop.c +36 -0
  42. package/src/uv/unix/thread.c +118 -0
  43. package/src/uv/uv-common.c +51 -0
  44. package/src/uv/uv-common.h +68 -0
@@ -0,0 +1,408 @@
1
+ /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
+ *
3
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
4
+ * of this software and associated documentation files (the "Software"), to
5
+ * deal in the Software without restriction, including without limitation the
6
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
7
+ * sell copies of the Software, and to permit persons to whom the Software is
8
+ * furnished to do so, subject to the following conditions:
9
+ *
10
+ * The above copyright notice and this permission notice shall be included in
11
+ * all copies or substantial portions of the Software.
12
+ *
13
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
18
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
19
+ * IN THE SOFTWARE.
20
+ */
21
+
22
+ // from libuv 1.43.0
23
+
24
+ #if defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)
25
+
26
+ #include <stdlib.h>
27
+ #include <errno.h>
28
+ #include <string.h>
29
+ #include "uv-common.h"
30
+ #include "common.h"
31
+
32
+ #define MAX_THREADPOOL_SIZE 1024
33
+
34
+ static uv_once_t once = UV_ONCE_INIT;
35
+ static uv_cond_t cond;
36
+ static uv_mutex_t mutex;
37
+ static unsigned int idle_threads;
38
+ static unsigned int slow_io_work_running;
39
+ static unsigned int nthreads;
40
+ static uv_thread_t* threads;
41
+ static uv_thread_t default_threads[4];
42
+ static QUEUE exit_message;
43
+ static QUEUE wq;
44
+ static QUEUE run_slow_work_message;
45
+ static QUEUE slow_io_pending_wq;
46
+
47
+ static unsigned int slow_work_thread_threshold(void) {
48
+ return (nthreads + 1) / 2;
49
+ }
50
+
51
+ static void uv__cancelled(struct uv__work* w) {
52
+ abort();
53
+ }
54
+
55
+ EMNAPI_INTERNAL_EXTERN void _emnapi_worker_unref(uv_thread_t pid);
56
+
57
+ /* To avoid deadlock with uv_cancel() it's crucial that the worker
58
+ * never holds the global mutex and the loop-local mutex at the same time.
59
+ */
60
+ static void* worker(void* arg) {
61
+ struct uv__work* w;
62
+ QUEUE* q;
63
+ int is_slow_work;
64
+
65
+ uv_sem_post((uv_sem_t*) arg);
66
+ arg = NULL;
67
+
68
+ uv_mutex_lock(&mutex);
69
+ for (;;) {
70
+ /* `mutex` should always be locked at this point. */
71
+
72
+ /* Keep waiting while either no work is present or only slow I/O
73
+ and we're at the threshold for that. */
74
+ while (QUEUE_EMPTY(&wq) ||
75
+ (QUEUE_HEAD(&wq) == &run_slow_work_message &&
76
+ QUEUE_NEXT(&run_slow_work_message) == &wq &&
77
+ slow_io_work_running >= slow_work_thread_threshold())) {
78
+ idle_threads += 1;
79
+ uv_cond_wait(&cond, &mutex);
80
+ idle_threads -= 1;
81
+ }
82
+
83
+ q = QUEUE_HEAD(&wq);
84
+ if (q == &exit_message) {
85
+ uv_cond_signal(&cond);
86
+ uv_mutex_unlock(&mutex);
87
+ break;
88
+ }
89
+
90
+ QUEUE_REMOVE(q);
91
+ QUEUE_INIT(q); /* Signal uv_cancel() that the work req is executing. */
92
+
93
+ is_slow_work = 0;
94
+ if (q == &run_slow_work_message) {
95
+ /* If we're at the slow I/O threshold, re-schedule until after all
96
+ other work in the queue is done. */
97
+ if (slow_io_work_running >= slow_work_thread_threshold()) {
98
+ QUEUE_INSERT_TAIL(&wq, q);
99
+ continue;
100
+ }
101
+
102
+ /* If we encountered a request to run slow I/O work but there is none
103
+ to run, that means it's cancelled => Start over. */
104
+ if (QUEUE_EMPTY(&slow_io_pending_wq))
105
+ continue;
106
+
107
+ is_slow_work = 1;
108
+ slow_io_work_running++;
109
+
110
+ q = QUEUE_HEAD(&slow_io_pending_wq);
111
+ QUEUE_REMOVE(q);
112
+ QUEUE_INIT(q);
113
+
114
+ /* If there is more slow I/O work, schedule it to be run as well. */
115
+ if (!QUEUE_EMPTY(&slow_io_pending_wq)) {
116
+ QUEUE_INSERT_TAIL(&wq, &run_slow_work_message);
117
+ if (idle_threads > 0)
118
+ uv_cond_signal(&cond);
119
+ }
120
+ }
121
+
122
+ uv_mutex_unlock(&mutex);
123
+
124
+ w = QUEUE_DATA(q, struct uv__work, wq);
125
+ w->work(w);
126
+
127
+ uv_mutex_lock(&w->loop->wq_mutex);
128
+ w->work = NULL; /* Signal uv_cancel() that the work req is done
129
+ executing. */
130
+ QUEUE_INSERT_TAIL(&w->loop->wq, &w->wq);
131
+ uv_async_send(&w->loop->wq_async);
132
+ uv_mutex_unlock(&w->loop->wq_mutex);
133
+
134
+ /* Lock `mutex` since that is expected at the start of the next
135
+ * iteration. */
136
+ uv_mutex_lock(&mutex);
137
+ if (is_slow_work) {
138
+ /* `slow_io_work_running` is protected by `mutex`. */
139
+ slow_io_work_running--;
140
+ }
141
+ }
142
+ return NULL;
143
+ }
144
+
145
+
146
+ static void post(QUEUE* q, enum uv__work_kind kind) {
147
+ uv_mutex_lock(&mutex);
148
+ // if (kind == UV__WORK_SLOW_IO) {
149
+ // /* Insert into a separate queue. */
150
+ // QUEUE_INSERT_TAIL(&slow_io_pending_wq, q);
151
+ // if (!QUEUE_EMPTY(&run_slow_work_message)) {
152
+ // /* Running slow I/O tasks is already scheduled => Nothing to do here.
153
+ // The worker that runs said other task will schedule this one as well. */
154
+ // uv_mutex_unlock(&mutex);
155
+ // return;
156
+ // }
157
+ // q = &run_slow_work_message;
158
+ // }
159
+
160
+ QUEUE_INSERT_TAIL(&wq, q);
161
+ if (idle_threads > 0)
162
+ uv_cond_signal(&cond);
163
+ uv_mutex_unlock(&mutex);
164
+ }
165
+
166
+
167
+ // #ifdef __MVS__
168
+ // /* TODO(itodorov) - zos: revisit when Woz compiler is available. */
169
+ // __attribute__((destructor))
170
+ // #endif
171
+ // void uv__threadpool_cleanup(void) {
172
+ // unsigned int i;
173
+
174
+ // if (nthreads == 0)
175
+ // return;
176
+
177
+ // #ifndef __MVS__
178
+ // /* TODO(gabylb) - zos: revisit when Woz compiler is available. */
179
+ // post(&exit_message, UV__WORK_CPU);
180
+ // #endif
181
+
182
+ // for (i = 0; i < nthreads; i++)
183
+ // if (uv_thread_join(threads + i))
184
+ // abort();
185
+
186
+ // if (threads != default_threads)
187
+ // free(threads);
188
+
189
+ // uv_mutex_destroy(&mutex);
190
+ // uv_cond_destroy(&cond);
191
+
192
+ // threads = NULL;
193
+ // nthreads = 0;
194
+ // }
195
+
196
+
197
+ static void init_threads(void) {
198
+ unsigned int i;
199
+ #if !defined(EMNAPI_WORKER_POOL_SIZE) || !(EMNAPI_WORKER_POOL_SIZE > 0)
200
+ const char* val;
201
+ #endif
202
+ uv_sem_t sem;
203
+
204
+ #if defined(EMNAPI_WORKER_POOL_SIZE) && EMNAPI_WORKER_POOL_SIZE > 0
205
+ nthreads = EMNAPI_WORKER_POOL_SIZE;
206
+ #else
207
+ nthreads = ARRAY_SIZE(default_threads);
208
+ val = getenv("UV_THREADPOOL_SIZE");
209
+ if (val != NULL)
210
+ nthreads = atoi(val);
211
+ #endif
212
+ if (nthreads == 0)
213
+ nthreads = 1;
214
+ if (nthreads > MAX_THREADPOOL_SIZE)
215
+ nthreads = MAX_THREADPOOL_SIZE;
216
+
217
+ threads = default_threads;
218
+ if (nthreads > ARRAY_SIZE(default_threads)) {
219
+ threads = (uv_thread_t *)malloc(nthreads * sizeof(threads[0]));
220
+ if (threads == NULL) {
221
+ nthreads = ARRAY_SIZE(default_threads);
222
+ threads = default_threads;
223
+ }
224
+ }
225
+
226
+ if (uv_cond_init(&cond))
227
+ abort();
228
+
229
+ if (uv_mutex_init(&mutex))
230
+ abort();
231
+
232
+ QUEUE_INIT(&wq);
233
+ QUEUE_INIT(&slow_io_pending_wq);
234
+ QUEUE_INIT(&run_slow_work_message);
235
+
236
+ if (uv_sem_init(&sem, 0))
237
+ abort();
238
+
239
+ for (i = 0; i < nthreads; i++)
240
+ if (uv_thread_create(threads + i, (uv_thread_cb) worker, &sem))
241
+ abort();
242
+
243
+ for (i = 0; i < nthreads; i++)
244
+ uv_sem_wait(&sem);
245
+
246
+ uv_sem_destroy(&sem);
247
+
248
+ for (i = 0; i < nthreads; i++)
249
+ _emnapi_worker_unref(*(threads + i));
250
+ }
251
+
252
+
253
+ #ifndef _WIN32
254
+ static void reset_once(void) {
255
+ uv_once_t child_once = UV_ONCE_INIT;
256
+ memcpy(&once, &child_once, sizeof(child_once));
257
+ }
258
+ #endif
259
+
260
+
261
+ static void init_once(void) {
262
+ #ifndef _WIN32
263
+ /* Re-initialize the threadpool after fork.
264
+ * Note that this discards the global mutex and condition as well
265
+ * as the work queue.
266
+ */
267
+ if (pthread_atfork(NULL, NULL, &reset_once))
268
+ abort();
269
+ #endif
270
+ init_threads();
271
+ }
272
+
273
+
274
+ void uv__work_submit(uv_loop_t* loop,
275
+ struct uv__work* w,
276
+ enum uv__work_kind kind,
277
+ void (*work)(struct uv__work* w),
278
+ void (*done)(struct uv__work* w, int status)) {
279
+ uv_once(&once, init_once);
280
+ w->loop = loop;
281
+ w->work = work;
282
+ w->done = done;
283
+ post(&w->wq, kind);
284
+ }
285
+
286
+
287
+ static int uv__work_cancel(uv_loop_t* loop, uv_req_t* req, struct uv__work* w) {
288
+ int cancelled;
289
+
290
+ uv_mutex_lock(&mutex);
291
+ uv_mutex_lock(&w->loop->wq_mutex);
292
+
293
+ cancelled = !QUEUE_EMPTY(&w->wq) && w->work != NULL;
294
+ if (cancelled)
295
+ QUEUE_REMOVE(&w->wq);
296
+
297
+ uv_mutex_unlock(&w->loop->wq_mutex);
298
+ uv_mutex_unlock(&mutex);
299
+
300
+ if (!cancelled)
301
+ return EBUSY;
302
+
303
+ w->work = uv__cancelled;
304
+ uv_mutex_lock(&loop->wq_mutex);
305
+ QUEUE_INSERT_TAIL(&loop->wq, &w->wq);
306
+ uv_async_send(&loop->wq_async);
307
+ uv_mutex_unlock(&loop->wq_mutex);
308
+
309
+ return 0;
310
+ }
311
+
312
+
313
+ void uv__work_done(uv_async_t* handle) {
314
+ struct uv__work* w;
315
+ uv_loop_t* loop;
316
+ QUEUE* q;
317
+ QUEUE wq;
318
+ int err;
319
+
320
+ loop = container_of(handle, uv_loop_t, wq_async);
321
+ uv_mutex_lock(&loop->wq_mutex);
322
+ QUEUE_MOVE(&loop->wq, &wq);
323
+ uv_mutex_unlock(&loop->wq_mutex);
324
+
325
+ while (!QUEUE_EMPTY(&wq)) {
326
+ q = QUEUE_HEAD(&wq);
327
+ QUEUE_REMOVE(q);
328
+
329
+ w = container_of(q, struct uv__work, wq);
330
+ err = (w->work == uv__cancelled) ? ECANCELED : 0;
331
+ w->done(w, err);
332
+ }
333
+ }
334
+
335
+
336
+ static void uv__queue_work(struct uv__work* w) {
337
+ uv_work_t* req = container_of(w, uv_work_t, work_req);
338
+
339
+ req->work_cb(req);
340
+ }
341
+
342
+
343
+ static void uv__queue_done(struct uv__work* w, int err) {
344
+ uv_work_t* req;
345
+
346
+ req = container_of(w, uv_work_t, work_req);
347
+ uv__req_unregister(req->loop, req);
348
+
349
+ if (req->after_work_cb == NULL)
350
+ return;
351
+
352
+ req->after_work_cb(req, err);
353
+ }
354
+
355
+
356
+ int uv_queue_work(uv_loop_t* loop,
357
+ uv_work_t* req,
358
+ uv_work_cb work_cb,
359
+ uv_after_work_cb after_work_cb) {
360
+ if (work_cb == NULL)
361
+ return EINVAL;
362
+
363
+ uv__req_init(loop, req, UV_WORK);
364
+ req->loop = loop;
365
+ req->work_cb = work_cb;
366
+ req->after_work_cb = after_work_cb;
367
+ uv__work_submit(loop,
368
+ &req->work_req,
369
+ UV__WORK_CPU,
370
+ uv__queue_work,
371
+ uv__queue_done);
372
+ return 0;
373
+ }
374
+
375
+
376
+ int uv_cancel(uv_req_t* req) {
377
+ struct uv__work* wreq;
378
+ uv_loop_t* loop;
379
+
380
+ switch (req->type) {
381
+ // case UV_FS:
382
+ // loop = ((uv_fs_t*) req)->loop;
383
+ // wreq = &((uv_fs_t*) req)->work_req;
384
+ // break;
385
+ // case UV_GETADDRINFO:
386
+ // loop = ((uv_getaddrinfo_t*) req)->loop;
387
+ // wreq = &((uv_getaddrinfo_t*) req)->work_req;
388
+ // break;
389
+ // case UV_GETNAMEINFO:
390
+ // loop = ((uv_getnameinfo_t*) req)->loop;
391
+ // wreq = &((uv_getnameinfo_t*) req)->work_req;
392
+ // break;
393
+ // case UV_RANDOM:
394
+ // loop = ((uv_random_t*) req)->loop;
395
+ // wreq = &((uv_random_t*) req)->work_req;
396
+ // break;
397
+ case UV_WORK:
398
+ loop = ((uv_work_t*) req)->loop;
399
+ wreq = &((uv_work_t*) req)->work_req;
400
+ break;
401
+ default:
402
+ return EINVAL;
403
+ }
404
+
405
+ return uv__work_cancel(loop, req, wreq);
406
+ }
407
+
408
+ #endif
@@ -0,0 +1,206 @@
1
+ /* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
2
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
3
+ * of this software and associated documentation files (the "Software"), to
4
+ * deal in the Software without restriction, including without limitation the
5
+ * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6
+ * sell copies of the Software, and to permit persons to whom the Software is
7
+ * furnished to do so, subject to the following conditions:
8
+ *
9
+ * The above copyright notice and this permission notice shall be included in
10
+ * all copies or substantial portions of the Software.
11
+ *
12
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
18
+ * IN THE SOFTWARE.
19
+ */
20
+
21
+ /* This file contains both the uv__async internal infrastructure and the
22
+ * user-facing uv_async_t functions.
23
+ */
24
+
25
+ #if defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)
26
+
27
+ #include <stdlib.h>
28
+ #include <sched.h>
29
+ #include "../uv-common.h"
30
+ #include "common.h"
31
+
32
+ #if defined(__clang__) || \
33
+ defined(__GNUC__) || \
34
+ defined(__INTEL_COMPILER)
35
+ # define UV_UNUSED(declaration) __attribute__((unused)) declaration
36
+ #else
37
+ # define UV_UNUSED(declaration) declaration
38
+ #endif
39
+
40
+ UV_UNUSED(static int cmpxchgi(int* ptr, int oldval, int newval));
41
+
42
+ UV_UNUSED(static int cmpxchgi(int* ptr, int oldval, int newval)) {
43
+ return __sync_val_compare_and_swap(ptr, oldval, newval);
44
+ }
45
+
46
+ #ifndef EMNAPI_NEXTTICK_TYPE
47
+ #define EMNAPI_NEXTTICK_TYPE 0
48
+ #endif
49
+ #if EMNAPI_NEXTTICK_TYPE == 0
50
+ EMNAPI_INTERNAL_EXTERN void _emnapi_set_immediate(void (*callback)(void*), void* data);
51
+ #define NEXT_TICK(callback, data) _emnapi_set_immediate((callback), (data))
52
+ #elif EMNAPI_NEXTTICK_TYPE == 1
53
+ EMNAPI_INTERNAL_EXTERN void _emnapi_next_tick(void (*callback)(void*), void* data);
54
+ #define NEXT_TICK(callback, data) _emnapi_next_tick((callback), (data))
55
+ #else
56
+ #error "Invalid EMNAPI_NEXTTICK_TYPE"
57
+ #endif
58
+
59
+ #if EMNAPI_USE_PROXYING
60
+ #include <emscripten/threading.h>
61
+ #include <emscripten/proxying.h>
62
+ #include <errno.h>
63
+
64
+ int _emnapi_create_proxying_queue(uv_loop_t* loop) {
65
+ em_proxying_queue* queue = em_proxying_queue_create();
66
+ loop->em_queue = queue;
67
+ if (queue == NULL) return ENOMEM;
68
+ return 0;
69
+ }
70
+
71
+ void _emnapi_destroy_proxying_queue(uv_loop_t* loop) {
72
+ if (loop->em_queue != NULL) {
73
+ em_proxying_queue_destroy(loop->em_queue);
74
+ }
75
+ }
76
+
77
+ #else
78
+ EMNAPI_INTERNAL_EXTERN void _emnapi_async_send_js(int type,
79
+ void (*callback)(void*),
80
+ void* data);
81
+
82
+ int _emnapi_create_proxying_queue(uv_loop_t* loop) {
83
+ loop->em_queue = NULL;
84
+ return 0;
85
+ }
86
+
87
+ void _emnapi_destroy_proxying_queue(uv_loop_t* loop) {}
88
+
89
+ #endif
90
+
91
+ int uv_async_init(uv_loop_t* loop, uv_async_t* handle, uv_async_cb async_cb) {
92
+ handle->loop = loop;
93
+ handle->type = UV_ASYNC;
94
+ handle->async_cb = async_cb;
95
+ handle->pending = 0;
96
+ QUEUE_INSERT_TAIL(&loop->async_handles, &handle->queue);
97
+ return 0;
98
+ }
99
+
100
+ /* Only call this from the event loop thread. */
101
+ static int uv__async_spin(uv_async_t* handle) {
102
+ int i;
103
+ int rc;
104
+
105
+ for (;;) {
106
+ /* 997 is not completely chosen at random. It's a prime number, acyclical
107
+ * by nature, and should therefore hopefully dampen sympathetic resonance.
108
+ */
109
+ for (i = 0; i < 997; i++) {
110
+ /* rc=0 -- handle is not pending.
111
+ * rc=1 -- handle is pending, other thread is still working with it.
112
+ * rc=2 -- handle is pending, other thread is done.
113
+ */
114
+ rc = cmpxchgi(&handle->pending, 2, 0);
115
+
116
+ if (rc != 1)
117
+ return rc;
118
+
119
+ /* Other thread is busy with this handle, spin until it's done. */
120
+ // cpu_relax();
121
+ }
122
+
123
+ /* Yield the CPU. We may have preempted the other thread while it's
124
+ * inside the critical section and if it's running on the same CPU
125
+ * as us, we'll just burn CPU cycles until the end of our time slice.
126
+ */
127
+ sched_yield();
128
+ }
129
+ }
130
+
131
+ static void uv__async_io(uv_loop_t* loop) {
132
+ QUEUE queue;
133
+ QUEUE* q;
134
+ uv_async_t* h;
135
+
136
+ QUEUE_MOVE(&loop->async_handles, &queue);
137
+ while (!QUEUE_EMPTY(&queue)) {
138
+ q = QUEUE_HEAD(&queue);
139
+ h = QUEUE_DATA(q, uv_async_t, queue);
140
+
141
+ QUEUE_REMOVE(q);
142
+ QUEUE_INSERT_TAIL(&loop->async_handles, q);
143
+
144
+ if (0 == uv__async_spin(h))
145
+ continue; /* Not pending. */
146
+
147
+ if (h->async_cb == NULL)
148
+ continue;
149
+
150
+ h->async_cb(h);
151
+ }
152
+ }
153
+
154
+ static void uv__async_send(uv_loop_t* loop) {
155
+ #if EMNAPI_USE_PROXYING
156
+ pthread_t main_thread = emscripten_main_browser_thread_id();
157
+ if (pthread_equal(main_thread, pthread_self())) {
158
+ NEXT_TICK((void (*)(void *))uv__async_io, loop);
159
+ } else {
160
+ // Neither emscripten_dispatch_to_thread_async nor MAIN_THREAD_ASYNC_EM_ASM
161
+ // invoke the async_cb callback if there is a printf() in worker thread.
162
+ // Using emscripten_proxy_async(emscripten_proxy_get_system_queue(), ...)
163
+ // also has the same problem. Not sure what happens.
164
+ // But creating a new queue is all ok.
165
+ if (!emscripten_proxy_async(loop->em_queue,
166
+ main_thread,
167
+ (void (*)(void *))uv__async_io,
168
+ loop)) {
169
+ abort();
170
+ }
171
+ }
172
+ #else
173
+ _emnapi_async_send_js(EMNAPI_NEXTTICK_TYPE,
174
+ (void (*)(void *))uv__async_io,
175
+ loop);
176
+ #endif
177
+ }
178
+
179
+ #define ACCESS_ONCE(type, var) (*(volatile type*) &(var))
180
+
181
+ int uv_async_send(uv_async_t* handle) {
182
+ /* Do a cheap read first. */
183
+ if (ACCESS_ONCE(int, handle->pending) != 0)
184
+ return 0;
185
+
186
+ /* Tell the other thread we're busy with the handle. */
187
+ if (cmpxchgi(&handle->pending, 0, 1) != 0)
188
+ return 0;
189
+
190
+ /* Wake up the other thread's event loop. */
191
+ uv__async_send(handle->loop);
192
+
193
+ /* Tell the other thread we're done. */
194
+ if (cmpxchgi(&handle->pending, 1, 2) != 1)
195
+ abort();
196
+
197
+ return 0;
198
+ }
199
+
200
+ void uv__async_close(uv_async_t* handle) {
201
+ uv__async_spin(handle);
202
+ QUEUE_REMOVE(&handle->queue);
203
+ NEXT_TICK(((void (*)(void *))handle->close_cb), handle);
204
+ }
205
+
206
+ #endif
@@ -0,0 +1,35 @@
1
+ #if defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)
2
+
3
+ #include <errno.h>
4
+ #include <time.h>
5
+ #include "../uv-common.h"
6
+
7
+ void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
8
+ handle->close_cb = close_cb;
9
+
10
+ switch (handle->type) {
11
+ case UV_ASYNC:
12
+ uv__async_close((uv_async_t*)handle);
13
+ break;
14
+ default:
15
+ assert(0);
16
+ }
17
+ }
18
+
19
+ int nanosleep(const struct timespec *, struct timespec *);
20
+
21
+ void uv_sleep(unsigned int msec) {
22
+ struct timespec timeout;
23
+ int rc;
24
+
25
+ timeout.tv_sec = msec / 1000;
26
+ timeout.tv_nsec = (msec % 1000) * 1000 * 1000;
27
+
28
+ do
29
+ rc = nanosleep(&timeout, &timeout);
30
+ while (rc == -1 && errno == EINTR);
31
+
32
+ assert(rc == 0);
33
+ }
34
+
35
+ #endif
@@ -0,0 +1,36 @@
1
+ #if defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)
2
+
3
+ #include "../uv-common.h"
4
+
5
+ int _emnapi_create_proxying_queue(uv_loop_t* loop);
6
+ void _emnapi_destroy_proxying_queue(uv_loop_t* loop);
7
+
8
+ int uv_loop_init(uv_loop_t* loop) {
9
+ int err;
10
+ QUEUE_INIT(&loop->wq);
11
+ QUEUE_INIT(&loop->async_handles);
12
+ err = _emnapi_create_proxying_queue(loop);
13
+ if (err) return err;
14
+ err = uv_mutex_init(&loop->wq_mutex);
15
+ if (err) goto fail_mutex_init;
16
+ err = uv_async_init(loop, &loop->wq_async, uv__work_done);
17
+ if (err) goto fail_async_init;
18
+ return 0;
19
+
20
+ fail_async_init:
21
+ uv_mutex_destroy(&loop->wq_mutex);
22
+
23
+ fail_mutex_init:
24
+ return err;
25
+ }
26
+
27
+ void uv__loop_close(uv_loop_t* loop) {
28
+ uv_mutex_lock(&loop->wq_mutex);
29
+ assert(QUEUE_EMPTY(&loop->wq) && "thread pool work queue not empty!");
30
+ assert(!uv__has_active_reqs(loop));
31
+ _emnapi_destroy_proxying_queue(loop);
32
+ uv_mutex_unlock(&loop->wq_mutex);
33
+ uv_mutex_destroy(&loop->wq_mutex);
34
+ }
35
+
36
+ #endif