tcp/udp 추가, todo:iocp openssl 지원하도록 업데이트하기
This commit is contained in:
32
impl/utils/generate_id.cpp
Normal file
32
impl/utils/generate_id.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
#include "utils/snowflake.h"
|
||||
|
||||
#include <thread>
|
||||
|
||||
namespace utils {
|
||||
|
||||
static struct EpochInitializer {
|
||||
EpochInitializer() { EPOCH = std::chrono::system_clock::now(); }
|
||||
std::chrono::system_clock::time_point EPOCH;
|
||||
} epochInitializer;
|
||||
|
||||
Snowflake GenerateID() {
|
||||
static std::mutex snowflakeGenerateMutex_;
|
||||
|
||||
std::lock_guard<std::mutex> lock(snowflakeGenerateMutex_);
|
||||
|
||||
std::size_t tid =
|
||||
std::hash<std::thread::id>{}(std::this_thread::get_id());
|
||||
|
||||
thread_local static int sequence = 0;
|
||||
Snowflake id = {};
|
||||
|
||||
auto timestamp = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::system_clock::now() - epochInitializer.EPOCH);
|
||||
id.timestamp = timestamp.count();
|
||||
id.instance = tid;
|
||||
id.sequence = sequence++;
|
||||
|
||||
return id;
|
||||
}
|
||||
|
||||
}; // namespace Chattr
|
||||
27
impl/utils/log.cpp
Normal file
27
impl/utils/log.cpp
Normal file
@@ -0,0 +1,27 @@
|
||||
#include "utils/log.h"
|
||||
|
||||
#include "spdlog/sinks/rotating_file_sink.h"
|
||||
#include "spdlog/sinks/stdout_color_sinks.h"
|
||||
#ifdef _WIN32
|
||||
#include "spdlog/sinks/msvc_sink.h"
|
||||
#endif
|
||||
|
||||
namespace utils {
|
||||
|
||||
void setDefaultLogger(spdlog::level::level_enum logLevel,
|
||||
gsl::czstring logFileName, std::uint32_t logFileSize,
|
||||
std::uint32_t logFileCount) {
|
||||
std::vector<spdlog::sink_ptr> sinks;
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::stdout_color_sink_mt>());
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
|
||||
logFileName, logFileSize, logFileCount, false));
|
||||
#ifdef _WIN32
|
||||
sinks.push_back(std::make_shared<spdlog::sinks::msvc_sink_mt>());
|
||||
#endif
|
||||
auto chatteringLogger = std::make_shared<spdlog::logger>(
|
||||
"Chattering Logger", begin(sinks), end(sinks));
|
||||
chatteringLogger->set_level(logLevel);
|
||||
spdlog::set_default_logger(chatteringLogger);
|
||||
}
|
||||
|
||||
} // namespace Chattr::log
|
||||
82
impl/utils/thread_pool.cpp
Normal file
82
impl/utils/thread_pool.cpp
Normal file
@@ -0,0 +1,82 @@
|
||||
#include "utils/thread_pool.h"
|
||||
#include "precomp.h"
|
||||
|
||||
namespace utils {
|
||||
|
||||
ThreadPool::ThreadPool() : ThreadPool(0) {}
|
||||
|
||||
ThreadPool::ThreadPool(std::uint32_t numThreads) { init(numThreads); }
|
||||
|
||||
ThreadPool::~ThreadPool() { terminate(); }
|
||||
|
||||
void ThreadPool::init(std::uint32_t numThreads) {
|
||||
int numCPU = numThreads;
|
||||
if (numThreads == 0) {
|
||||
#ifdef _WIN32
|
||||
SYSTEM_INFO sysinfo;
|
||||
GetSystemInfo(&sysinfo);
|
||||
numCPU = sysinfo.dwNumberOfProcessors;
|
||||
#elif __linux__
|
||||
numCPU = sysconf(_SC_NPROCESSORS_ONLN);
|
||||
#endif
|
||||
spdlog::info("Auto-detected cpu count: {}", numCPU);
|
||||
if (numCPU == 1 || numCPU == 2) {
|
||||
numCPU = 4;
|
||||
spdlog::info(
|
||||
"Set ThreadPool Worker count to: {} due to program to oprate "
|
||||
"concurrently",
|
||||
numCPU);
|
||||
} else {
|
||||
spdlog::info("Set ThreadPool Worker count to: {}", numCPU);
|
||||
}
|
||||
}
|
||||
threadCount = numCPU;
|
||||
workers_.reserve(numCPU);
|
||||
|
||||
while (numCPU--) workers_.emplace_back([this]() { this->Worker(); });
|
||||
}
|
||||
|
||||
void ThreadPool::terminate() {
|
||||
terminate_ = true;
|
||||
jobQueueCV_.notify_all();
|
||||
|
||||
spdlog::debug("waiting for threads to end their jobs...");
|
||||
for (auto& t : workers_) t.join();
|
||||
}
|
||||
|
||||
void ThreadPool::respawnWorker(std::uint32_t numThreads) {
|
||||
terminate();
|
||||
terminate_ = false;
|
||||
init(numThreads);
|
||||
}
|
||||
|
||||
void* ThreadPool::Worker() {
|
||||
#ifdef _WIN32
|
||||
DWORD pid = GetCurrentThreadId();
|
||||
#elif __linux__
|
||||
pthread_t pid = pthread_self();
|
||||
#endif
|
||||
spdlog::trace("ThreadPool Worker : {} up", pid);
|
||||
while (!terminate_) {
|
||||
std::unique_lock<std::mutex> lock(jobQueueMutex);
|
||||
jobQueueCV_.wait(lock,
|
||||
[this]() { return !this->jobs_.empty() || terminate_; });
|
||||
if (this->jobs_.empty() || terminate_) {
|
||||
jobs_ = std::queue<std::packaged_task<void()>>();
|
||||
break;
|
||||
}
|
||||
if (this->jobs_.empty()) continue;
|
||||
|
||||
auto job = std::move(jobs_.front());
|
||||
jobs_.pop();
|
||||
lock.unlock();
|
||||
|
||||
spdlog::trace("ThreadPool Worker : {} Executing a job", pid);
|
||||
job();
|
||||
}
|
||||
spdlog::trace("ThreadPool Worker : {} down", pid);
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
} // namespace Chattr
|
||||
Reference in New Issue
Block a user