Skip to content

Commit 6ac98c3

Browse files
authored
update libuv source (#121)
1 parent 2df2818 commit 6ac98c3

File tree

16 files changed

+720
-233
lines changed

16 files changed

+720
-233
lines changed

packages/emnapi/CMakeLists.txt

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ set(UV_SRC
3333
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/uv-common.c"
3434
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/threadpool.c"
3535
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/unix/loop.c"
36+
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/unix/posix-hrtime.c"
3637
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/unix/thread.c"
3738
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/unix/async.c"
3839
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/unix/core.c"
@@ -139,7 +140,10 @@ endif()
139140

140141
add_library(${EMNAPI_BASIC_TARGET_NAME} STATIC ${ENAPI_BASIC_SRC})
141142
target_include_directories(${EMNAPI_BASIC_TARGET_NAME} PUBLIC ${EMNAPI_INCLUDE})
142-
target_compile_definitions(${EMNAPI_BASIC_TARGET_NAME} PUBLIC ${EMNAPI_DEFINES})
143+
target_compile_definitions(${EMNAPI_BASIC_TARGET_NAME}
144+
PUBLIC ${EMNAPI_DEFINES}
145+
PRIVATE "EMNAPI_DISABLE_UV"
146+
)
143147
if(IS_EMSCRIPTEN)
144148
set_target_properties(${EMNAPI_BASIC_TARGET_NAME} PROPERTIES INTERFACE_LINK_DEPENDS ${EMNAPI_JS_LIB})
145149
target_link_options(${EMNAPI_BASIC_TARGET_NAME} INTERFACE "--js-library=${EMNAPI_JS_LIB}")
@@ -159,7 +163,10 @@ if(EMNAPI_BUILD_BASIC_MT)
159163
)
160164
target_compile_options(${EMNAPI_BASIC_MT_TARGET_NAME} PUBLIC "-matomics" "-mbulk-memory")
161165
target_include_directories(${EMNAPI_BASIC_MT_TARGET_NAME} PUBLIC ${EMNAPI_INCLUDE})
162-
target_compile_definitions(${EMNAPI_BASIC_MT_TARGET_NAME} PUBLIC ${EMNAPI_DEFINES})
166+
target_compile_definitions(${EMNAPI_BASIC_MT_TARGET_NAME}
167+
PUBLIC ${EMNAPI_DEFINES}
168+
PRIVATE "EMNAPI_DISABLE_UV"
169+
)
163170

164171
if(IS_EMSCRIPTEN)
165172
set_target_properties(${EMNAPI_BASIC_MT_TARGET_NAME} PROPERTIES INTERFACE_LINK_DEPENDS ${EMNAPI_JS_LIB})
@@ -175,7 +182,10 @@ endif()
175182

176183
if(EMNAPI_BUILD_MT)
177184
add_library(${EMNAPI_MT_TARGET_NAME} STATIC ${EMNAPI_SRC} ${UV_SRC})
178-
target_compile_options(${EMNAPI_MT_TARGET_NAME} PRIVATE ${EMNAPI_MT_CFLAGS})
185+
target_compile_options(${EMNAPI_MT_TARGET_NAME}
186+
PUBLIC "-matomics" "-mbulk-memory"
187+
PRIVATE ${EMNAPI_MT_CFLAGS}
188+
)
179189
target_include_directories(${EMNAPI_MT_TARGET_NAME} PUBLIC ${EMNAPI_INCLUDE})
180190
target_compile_definitions(${EMNAPI_MT_TARGET_NAME} PUBLIC ${EMNAPI_DEFINES})
181191
if(IS_EMSCRIPTEN)

packages/emnapi/emnapi.gyp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@
4747
{
4848
'target_name': 'emnapi_basic',
4949
'type': 'static_library',
50+
'defines': [
51+
'EMNAPI_DISABLE_UV'
52+
],
5053
'sources': [
5154
'src/js_native_api.c',
5255
'src/node_api.c',

packages/emnapi/include/node/uv.h

Lines changed: 49 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,21 @@
33

44
#if defined(__EMSCRIPTEN_PTHREADS__) || defined(_REENTRANT)
55

6-
#include <stddef.h>
7-
#include "uv/unix.h"
8-
96
#ifdef __cplusplus
107
extern "C" {
118
#endif
129

10+
#include <stddef.h>
11+
#include <stdint.h>
12+
13+
/* Internal type, do not use. */
14+
struct uv__queue {
15+
struct uv__queue* next;
16+
struct uv__queue* prev;
17+
};
18+
19+
#include "uv/unix.h"
20+
1321
#define UV_EXTERN /* nothing */
1422

1523
typedef enum {
@@ -29,6 +37,8 @@ typedef struct uv_work_s uv_work_t;
2937
typedef struct uv_handle_s uv_handle_t;
3038
typedef struct uv_async_s uv_async_t;
3139

40+
typedef struct uv_metrics_s uv_metrics_t;
41+
3242
typedef void (*uv_work_cb)(uv_work_t* req);
3343
typedef void (*uv_after_work_cb)(uv_work_t* req, int status);
3444

@@ -40,10 +50,22 @@ struct uv_req_s {
4050
UV_REQ_FIELDS
4151
};
4252

53+
typedef void* (*uv_malloc_func)(size_t size);
54+
typedef void* (*uv_realloc_func)(void* ptr, size_t size);
55+
typedef void* (*uv_calloc_func)(size_t count, size_t size);
56+
typedef void (*uv_free_func)(void* ptr);
57+
4358
UV_EXTERN void uv_library_shutdown(void);
59+
60+
UV_EXTERN int uv_replace_allocator(uv_malloc_func malloc_func,
61+
uv_realloc_func realloc_func,
62+
uv_calloc_func calloc_func,
63+
uv_free_func free_func);
64+
4465
UV_EXTERN uv_loop_t* uv_default_loop(void);
4566
UV_EXTERN int uv_loop_init(uv_loop_t* loop);
4667
UV_EXTERN int uv_loop_close(uv_loop_t* loop);
68+
UV_EXTERN uint64_t uv_hrtime(void);
4769
UV_EXTERN void uv_sleep(unsigned int msec);
4870

4971
UV_EXTERN int uv_sem_init(uv_sem_t* sem, unsigned int value);
@@ -111,6 +133,12 @@ typedef void (*uv_async_cb)(uv_async_t* handle);
111133
uv_loop_t* loop; \
112134
uv_handle_type type; \
113135
uv_close_cb close_cb; \
136+
struct uv__queue handle_queue; \
137+
union { \
138+
int fd; \
139+
void* reserved[4]; \
140+
} u; \
141+
UV_HANDLE_PRIVATE_FIELDS \
114142

115143
struct uv_handle_s {
116144
UV_HANDLE_FIELDS
@@ -119,7 +147,7 @@ struct uv_handle_s {
119147
struct uv_async_s {
120148
UV_HANDLE_FIELDS
121149
uv_async_cb async_cb;
122-
void* queue[2];
150+
struct uv__queue queue;
123151
int pending;
124152
};
125153

@@ -129,18 +157,33 @@ UV_EXTERN int uv_async_init(uv_loop_t*,
129157
UV_EXTERN int uv_async_send(uv_async_t* async);
130158

131159
UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
160+
UV_EXTERN int uv_is_closing(const uv_handle_t* handle);
161+
162+
struct uv_metrics_s {
163+
uint64_t loop_count;
164+
uint64_t events;
165+
uint64_t events_waiting;
166+
/* private */
167+
uint64_t* reserved[13];
168+
};
169+
170+
UV_EXTERN int uv_metrics_info(uv_loop_t* loop, uv_metrics_t* metrics);
171+
UV_EXTERN uint64_t uv_metrics_idle_time(uv_loop_t* loop);
132172

133173
struct uv_loop_s {
134174
void* data;
175+
unsigned int active_handles;
176+
struct uv__queue handle_queue;
135177
union {
136178
void* unused;
137179
unsigned int count;
138180
} active_reqs;
139-
void* wq[2];
181+
void* internal_fields;
140182

183+
struct uv__queue wq;
141184
uv_mutex_t wq_mutex;
142185
uv_async_t wq_async;
143-
void* async_handles[2];
186+
struct uv__queue async_handles;
144187
void* em_queue;
145188
};
146189

packages/emnapi/include/node/uv/threadpool.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ struct uv__work {
3333
void (*work)(struct uv__work *w);
3434
void (*done)(struct uv__work *w, int status);
3535
struct uv_loop_s* loop;
36-
void* wq[2];
36+
struct uv__queue wq;
3737
};
3838

3939
#endif

packages/emnapi/include/node/uv/unix.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,8 @@ typedef pthread_cond_t uv_cond_t;
1818

1919
#endif
2020

21+
#define UV_HANDLE_PRIVATE_FIELDS \
22+
uv_handle_t* next_closing; \
23+
unsigned int flags; \
24+
2125
#endif /* UV_UNIX_H */

packages/emnapi/src/node_api.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include "node_api.h"
22
#include "emnapi_internal.h"
33

4-
#if EMNAPI_HAVE_THREADS
4+
#if EMNAPI_HAVE_THREADS && !defined(EMNAPI_DISABLE_UV)
55
#include "uv.h"
66
#endif
77

@@ -31,7 +31,7 @@ napi_get_node_version(node_api_basic_env env,
3131

3232
napi_status napi_get_uv_event_loop(node_api_basic_env env,
3333
struct uv_loop_s** loop) {
34-
#if EMNAPI_HAVE_THREADS
34+
#if EMNAPI_HAVE_THREADS && !defined(EMNAPI_DISABLE_UV)
3535
CHECK_ENV(env);
3636
CHECK_ARG(env, loop);
3737
// Though this is fake libuv loop

packages/emnapi/src/threadsafe_function.c

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55
#include <stdatomic.h>
66
#include <pthread.h>
77
#include <errno.h>
8-
#include "uv/queue.h"
9-
108
#include "uv.h"
9+
#include "uv/queue.h"
1110

1211
EXTERN_C_START
1312

@@ -21,7 +20,7 @@ static const unsigned int kMaxIterationCount = 1000;
2120

2221
struct data_queue_node {
2322
void* data;
24-
void* q[2];
23+
struct uv__queue q;
2524
};
2625

2726
struct napi_threadsafe_function__ {
@@ -30,7 +29,7 @@ struct napi_threadsafe_function__ {
3029
pthread_mutex_t mutex;
3130
pthread_cond_t* cond;
3231
size_t queue_size;
33-
void* queue[2];
32+
struct uv__queue queue;
3433
uv_async_t async;
3534
size_t thread_count;
3635
bool is_closing;
@@ -92,7 +91,7 @@ _emnapi_tsfn_create(napi_env env,
9291
pthread_mutex_init(&ts_fn->mutex, NULL);
9392
ts_fn->cond = NULL;
9493
ts_fn->queue_size = 0;
95-
QUEUE_INIT(&ts_fn->queue);
94+
uv__queue_init(&ts_fn->queue);
9695
ts_fn->thread_count = initial_thread_count;
9796
ts_fn->is_closing = false;
9897
ts_fn->dispatch_state = kDispatchIdle;
@@ -125,13 +124,13 @@ static void _emnapi_tsfn_destroy(napi_threadsafe_function func) {
125124
func->cond = NULL;
126125
}
127126

128-
QUEUE* tmp;
127+
struct uv__queue* tmp;
129128
struct data_queue_node* node;
130-
QUEUE_FOREACH(tmp, &func->queue) {
131-
node = QUEUE_DATA(tmp, struct data_queue_node, q);
129+
uv__queue_foreach(tmp, &func->queue) {
130+
node = uv__queue_data(tmp, struct data_queue_node, q);
132131
free(node);
133132
}
134-
QUEUE_INIT(&func->queue);
133+
uv__queue_init(&func->queue);
135134

136135
if (func->ref != NULL) {
137136
EMNAPI_ASSERT_CALL(napi_delete_reference(func->env, func->ref));
@@ -187,14 +186,14 @@ static napi_status _emnapi_tsfn_init(napi_threadsafe_function func) {
187186
}
188187

189188
static void _emnapi_tsfn_empty_queue_and_delete(napi_threadsafe_function func) {
190-
while (!QUEUE_EMPTY(&func->queue)) {
191-
QUEUE* q = QUEUE_HEAD(&func->queue);
192-
struct data_queue_node* node = QUEUE_DATA(q, struct data_queue_node, q);
189+
while (!uv__queue_empty(&func->queue)) {
190+
struct uv__queue* q = uv__queue_head(&func->queue);
191+
struct data_queue_node* node = uv__queue_data(q, struct data_queue_node, q);
193192

194193
func->call_js_cb(NULL, NULL, func->context, node->data);
195194

196-
QUEUE_REMOVE(q);
197-
QUEUE_INIT(q);
195+
uv__queue_remove(q);
196+
uv__queue_init(q);
198197
func->queue_size--;
199198
free(node);
200199
}
@@ -295,10 +294,10 @@ static bool _emnapi_tsfn_dispatch_one(napi_threadsafe_function func) {
295294
} else {
296295
size_t size = func->queue_size;
297296
if (size > 0) {
298-
QUEUE* q = QUEUE_HEAD(&func->queue);
299-
struct data_queue_node* node = QUEUE_DATA(q, struct data_queue_node, q);
300-
QUEUE_REMOVE(q);
301-
QUEUE_INIT(q);
297+
struct uv__queue* q = uv__queue_head(&func->queue);
298+
struct data_queue_node* node = uv__queue_data(q, struct data_queue_node, q);
299+
uv__queue_remove(q);
300+
uv__queue_init(q);
302301
func->queue_size--;
303302
data = node->data;
304303
free(node);
@@ -519,7 +518,7 @@ napi_call_threadsafe_function(napi_threadsafe_function func,
519518
return napi_generic_failure;
520519
}
521520
queue_node->data = data;
522-
QUEUE_INSERT_TAIL(&func->queue, &queue_node->q);
521+
uv__queue_insert_tail(&func->queue, &queue_node->q);
523522
func->queue_size++;
524523
_emnapi_tsfn_send(func);
525524
pthread_mutex_unlock(&func->mutex);

0 commit comments

Comments
 (0)