Skip to content

Commit

Permalink
update libuv source (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
toyobayashi authored Aug 8, 2024
1 parent 2df2818 commit 6ac98c3
Show file tree
Hide file tree
Showing 16 changed files with 720 additions and 233 deletions.
16 changes: 13 additions & 3 deletions packages/emnapi/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ set(UV_SRC
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/uv-common.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/threadpool.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/unix/loop.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/unix/posix-hrtime.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/unix/thread.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/unix/async.c"
"${CMAKE_CURRENT_SOURCE_DIR}/src/uv/unix/core.c"
Expand Down Expand Up @@ -139,7 +140,10 @@ endif()

add_library(${EMNAPI_BASIC_TARGET_NAME} STATIC ${ENAPI_BASIC_SRC})
target_include_directories(${EMNAPI_BASIC_TARGET_NAME} PUBLIC ${EMNAPI_INCLUDE})
target_compile_definitions(${EMNAPI_BASIC_TARGET_NAME} PUBLIC ${EMNAPI_DEFINES})
target_compile_definitions(${EMNAPI_BASIC_TARGET_NAME}
PUBLIC ${EMNAPI_DEFINES}
PRIVATE "EMNAPI_DISABLE_UV"
)
if(IS_EMSCRIPTEN)
set_target_properties(${EMNAPI_BASIC_TARGET_NAME} PROPERTIES INTERFACE_LINK_DEPENDS ${EMNAPI_JS_LIB})
target_link_options(${EMNAPI_BASIC_TARGET_NAME} INTERFACE "--js-library=${EMNAPI_JS_LIB}")
Expand All @@ -159,7 +163,10 @@ if(EMNAPI_BUILD_BASIC_MT)
)
target_compile_options(${EMNAPI_BASIC_MT_TARGET_NAME} PUBLIC "-matomics" "-mbulk-memory")
target_include_directories(${EMNAPI_BASIC_MT_TARGET_NAME} PUBLIC ${EMNAPI_INCLUDE})
target_compile_definitions(${EMNAPI_BASIC_MT_TARGET_NAME} PUBLIC ${EMNAPI_DEFINES})
target_compile_definitions(${EMNAPI_BASIC_MT_TARGET_NAME}
PUBLIC ${EMNAPI_DEFINES}
PRIVATE "EMNAPI_DISABLE_UV"
)

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

if(EMNAPI_BUILD_MT)
add_library(${EMNAPI_MT_TARGET_NAME} STATIC ${EMNAPI_SRC} ${UV_SRC})
target_compile_options(${EMNAPI_MT_TARGET_NAME} PRIVATE ${EMNAPI_MT_CFLAGS})
target_compile_options(${EMNAPI_MT_TARGET_NAME}
PUBLIC "-matomics" "-mbulk-memory"
PRIVATE ${EMNAPI_MT_CFLAGS}
)
target_include_directories(${EMNAPI_MT_TARGET_NAME} PUBLIC ${EMNAPI_INCLUDE})
target_compile_definitions(${EMNAPI_MT_TARGET_NAME} PUBLIC ${EMNAPI_DEFINES})
if(IS_EMSCRIPTEN)
Expand Down
3 changes: 3 additions & 0 deletions packages/emnapi/emnapi.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@
{
'target_name': 'emnapi_basic',
'type': 'static_library',
'defines': [
'EMNAPI_DISABLE_UV'
],
'sources': [
'src/js_native_api.c',
'src/node_api.c',
Expand Down
55 changes: 49 additions & 6 deletions packages/emnapi/include/node/uv.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,21 @@

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

#include <stddef.h>
#include "uv/unix.h"

#ifdef __cplusplus
extern "C" {
#endif

#include <stddef.h>
#include <stdint.h>

/* Internal type, do not use. */
struct uv__queue {
struct uv__queue* next;
struct uv__queue* prev;
};

#include "uv/unix.h"

#define UV_EXTERN /* nothing */

typedef enum {
Expand All @@ -29,6 +37,8 @@ typedef struct uv_work_s uv_work_t;
typedef struct uv_handle_s uv_handle_t;
typedef struct uv_async_s uv_async_t;

typedef struct uv_metrics_s uv_metrics_t;

typedef void (*uv_work_cb)(uv_work_t* req);
typedef void (*uv_after_work_cb)(uv_work_t* req, int status);

Expand All @@ -40,10 +50,22 @@ struct uv_req_s {
UV_REQ_FIELDS
};

typedef void* (*uv_malloc_func)(size_t size);
typedef void* (*uv_realloc_func)(void* ptr, size_t size);
typedef void* (*uv_calloc_func)(size_t count, size_t size);
typedef void (*uv_free_func)(void* ptr);

UV_EXTERN void uv_library_shutdown(void);

UV_EXTERN int uv_replace_allocator(uv_malloc_func malloc_func,
uv_realloc_func realloc_func,
uv_calloc_func calloc_func,
uv_free_func free_func);

UV_EXTERN uv_loop_t* uv_default_loop(void);
UV_EXTERN int uv_loop_init(uv_loop_t* loop);
UV_EXTERN int uv_loop_close(uv_loop_t* loop);
UV_EXTERN uint64_t uv_hrtime(void);
UV_EXTERN void uv_sleep(unsigned int msec);

UV_EXTERN int uv_sem_init(uv_sem_t* sem, unsigned int value);
Expand Down Expand Up @@ -111,6 +133,12 @@ typedef void (*uv_async_cb)(uv_async_t* handle);
uv_loop_t* loop; \
uv_handle_type type; \
uv_close_cb close_cb; \
struct uv__queue handle_queue; \
union { \
int fd; \
void* reserved[4]; \
} u; \
UV_HANDLE_PRIVATE_FIELDS \

struct uv_handle_s {
UV_HANDLE_FIELDS
Expand All @@ -119,7 +147,7 @@ struct uv_handle_s {
struct uv_async_s {
UV_HANDLE_FIELDS
uv_async_cb async_cb;
void* queue[2];
struct uv__queue queue;
int pending;
};

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

UV_EXTERN void uv_close(uv_handle_t* handle, uv_close_cb close_cb);
UV_EXTERN int uv_is_closing(const uv_handle_t* handle);

struct uv_metrics_s {
uint64_t loop_count;
uint64_t events;
uint64_t events_waiting;
/* private */
uint64_t* reserved[13];
};

UV_EXTERN int uv_metrics_info(uv_loop_t* loop, uv_metrics_t* metrics);
UV_EXTERN uint64_t uv_metrics_idle_time(uv_loop_t* loop);

struct uv_loop_s {
void* data;
unsigned int active_handles;
struct uv__queue handle_queue;
union {
void* unused;
unsigned int count;
} active_reqs;
void* wq[2];
void* internal_fields;

struct uv__queue wq;
uv_mutex_t wq_mutex;
uv_async_t wq_async;
void* async_handles[2];
struct uv__queue async_handles;
void* em_queue;
};

Expand Down
2 changes: 1 addition & 1 deletion packages/emnapi/include/node/uv/threadpool.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ struct uv__work {
void (*work)(struct uv__work *w);
void (*done)(struct uv__work *w, int status);
struct uv_loop_s* loop;
void* wq[2];
struct uv__queue wq;
};

#endif
Expand Down
4 changes: 4 additions & 0 deletions packages/emnapi/include/node/uv/unix.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@ typedef pthread_cond_t uv_cond_t;

#endif

#define UV_HANDLE_PRIVATE_FIELDS \
uv_handle_t* next_closing; \
unsigned int flags; \

#endif /* UV_UNIX_H */
4 changes: 2 additions & 2 deletions packages/emnapi/src/node_api.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "node_api.h"
#include "emnapi_internal.h"

#if EMNAPI_HAVE_THREADS
#if EMNAPI_HAVE_THREADS && !defined(EMNAPI_DISABLE_UV)
#include "uv.h"
#endif

Expand Down Expand Up @@ -31,7 +31,7 @@ napi_get_node_version(node_api_basic_env env,

napi_status napi_get_uv_event_loop(node_api_basic_env env,
struct uv_loop_s** loop) {
#if EMNAPI_HAVE_THREADS
#if EMNAPI_HAVE_THREADS && !defined(EMNAPI_DISABLE_UV)
CHECK_ENV(env);
CHECK_ARG(env, loop);
// Though this is fake libuv loop
Expand Down
37 changes: 18 additions & 19 deletions packages/emnapi/src/threadsafe_function.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
#include <stdatomic.h>
#include <pthread.h>
#include <errno.h>
#include "uv/queue.h"

#include "uv.h"
#include "uv/queue.h"

EXTERN_C_START

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

struct data_queue_node {
void* data;
void* q[2];
struct uv__queue q;
};

struct napi_threadsafe_function__ {
Expand All @@ -30,7 +29,7 @@ struct napi_threadsafe_function__ {
pthread_mutex_t mutex;
pthread_cond_t* cond;
size_t queue_size;
void* queue[2];
struct uv__queue queue;
uv_async_t async;
size_t thread_count;
bool is_closing;
Expand Down Expand Up @@ -92,7 +91,7 @@ _emnapi_tsfn_create(napi_env env,
pthread_mutex_init(&ts_fn->mutex, NULL);
ts_fn->cond = NULL;
ts_fn->queue_size = 0;
QUEUE_INIT(&ts_fn->queue);
uv__queue_init(&ts_fn->queue);
ts_fn->thread_count = initial_thread_count;
ts_fn->is_closing = false;
ts_fn->dispatch_state = kDispatchIdle;
Expand Down Expand Up @@ -125,13 +124,13 @@ static void _emnapi_tsfn_destroy(napi_threadsafe_function func) {
func->cond = NULL;
}

QUEUE* tmp;
struct uv__queue* tmp;
struct data_queue_node* node;
QUEUE_FOREACH(tmp, &func->queue) {
node = QUEUE_DATA(tmp, struct data_queue_node, q);
uv__queue_foreach(tmp, &func->queue) {
node = uv__queue_data(tmp, struct data_queue_node, q);
free(node);
}
QUEUE_INIT(&func->queue);
uv__queue_init(&func->queue);

if (func->ref != NULL) {
EMNAPI_ASSERT_CALL(napi_delete_reference(func->env, func->ref));
Expand Down Expand Up @@ -187,14 +186,14 @@ static napi_status _emnapi_tsfn_init(napi_threadsafe_function func) {
}

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

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

QUEUE_REMOVE(q);
QUEUE_INIT(q);
uv__queue_remove(q);
uv__queue_init(q);
func->queue_size--;
free(node);
}
Expand Down Expand Up @@ -295,10 +294,10 @@ static bool _emnapi_tsfn_dispatch_one(napi_threadsafe_function func) {
} else {
size_t size = func->queue_size;
if (size > 0) {
QUEUE* q = QUEUE_HEAD(&func->queue);
struct data_queue_node* node = QUEUE_DATA(q, struct data_queue_node, q);
QUEUE_REMOVE(q);
QUEUE_INIT(q);
struct uv__queue* q = uv__queue_head(&func->queue);
struct data_queue_node* node = uv__queue_data(q, struct data_queue_node, q);
uv__queue_remove(q);
uv__queue_init(q);
func->queue_size--;
data = node->data;
free(node);
Expand Down Expand Up @@ -519,7 +518,7 @@ napi_call_threadsafe_function(napi_threadsafe_function func,
return napi_generic_failure;
}
queue_node->data = data;
QUEUE_INSERT_TAIL(&func->queue, &queue_node->q);
uv__queue_insert_tail(&func->queue, &queue_node->q);
func->queue_size++;
_emnapi_tsfn_send(func);
pthread_mutex_unlock(&func->mutex);
Expand Down
Loading

0 comments on commit 6ac98c3

Please sign in to comment.