Skip to content

Commit b40903b

Browse files
committed
sync upstream uv source code
1 parent cba668c commit b40903b

3 files changed

Lines changed: 88 additions & 5 deletions

File tree

packages/emnapi/include/uv.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,23 @@ typedef void (*uv_thread_cb)(void* arg);
8484
UV_EXTERN int uv_thread_create(uv_thread_t* tid, uv_thread_cb entry, void* arg);
8585
UV_EXTERN int uv_thread_join(uv_thread_t *tid);
8686

87+
typedef enum {
88+
UV_THREAD_NO_FLAGS = 0x00,
89+
UV_THREAD_HAS_STACK_SIZE = 0x01
90+
} uv_thread_create_flags;
91+
92+
struct uv_thread_options_s {
93+
unsigned int flags;
94+
size_t stack_size;
95+
};
96+
97+
typedef struct uv_thread_options_s uv_thread_options_t;
98+
99+
UV_EXTERN int uv_thread_create_ex(uv_thread_t* tid,
100+
const uv_thread_options_t* params,
101+
uv_thread_cb entry,
102+
void* arg);
103+
87104
typedef void (*uv_close_cb)(uv_handle_t* handle);
88105
typedef void (*uv_async_cb)(uv_async_t* handle);
89106

packages/emnapi/src/uv/threadpool.c

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ void uv__threadpool_cleanup(void) {
212212
EMNAPI_INTERNAL_EXTERN int _emnapi_async_work_pool_size();
213213

214214
static void init_threads(void) {
215+
uv_thread_options_t config;
215216
unsigned int i;
216217
#if !defined(EMNAPI_WORKER_POOL_SIZE) || !(EMNAPI_WORKER_POOL_SIZE > 0)
217218
const char* val;
@@ -262,11 +263,17 @@ static void init_threads(void) {
262263
abort();
263264
#endif
264265

266+
// config.flags = UV_THREAD_HAS_STACK_SIZE;
267+
// config.stack_size = 8u << 20; /* 8 MB */
268+
269+
// use DEFAULT_PTHREAD_STACK_SIZE
270+
config.flags = UV_THREAD_NO_FLAGS;
271+
265272
for (i = 0; i < nthreads; i++)
266273
#ifndef __EMNAPI_WASI_THREADS__
267-
if (uv_thread_create(threads + i, (uv_thread_cb) worker, &sem))
274+
if (uv_thread_create_ex(threads + i, &config, (uv_thread_cb) worker, &sem))
268275
#else
269-
if (uv_thread_create(threads + i, (uv_thread_cb) worker, NULL))
276+
if (uv_thread_create_ex(threads + i, &config, (uv_thread_cb) worker, NULL))
270277
#endif
271278
abort();
272279

packages/emnapi/src/uv/unix/thread.c

Lines changed: 62 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@
1515
// } \
1616
// } while (0)
1717

18+
static size_t uv__min_stack_size(void) {
19+
static const size_t min = 8192;
20+
21+
#ifdef PTHREAD_STACK_MIN /* Not defined on NetBSD. */
22+
if (min < (size_t) PTHREAD_STACK_MIN)
23+
return PTHREAD_STACK_MIN;
24+
#endif /* PTHREAD_STACK_MIN */
25+
26+
return min;
27+
}
28+
29+
static size_t uv__default_stack_size(void) {
30+
#if !defined(__linux__)
31+
return 0;
32+
#elif defined(__PPC__) || defined(__ppc__) || defined(__powerpc__)
33+
return 4 << 20; /* glibc default. */
34+
#else
35+
return 2 << 20; /* glibc default. */
36+
#endif
37+
}
38+
39+
size_t uv__thread_stack_size(void) {
40+
return uv__default_stack_size();
41+
}
42+
1843
void uv_sem_post(sem_t* sem) {
1944
if (sem_post(sem))
2045
abort();
@@ -104,25 +129,59 @@ void uv_cond_destroy(uv_cond_t* cond) {
104129
}
105130

106131
int uv_thread_create_ex(uv_thread_t* tid,
107-
void* params,
132+
const uv_thread_options_t* params,
108133
void (*entry)(void *arg),
109134
void *arg) {
110135
int err;
136+
pthread_attr_t* attr;
137+
pthread_attr_t attr_storage;
138+
size_t pagesize;
139+
size_t stack_size;
140+
size_t min_stack_size;
111141

112142
/* Used to squelch a -Wcast-function-type warning. */
113143
union {
114144
void (*in)(void*);
115145
void* (*out)(void*);
116146
} f;
117147

148+
stack_size =
149+
params->flags & UV_THREAD_HAS_STACK_SIZE ? params->stack_size : 0;
150+
151+
attr = NULL;
152+
if (stack_size == 0) {
153+
stack_size = uv__thread_stack_size();
154+
} else {
155+
pagesize = 65536;
156+
stack_size = (stack_size + pagesize - 1) &~ (pagesize - 1);
157+
min_stack_size = uv__min_stack_size();
158+
if (stack_size < min_stack_size)
159+
stack_size = min_stack_size;
160+
}
161+
162+
if (stack_size > 0) {
163+
attr = &attr_storage;
164+
165+
if (pthread_attr_init(attr))
166+
abort();
167+
168+
if (pthread_attr_setstacksize(attr, stack_size))
169+
abort();
170+
}
171+
118172
f.in = entry;
119-
err = pthread_create(tid, NULL, f.out, arg);
173+
err = pthread_create(tid, attr, f.out, arg);
174+
175+
if (attr != NULL)
176+
pthread_attr_destroy(attr);
120177

121178
return err;
122179
}
123180

124181
int uv_thread_create(uv_thread_t *tid, void (*entry)(void *arg), void *arg) {
125-
return uv_thread_create_ex(tid, NULL, entry, arg);
182+
uv_thread_options_t params;
183+
params.flags = UV_THREAD_NO_FLAGS;
184+
return uv_thread_create_ex(tid, &params, entry, arg);
126185
}
127186

128187
int uv_thread_join(uv_thread_t *tid) {

0 commit comments

Comments
 (0)