Fixed libuv init/shutdown sequence

This commit is contained in:
SChernykh 2022-11-14 20:59:11 +01:00
parent 53ba29b288
commit d0eea1bf91
4 changed files with 68 additions and 35 deletions

View file

@ -93,12 +93,20 @@ else()
include_directories(external/src/curl/include)
endif()
if ((CMAKE_CXX_COMPILER_ID MATCHES MSVC) OR STATIC_BINARY OR STATIC_LIBS)
set(UV_INCLUDE_DIR external/src/libuv/include)
set(ZMQ_INCLUDE_DIR external/src/libzmq/include)
else()
find_path(UV_INCLUDE_DIR NAMES uv.h PATH_SUFFIXES "include")
find_path(ZMQ_INCLUDE_DIR NAMES zmq.h PATH_SUFFIXES "include")
endif()
include_directories(src)
include_directories(external/src)
include_directories(external/src/cryptonote)
include_directories(external/src/libuv/include)
include_directories(${UV_INCLUDE_DIR})
include_directories(external/src/cppzmq)
include_directories(external/src/libzmq/include)
include_directories(${ZMQ_INCLUDE_DIR})
if (WITH_RANDOMX)
include_directories(external/src/RandomX/src)
endif()

View file

@ -64,6 +64,7 @@ public:
// Create default loop here
uv_default_loop();
init_uv_threadpool();
uv_cond_init(&m_cond);
uv_mutex_init(&m_mutex);
@ -115,6 +116,10 @@ public:
uv_mutex_destroy(&m_mutex);
uv_loop_close(uv_default_loop());
#if ((UV_VERSION_MAJOR > 1) || ((UV_VERSION_MAJOR == 1) && (UV_VERSION_MINOR >= 38)))
uv_library_shutdown();
#endif
m_logFile.close();
}
@ -143,6 +148,33 @@ public:
uv_cond_signal(&m_cond);
}
private:
static void init_uv_threadpool()
{
#ifdef _MSC_VER
#define putenv _putenv
#endif
const uint32_t N = std::max(std::min(std::thread::hardware_concurrency(), 4U), 8U);
LOGINFO(4, "running " << N << " threads in the UV thread pool");
char buf[40] = {};
log::Stream s(buf);
s << "UV_THREADPOOL_SIZE=" << N << '\0';
int err = putenv(buf);
if (err != 0) {
err = errno;
LOGWARN(1, "Couldn't set UV thread pool size to " << N << " threads, putenv returned error " << err);
}
static uv_work_t dummy;
err = uv_queue_work(uv_default_loop_checked(), &dummy, [](uv_work_t*) {}, nullptr);
if (err) {
LOGERR(1, "init_uv_threadpool: uv_queue_work failed, error " << uv_err_name(err));
}
}
private:
static void run_wrapper(void* arg) { reinterpret_cast<Worker*>(arg)->run(); }
@ -153,7 +185,11 @@ private:
do {
uv_mutex_lock(&m_mutex);
if (m_readPos == m_writePos.load()) {
// Nothing to do, wait for the signal
// Nothing to do, wait for the signal or exit if stopped
if (stopped) {
uv_mutex_unlock(&m_mutex);
return;
}
uv_cond_wait(&m_cond, &m_mutex);
}
uv_mutex_unlock(&m_mutex);
@ -231,7 +267,7 @@ private:
m_logFile.open(log_file_name, std::ios::app | std::ios::binary);
}
}
} while (!stopped);
} while (1);
}
static FORCEINLINE void strip_colors(char* buf, uint32_t& size)

View file

@ -1442,29 +1442,6 @@ static void on_signal(uv_signal_t* handle, int signum)
pool->stop();
}
static bool init_uv_threadpool()
{
#ifdef _MSC_VER
#define putenv _putenv
#endif
const uint32_t N = std::max(std::min(std::thread::hardware_concurrency(), 4U), 8U);
LOGINFO(4, "running " << N << " threads in the UV thread pool");
char buf[40] = {};
log::Stream s(buf);
s << "UV_THREADPOOL_SIZE=" << N << '\0';
int err = putenv(buf);
if (err != 0) {
err = errno;
LOGWARN(1, "Couldn't set UV thread pool size to " << N << " threads, putenv returned error " << err);
}
static uv_work_t dummy;
return (uv_queue_work(uv_default_loop_checked(), &dummy, [](uv_work_t*) {}, nullptr) == 0);
}
bool init_signals(p2pool* pool, bool init)
{
#ifdef SIGPIPE
@ -1549,11 +1526,6 @@ int p2pool::run()
return 1;
}
if (!init_uv_threadpool()) {
LOGERR(1, "failed to start UV thread pool");
return 1;
}
if (!init_signals(this, true)) {
LOGERR(1, "failed to initialize signal handlers");
return 1;

View file

@ -55,13 +55,30 @@ set(SOURCES
../src/zmq_reader.cpp
)
if (NOT STATIC_BINARY AND NOT STATIC_LIBS)
include(FindCURL)
endif()
if (CURL_INCLUDE_DIRS)
include_directories(${CURL_INCLUDE_DIRS})
else()
include_directories(../external/src/curl/include)
endif()
if ((CMAKE_CXX_COMPILER_ID MATCHES MSVC) OR STATIC_BINARY OR STATIC_LIBS)
set(UV_INCLUDE_DIR ../external/src/libuv/include)
set(ZMQ_INCLUDE_DIR ../external/src/libzmq/include)
else()
find_path(UV_INCLUDE_DIR NAMES uv.h PATH_SUFFIXES "include")
find_path(ZMQ_INCLUDE_DIR NAMES zmq.h PATH_SUFFIXES "include")
endif()
include_directories(../src)
include_directories(../external/src)
include_directories(../external/src/cryptonote)
include_directories(../external/src/curl/include)
include_directories(../external/src/libuv/include)
include_directories(${UV_INCLUDE_DIR})
include_directories(../external/src/cppzmq)
include_directories(../external/src/libzmq/include)
include_directories(${ZMQ_INCLUDE_DIR})
include_directories(../external/src/RandomX/src)
include_directories(../external/src/rapidjson/include)
include_directories(../external/src/robin-hood-hashing/src/include)