#define UV_ONCE_INIT PTHREAD_ONCE_INIT staticuv_once_t once = UV_ONCE_INIT;
staticvoidinit_once(void){ #ifndef _WIN32 /* Re-initialize the threadpool after fork. * Note that this discards the global mutex and condition as well * as the work queue. */ if (pthread_atfork(NULL, NULL, &reset_once)) abort(); #endif init_threads(); }
PTHREAD_MUTEX_ERRORCHECK This type of mutex provides error checking. A thread attempting to relock this mutex without first unlocking it shall return with an error. A thread attempting to unlock a mutex which another thread has locked shall return with an error. A thread attempting to unlock an unlocked mutex shall return with an error.
🥡信号量
初始化每个线程时,libuv用信号量来保证init_threads函数在初始化完所有线程后退出。
1 2 3 4 5 6 7 8 9 10 11
if (uv_sem_init(&sem, 0)) abort();
for (i = 0; i < nthreads; i++) if (uv_thread_create(threads + i, worker, &sem)) abort();
ERRORS pthread_attr_setstacksize() can fail with the following error: EINVAL The stack size is less than PTHREAD_STACK_MIN (16384) bytes. On some systems, pthread_attr_setstacksize() can fail with the error EINVAL if stacksize is not a multiple of the system page size.
/* Musl's PTHREAD_STACK_MIN is 2 KB on all architectures, which is * too small to safely receive signals on. * * Musl's PTHREAD_STACK_MIN + MINSIGSTKSZ == 8192 on arm64 (which has * the largest MINSIGSTKSZ of the architectures that musl supports) so * let's use that as a lower bound. * * We use a hardcoded value because PTHREAD_STACK_MIN + MINSIGSTKSZ * is between 28 and 133 KB when compiling against glibc, depending * on the architecture. */ if (lim.rlim_cur >= 8192) if (lim.rlim_cur >= PTHREAD_STACK_MIN) return lim.rlim_cur; } ... return2 << 20; /* glibc default. */ #endif