emnapi 1.2.0 → 1.3.1
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.
- package/CMakeLists.txt +13 -3
- package/README.md +7 -18
- package/common.gypi +1 -0
- package/dist/library_napi.js +130 -41
- package/emnapi.gyp +4 -0
- package/include/node/emnapi.h +2 -2
- package/include/node/js_native_api.h +30 -25
- package/include/node/js_native_api_types.h +13 -9
- package/include/node/node_api.h +25 -13
- package/include/node/uv/threadpool.h +1 -1
- package/include/node/uv/unix.h +4 -0
- package/include/node/uv.h +49 -6
- package/index.js +1 -0
- package/lib/wasm32/libemnapi.a +0 -0
- package/lib/wasm32-emscripten/libemnapi-mt.a +0 -0
- package/lib/wasm32-emscripten/libemnapi.a +0 -0
- package/lib/wasm32-wasi/libemnapi.a +0 -0
- package/lib/wasm32-wasi-threads/libemnapi-basic-mt.a +0 -0
- package/lib/wasm32-wasi-threads/libemnapi-basic.a +0 -0
- package/lib/wasm32-wasi-threads/libemnapi-mt.a +0 -0
- package/lib/wasm32-wasi-threads/libemnapi.a +0 -0
- package/lib/wasm32-wasip1/libemnapi.a +0 -0
- package/lib/wasm32-wasip1-threads/libemnapi-basic-mt.a +0 -0
- package/lib/wasm32-wasip1-threads/libemnapi-basic.a +0 -0
- package/lib/wasm32-wasip1-threads/libemnapi-mt.a +0 -0
- package/lib/wasm32-wasip1-threads/libemnapi.a +0 -0
- package/lib/wasm64-emscripten/libemnapi-mt.a +0 -0
- package/lib/wasm64-emscripten/libemnapi.a +0 -0
- package/package.json +1 -1
- package/src/async_cleanup_hook.c +1 -1
- package/src/async_work.c +2 -2
- package/src/emnapi_internal.h +2 -2
- package/src/js_native_api.c +2 -1
- package/src/node_api.c +5 -5
- package/src/threadsafe_function.c +20 -21
- package/src/uv/queue.h +68 -86
- package/src/uv/threadpool.c +58 -40
- package/src/uv/unix/async.c +67 -64
- package/src/uv/unix/core.c +36 -1
- package/src/uv/unix/internal.h +54 -0
- package/src/uv/unix/loop.c +40 -4
- package/src/uv/unix/posix-hrtime.c +40 -0
- package/src/uv/uv-common.c +123 -7
- package/src/uv/uv-common.h +137 -9
package/src/uv/unix/async.c
CHANGED
|
@@ -24,38 +24,14 @@
|
|
|
24
24
|
|
|
25
25
|
#if defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)
|
|
26
26
|
|
|
27
|
+
#include "uv.h"
|
|
28
|
+
#include "internal.h"
|
|
29
|
+
|
|
30
|
+
#include <stdatomic.h>
|
|
27
31
|
#include <stdlib.h>
|
|
28
32
|
#include <sched.h>
|
|
29
|
-
#include "../uv-common.h"
|
|
30
33
|
#include "emnapi_common.h"
|
|
31
34
|
|
|
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
35
|
#if EMNAPI_USE_PROXYING
|
|
60
36
|
#include <emscripten/threading.h>
|
|
61
37
|
#include <emscripten/proxying.h>
|
|
@@ -88,36 +64,43 @@ void _emnapi_destroy_proxying_queue(uv_loop_t* loop) {}
|
|
|
88
64
|
|
|
89
65
|
#endif
|
|
90
66
|
|
|
67
|
+
static void uv__async_send(uv_loop_t* loop);
|
|
68
|
+
static void uv__cpu_relax(void);
|
|
69
|
+
|
|
91
70
|
int uv_async_init(uv_loop_t* loop, uv_async_t* handle, uv_async_cb async_cb) {
|
|
92
|
-
|
|
93
|
-
handle->type = UV_ASYNC;
|
|
71
|
+
uv__handle_init(loop, (uv_handle_t*)handle, UV_ASYNC);
|
|
94
72
|
handle->async_cb = async_cb;
|
|
95
73
|
handle->pending = 0;
|
|
96
|
-
|
|
74
|
+
handle->u.fd = 0; /* This will be used as a busy flag. */
|
|
75
|
+
|
|
76
|
+
uv__queue_insert_tail(&loop->async_handles, &handle->queue);
|
|
77
|
+
uv__handle_start(handle);
|
|
97
78
|
return 0;
|
|
98
79
|
}
|
|
99
80
|
|
|
100
81
|
/* Only call this from the event loop thread. */
|
|
101
|
-
static
|
|
82
|
+
static void uv__async_spin(uv_async_t* handle) {
|
|
83
|
+
_Atomic int* pending;
|
|
84
|
+
_Atomic int* busy;
|
|
102
85
|
int i;
|
|
103
|
-
|
|
86
|
+
|
|
87
|
+
pending = (_Atomic int*) &handle->pending;
|
|
88
|
+
busy = (_Atomic int*) &handle->u.fd;
|
|
89
|
+
|
|
90
|
+
/* Set the pending flag first, so no new events will be added by other
|
|
91
|
+
* threads after this function returns. */
|
|
92
|
+
atomic_store(pending, 1);
|
|
104
93
|
|
|
105
94
|
for (;;) {
|
|
106
|
-
/* 997 is not completely chosen at random. It's a prime number,
|
|
107
|
-
*
|
|
95
|
+
/* 997 is not completely chosen at random. It's a prime number, acyclic by
|
|
96
|
+
* nature, and should therefore hopefully dampen sympathetic resonance.
|
|
108
97
|
*/
|
|
109
98
|
for (i = 0; i < 997; i++) {
|
|
110
|
-
|
|
111
|
-
|
|
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;
|
|
99
|
+
if (atomic_load(busy) == 0)
|
|
100
|
+
return;
|
|
118
101
|
|
|
119
102
|
/* Other thread is busy with this handle, spin until it's done. */
|
|
120
|
-
|
|
103
|
+
uv__cpu_relax();
|
|
121
104
|
}
|
|
122
105
|
|
|
123
106
|
/* Yield the CPU. We may have preempted the other thread while it's
|
|
@@ -129,20 +112,23 @@ static int uv__async_spin(uv_async_t* handle) {
|
|
|
129
112
|
}
|
|
130
113
|
|
|
131
114
|
static void uv__async_io(uv_loop_t* loop) {
|
|
132
|
-
|
|
133
|
-
|
|
115
|
+
struct uv__queue queue;
|
|
116
|
+
struct uv__queue* q;
|
|
134
117
|
uv_async_t* h;
|
|
118
|
+
_Atomic int *pending;
|
|
135
119
|
|
|
136
|
-
|
|
137
|
-
while (!
|
|
138
|
-
q =
|
|
139
|
-
h =
|
|
120
|
+
uv__queue_move(&loop->async_handles, &queue);
|
|
121
|
+
while (!uv__queue_empty(&queue)) {
|
|
122
|
+
q = uv__queue_head(&queue);
|
|
123
|
+
h = uv__queue_data(q, uv_async_t, queue);
|
|
140
124
|
|
|
141
|
-
|
|
142
|
-
|
|
125
|
+
uv__queue_remove(q);
|
|
126
|
+
uv__queue_insert_tail(&loop->async_handles, q);
|
|
143
127
|
|
|
144
|
-
|
|
145
|
-
|
|
128
|
+
/* Atomically fetch and clear pending flag */
|
|
129
|
+
pending = (_Atomic int*) &h->pending;
|
|
130
|
+
if (atomic_exchange(pending, 0) == 0)
|
|
131
|
+
continue;
|
|
146
132
|
|
|
147
133
|
if (h->async_cb == NULL)
|
|
148
134
|
continue;
|
|
@@ -195,28 +181,45 @@ static void uv__async_send(uv_loop_t* loop) {
|
|
|
195
181
|
#define ACCESS_ONCE(type, var) (*(volatile type*) &(var))
|
|
196
182
|
|
|
197
183
|
int uv_async_send(uv_async_t* handle) {
|
|
184
|
+
_Atomic int* pending;
|
|
185
|
+
_Atomic int* busy;
|
|
186
|
+
|
|
187
|
+
pending = (_Atomic int*) &handle->pending;
|
|
188
|
+
busy = (_Atomic int*) &handle->u.fd;
|
|
189
|
+
|
|
198
190
|
/* Do a cheap read first. */
|
|
199
|
-
if (
|
|
191
|
+
if (atomic_load_explicit(pending, memory_order_relaxed) != 0)
|
|
200
192
|
return 0;
|
|
201
193
|
|
|
202
|
-
/*
|
|
203
|
-
|
|
204
|
-
return 0;
|
|
194
|
+
/* Set the loop to busy. */
|
|
195
|
+
atomic_fetch_add(busy, 1);
|
|
205
196
|
|
|
206
197
|
/* Wake up the other thread's event loop. */
|
|
207
|
-
|
|
198
|
+
if (atomic_exchange(pending, 1) == 0)
|
|
199
|
+
uv__async_send(handle->loop);
|
|
208
200
|
|
|
209
|
-
/*
|
|
210
|
-
|
|
211
|
-
abort();
|
|
201
|
+
/* Set the loop to not-busy. */
|
|
202
|
+
atomic_fetch_add(busy, -1);
|
|
212
203
|
|
|
213
204
|
return 0;
|
|
214
205
|
}
|
|
215
206
|
|
|
216
207
|
void uv__async_close(uv_async_t* handle) {
|
|
217
208
|
uv__async_spin(handle);
|
|
218
|
-
|
|
219
|
-
|
|
209
|
+
uv__queue_remove(&handle->queue);
|
|
210
|
+
uv__handle_stop(handle);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
static void uv__cpu_relax(void) {
|
|
214
|
+
#if defined(__i386__) || defined(__x86_64__)
|
|
215
|
+
__asm__ __volatile__ ("rep; nop" ::: "memory"); /* a.k.a. PAUSE */
|
|
216
|
+
#elif (defined(__arm__) && __ARM_ARCH >= 7) || defined(__aarch64__)
|
|
217
|
+
__asm__ __volatile__ ("yield" ::: "memory");
|
|
218
|
+
#elif (defined(__ppc__) || defined(__ppc64__)) && defined(__APPLE__)
|
|
219
|
+
__asm volatile ("" : : : "memory");
|
|
220
|
+
#elif !defined(__APPLE__) && (defined(__powerpc64__) || defined(__ppc64__) || defined(__PPC64__))
|
|
221
|
+
__asm__ __volatile__ ("or 1,1,1; or 2,2,2" ::: "memory");
|
|
222
|
+
#endif
|
|
220
223
|
}
|
|
221
224
|
|
|
222
225
|
#endif
|
package/src/uv/unix/core.c
CHANGED
|
@@ -1,10 +1,18 @@
|
|
|
1
1
|
#if defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)
|
|
2
2
|
|
|
3
|
+
#include "uv.h"
|
|
4
|
+
#include "internal.h"
|
|
3
5
|
#include <errno.h>
|
|
4
6
|
#include <time.h>
|
|
5
|
-
|
|
7
|
+
|
|
8
|
+
uint64_t uv_hrtime(void) {
|
|
9
|
+
return uv__hrtime(UV_CLOCK_PRECISE);
|
|
10
|
+
}
|
|
6
11
|
|
|
7
12
|
void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
|
|
13
|
+
assert(!uv__is_closing(handle));
|
|
14
|
+
|
|
15
|
+
handle->flags |= UV_HANDLE_CLOSING;
|
|
8
16
|
handle->close_cb = close_cb;
|
|
9
17
|
|
|
10
18
|
switch (handle->type) {
|
|
@@ -14,6 +22,33 @@ void uv_close(uv_handle_t* handle, uv_close_cb close_cb) {
|
|
|
14
22
|
default:
|
|
15
23
|
assert(0);
|
|
16
24
|
}
|
|
25
|
+
|
|
26
|
+
uv__make_close_pending(handle);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
static void uv__finish_close(uv_handle_t* handle) {
|
|
30
|
+
assert(handle->flags & UV_HANDLE_CLOSING);
|
|
31
|
+
assert(!(handle->flags & UV_HANDLE_CLOSED));
|
|
32
|
+
handle->flags |= UV_HANDLE_CLOSED;
|
|
33
|
+
|
|
34
|
+
uv__handle_unref(handle);
|
|
35
|
+
uv__queue_remove(&handle->handle_queue);
|
|
36
|
+
|
|
37
|
+
if (handle->close_cb) {
|
|
38
|
+
handle->close_cb(handle);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
void uv__make_close_pending(uv_handle_t* handle) {
|
|
43
|
+
assert(handle->flags & UV_HANDLE_CLOSING);
|
|
44
|
+
assert(!(handle->flags & UV_HANDLE_CLOSED));
|
|
45
|
+
// handle->next_closing = handle->loop->closing_handles;
|
|
46
|
+
// handle->loop->closing_handles = handle;
|
|
47
|
+
NEXT_TICK(((void (*)(void *))uv__finish_close), handle);
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
int uv_is_closing(const uv_handle_t* handle) {
|
|
51
|
+
return uv__is_closing(handle);
|
|
17
52
|
}
|
|
18
53
|
|
|
19
54
|
int nanosleep(const struct timespec *, struct timespec *);
|
|
@@ -0,0 +1,54 @@
|
|
|
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
|
+
#ifndef UV_UNIX_INTERNAL_H_
|
|
23
|
+
#define UV_UNIX_INTERNAL_H_
|
|
24
|
+
|
|
25
|
+
#include "../uv-common.h"
|
|
26
|
+
#include "emnapi_common.h"
|
|
27
|
+
#include <stdint.h>
|
|
28
|
+
|
|
29
|
+
#ifndef EMNAPI_NEXTTICK_TYPE
|
|
30
|
+
#define EMNAPI_NEXTTICK_TYPE 0
|
|
31
|
+
#endif
|
|
32
|
+
#if EMNAPI_NEXTTICK_TYPE == 0
|
|
33
|
+
EMNAPI_INTERNAL_EXTERN void _emnapi_set_immediate(void (*callback)(void*), void* data);
|
|
34
|
+
#define NEXT_TICK(callback, data) _emnapi_set_immediate((callback), (data))
|
|
35
|
+
#elif EMNAPI_NEXTTICK_TYPE == 1
|
|
36
|
+
EMNAPI_INTERNAL_EXTERN void _emnapi_next_tick(void (*callback)(void*), void* data);
|
|
37
|
+
#define NEXT_TICK(callback, data) _emnapi_next_tick((callback), (data))
|
|
38
|
+
#else
|
|
39
|
+
#error "Invalid EMNAPI_NEXTTICK_TYPE"
|
|
40
|
+
#endif
|
|
41
|
+
|
|
42
|
+
typedef enum {
|
|
43
|
+
UV_CLOCK_PRECISE = 0, /* Use the highest resolution clock available. */
|
|
44
|
+
UV_CLOCK_FAST = 1 /* Use the fastest clock with <= 1ms granularity. */
|
|
45
|
+
} uv_clocktype_t;
|
|
46
|
+
|
|
47
|
+
uint64_t uv__hrtime(uv_clocktype_t type);
|
|
48
|
+
|
|
49
|
+
#if defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)
|
|
50
|
+
void uv__async_close(uv_async_t* handle);
|
|
51
|
+
void uv__make_close_pending(uv_handle_t* handle);
|
|
52
|
+
#endif
|
|
53
|
+
|
|
54
|
+
#endif /* UV_UNIX_INTERNAL_H_ */
|
package/src/uv/unix/loop.c
CHANGED
|
@@ -1,16 +1,38 @@
|
|
|
1
1
|
#if defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)
|
|
2
2
|
|
|
3
3
|
#include "../uv-common.h"
|
|
4
|
+
#include <errno.h>
|
|
5
|
+
#include <string.h>
|
|
4
6
|
|
|
5
7
|
int _emnapi_create_proxying_queue(uv_loop_t* loop);
|
|
6
8
|
void _emnapi_destroy_proxying_queue(uv_loop_t* loop);
|
|
7
9
|
|
|
8
10
|
int uv_loop_init(uv_loop_t* loop) {
|
|
11
|
+
uv__loop_internal_fields_t* lfields;
|
|
12
|
+
void* saved_data;
|
|
9
13
|
int err;
|
|
10
|
-
|
|
11
|
-
|
|
14
|
+
|
|
15
|
+
saved_data = loop->data;
|
|
16
|
+
memset(loop, 0, sizeof(*loop));
|
|
17
|
+
loop->data = saved_data;
|
|
18
|
+
|
|
19
|
+
lfields = (uv__loop_internal_fields_t*) uv__calloc(1, sizeof(*lfields));
|
|
20
|
+
if (lfields == NULL)
|
|
21
|
+
return ENOMEM;
|
|
22
|
+
loop->internal_fields = lfields;
|
|
23
|
+
|
|
24
|
+
err = uv_mutex_init(&lfields->loop_metrics.lock);
|
|
25
|
+
if (err)
|
|
26
|
+
goto fail_metrics_mutex_init;
|
|
27
|
+
memset(&lfields->loop_metrics.metrics,
|
|
28
|
+
0,
|
|
29
|
+
sizeof(lfields->loop_metrics.metrics));
|
|
30
|
+
|
|
31
|
+
uv__queue_init(&loop->wq);
|
|
32
|
+
uv__queue_init(&loop->async_handles);
|
|
33
|
+
uv__queue_init(&loop->handle_queue);
|
|
12
34
|
err = _emnapi_create_proxying_queue(loop);
|
|
13
|
-
if (err)
|
|
35
|
+
if (err) goto fail_proxying_queue_init;
|
|
14
36
|
err = uv_mutex_init(&loop->wq_mutex);
|
|
15
37
|
if (err) goto fail_mutex_init;
|
|
16
38
|
err = uv_async_init(loop, &loop->wq_async, uv__work_done);
|
|
@@ -21,16 +43,30 @@ fail_async_init:
|
|
|
21
43
|
uv_mutex_destroy(&loop->wq_mutex);
|
|
22
44
|
|
|
23
45
|
fail_mutex_init:
|
|
46
|
+
|
|
47
|
+
fail_proxying_queue_init:
|
|
48
|
+
|
|
49
|
+
fail_metrics_mutex_init:
|
|
50
|
+
uv__free(lfields);
|
|
51
|
+
loop->internal_fields = NULL;
|
|
52
|
+
|
|
24
53
|
return err;
|
|
25
54
|
}
|
|
26
55
|
|
|
27
56
|
void uv__loop_close(uv_loop_t* loop) {
|
|
57
|
+
uv__loop_internal_fields_t* lfields;
|
|
58
|
+
|
|
28
59
|
uv_mutex_lock(&loop->wq_mutex);
|
|
29
|
-
assert(
|
|
60
|
+
assert(uv__queue_empty(&loop->wq) && "thread pool work queue not empty!");
|
|
30
61
|
assert(!uv__has_active_reqs(loop));
|
|
31
62
|
_emnapi_destroy_proxying_queue(loop);
|
|
32
63
|
uv_mutex_unlock(&loop->wq_mutex);
|
|
33
64
|
uv_mutex_destroy(&loop->wq_mutex);
|
|
65
|
+
|
|
66
|
+
lfields = uv__get_internal_fields(loop);
|
|
67
|
+
uv_mutex_destroy(&lfields->loop_metrics.lock);
|
|
68
|
+
uv__free(lfields);
|
|
69
|
+
loop->internal_fields = NULL;
|
|
34
70
|
}
|
|
35
71
|
|
|
36
72
|
#endif
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/* Copyright libuv project 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
|
+
#if !defined(__wasm__) || (defined(__EMSCRIPTEN__) || defined(__wasi__))
|
|
23
|
+
|
|
24
|
+
#include "uv.h"
|
|
25
|
+
#include "internal.h"
|
|
26
|
+
|
|
27
|
+
#include <stdint.h>
|
|
28
|
+
#include <stdlib.h>
|
|
29
|
+
#include <time.h>
|
|
30
|
+
|
|
31
|
+
uint64_t uv__hrtime(uv_clocktype_t type) {
|
|
32
|
+
struct timespec t;
|
|
33
|
+
|
|
34
|
+
if (clock_gettime(CLOCK_MONOTONIC, &t))
|
|
35
|
+
abort();
|
|
36
|
+
|
|
37
|
+
return t.tv_sec * (uint64_t) 1e9 + t.tv_nsec;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
#endif
|
package/src/uv/uv-common.c
CHANGED
|
@@ -1,9 +1,101 @@
|
|
|
1
1
|
#if defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)
|
|
2
2
|
|
|
3
3
|
#include <errno.h>
|
|
4
|
+
#include <stddef.h>
|
|
5
|
+
#include <stdlib.h>
|
|
4
6
|
#include <string.h>
|
|
5
7
|
#include "uv-common.h"
|
|
6
8
|
|
|
9
|
+
typedef struct {
|
|
10
|
+
uv_malloc_func local_malloc;
|
|
11
|
+
uv_realloc_func local_realloc;
|
|
12
|
+
uv_calloc_func local_calloc;
|
|
13
|
+
uv_free_func local_free;
|
|
14
|
+
} uv__allocator_t;
|
|
15
|
+
|
|
16
|
+
static uv__allocator_t uv__allocator = {
|
|
17
|
+
malloc,
|
|
18
|
+
realloc,
|
|
19
|
+
calloc,
|
|
20
|
+
free,
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
char* uv__strdup(const char* s) {
|
|
24
|
+
size_t len = strlen(s) + 1;
|
|
25
|
+
char* m = uv__malloc(len);
|
|
26
|
+
if (m == NULL)
|
|
27
|
+
return NULL;
|
|
28
|
+
return memcpy(m, s, len);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
char* uv__strndup(const char* s, size_t n) {
|
|
32
|
+
char* m;
|
|
33
|
+
size_t len = strlen(s);
|
|
34
|
+
if (n < len)
|
|
35
|
+
len = n;
|
|
36
|
+
m = uv__malloc(len + 1);
|
|
37
|
+
if (m == NULL)
|
|
38
|
+
return NULL;
|
|
39
|
+
m[len] = '\0';
|
|
40
|
+
return memcpy(m, s, len);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
void* uv__malloc(size_t size) {
|
|
44
|
+
if (size > 0)
|
|
45
|
+
return uv__allocator.local_malloc(size);
|
|
46
|
+
return NULL;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
void uv__free(void* ptr) {
|
|
50
|
+
int saved_errno;
|
|
51
|
+
|
|
52
|
+
/* Libuv expects that free() does not clobber errno. The system allocator
|
|
53
|
+
* honors that assumption but custom allocators may not be so careful.
|
|
54
|
+
*/
|
|
55
|
+
saved_errno = errno;
|
|
56
|
+
uv__allocator.local_free(ptr);
|
|
57
|
+
errno = saved_errno;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
void* uv__calloc(size_t count, size_t size) {
|
|
61
|
+
return uv__allocator.local_calloc(count, size);
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
void* uv__realloc(void* ptr, size_t size) {
|
|
65
|
+
if (size > 0)
|
|
66
|
+
return uv__allocator.local_realloc(ptr, size);
|
|
67
|
+
uv__free(ptr);
|
|
68
|
+
return NULL;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
void* uv__reallocf(void* ptr, size_t size) {
|
|
72
|
+
void* newptr;
|
|
73
|
+
|
|
74
|
+
newptr = uv__realloc(ptr, size);
|
|
75
|
+
if (newptr == NULL)
|
|
76
|
+
if (size > 0)
|
|
77
|
+
uv__free(ptr);
|
|
78
|
+
|
|
79
|
+
return newptr;
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
int uv_replace_allocator(uv_malloc_func malloc_func,
|
|
83
|
+
uv_realloc_func realloc_func,
|
|
84
|
+
uv_calloc_func calloc_func,
|
|
85
|
+
uv_free_func free_func) {
|
|
86
|
+
if (malloc_func == NULL || realloc_func == NULL ||
|
|
87
|
+
calloc_func == NULL || free_func == NULL) {
|
|
88
|
+
return EINVAL;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
uv__allocator.local_malloc = malloc_func;
|
|
92
|
+
uv__allocator.local_realloc = realloc_func;
|
|
93
|
+
uv__allocator.local_calloc = calloc_func;
|
|
94
|
+
uv__allocator.local_free = free_func;
|
|
95
|
+
|
|
96
|
+
return 0;
|
|
97
|
+
}
|
|
98
|
+
|
|
7
99
|
static uv_loop_t default_loop_struct;
|
|
8
100
|
static uv_loop_t* default_loop_ptr;
|
|
9
101
|
|
|
@@ -20,8 +112,8 @@ uv_loop_t* uv_default_loop(void) {
|
|
|
20
112
|
}
|
|
21
113
|
|
|
22
114
|
int uv_loop_close(uv_loop_t* loop) {
|
|
23
|
-
|
|
24
|
-
|
|
115
|
+
struct uv__queue* q;
|
|
116
|
+
uv_handle_t* h;
|
|
25
117
|
#ifndef NDEBUG
|
|
26
118
|
void* saved_data;
|
|
27
119
|
#endif
|
|
@@ -29,11 +121,11 @@ int uv_loop_close(uv_loop_t* loop) {
|
|
|
29
121
|
if (uv__has_active_reqs(loop))
|
|
30
122
|
return EBUSY;
|
|
31
123
|
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
124
|
+
uv__queue_foreach(q, &loop->handle_queue) {
|
|
125
|
+
h = uv__queue_data(q, uv_handle_t, handle_queue);
|
|
126
|
+
if (!(h->flags & UV_HANDLE_INTERNAL))
|
|
127
|
+
return EBUSY;
|
|
128
|
+
}
|
|
37
129
|
|
|
38
130
|
uv__loop_close(loop);
|
|
39
131
|
|
|
@@ -67,4 +159,28 @@ void uv_library_shutdown(void) {
|
|
|
67
159
|
// #endif
|
|
68
160
|
}
|
|
69
161
|
|
|
162
|
+
int uv_metrics_info(uv_loop_t* loop, uv_metrics_t* metrics) {
|
|
163
|
+
memcpy(metrics,
|
|
164
|
+
&uv__get_loop_metrics(loop)->metrics,
|
|
165
|
+
sizeof(*metrics));
|
|
166
|
+
|
|
167
|
+
return 0;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
uint64_t uv_metrics_idle_time(uv_loop_t* loop) {
|
|
171
|
+
uv__loop_metrics_t* loop_metrics;
|
|
172
|
+
uint64_t entry_time;
|
|
173
|
+
uint64_t idle_time;
|
|
174
|
+
|
|
175
|
+
loop_metrics = uv__get_loop_metrics(loop);
|
|
176
|
+
uv_mutex_lock(&loop_metrics->lock);
|
|
177
|
+
idle_time = loop_metrics->provider_idle_time;
|
|
178
|
+
entry_time = loop_metrics->provider_entry_time;
|
|
179
|
+
uv_mutex_unlock(&loop_metrics->lock);
|
|
180
|
+
|
|
181
|
+
if (entry_time > 0)
|
|
182
|
+
idle_time += uv_hrtime() - entry_time;
|
|
183
|
+
return idle_time;
|
|
184
|
+
}
|
|
185
|
+
|
|
70
186
|
#endif
|