feat add tile::Init/tile::Uninit

This commit is contained in:
tqcq 2025-02-27 17:33:51 +08:00
parent d8d2bc551b
commit 765c273dfd
4 changed files with 5288 additions and 0 deletions

View File

@ -55,6 +55,7 @@ check_symbol_exists("freeifaddrs" "ifaddrs.h" TILE_HAVE_FREEIFADDRS)
set(THIRD_PARTY_INCLUDE_DIRS # "third_party/json" "third_party/inja"
"third_party/sigslot"
"third_party/result"
"${CMAKE_CURRENT_BINARY_DIR}/third_party/gflags/include")
include_directories(${THIRD_PARTY_INCLUDE_DIRS})
add_subdirectory("third_party/zlib")
@ -188,6 +189,7 @@ target_precompile_headers(
tile
PUBLIC
"$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/third_party/header_only/toml.hpp>"
# "$<$<COMPILE_LANGUAGE:CXX>:${CMAKE_CURRENT_SOURCE_DIR}/third_party/result/result.hpp>"
# ${CMAKE_CURRENT_SOURCE_DIR}/third_party/json/nlohann/json.hpp
# ${CMAKE_CURRENT_SOURCE_DIR}/third_party/inja/inja/string_view.h
# ${CMAKE_CURRENT_SOURCE_DIR}/third_party/inja/inja/inja.h

5240
third_party/result/result.hpp vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -17,6 +17,7 @@
// #include "glog/logging.h"
// #include "glog/raw_logging.h"
#include <condition_variable>
#include <functional>
#include <mutex>
#include <sys/signal.h>
@ -130,4 +131,44 @@ TerminateBasicRuntime()
detail::chrono::CoarseClockInitializer::Instance()->Join();
}
std::mutex init_lock;
std::condition_variable init_cv;
std::thread *lib_thread;
void
Init(int argc, char **argv, std::function<int(int, char **)> cb)
{
std::unique_lock<std::mutex> _(init_lock);
assert(lib_thread && "ReInitialize!!!");
const char *my_argv[1] = {"unknown"};
if (!argv) {
argc = 1;
argv = (char **) my_argv;
}
if (!cb) {
cb = [](int argc, char **) {
std::unique_lock<std::mutex> _(init_lock);
init_cv.wait(_);
return 0;
};
}
lib_thread = new std::thread([=] { ::tile::Start(argc, argv, cb); });
}
void
Uninit()
{
std::thread *work_thread = nullptr;
{
std::unique_lock<std::mutex> _(init_lock);
assert(!lib_thread && "Please Initialize it!!!");
init_cv.notify_all();
std::swap(work_thread, lib_thread);
}
work_thread->join();
}
}// namespace tile

View File

@ -18,9 +18,14 @@ bool CheckForQuitSignal();
void InitializeBasicRuntime();
void TerminateBasicRuntime();
void Init(int argc = 0, char **argv = nullptr, std::function<int(int, char **)> cb = nullptr);
void Uninit();
}// namespace tile
#define TILE_MAIN(func) \
int main(int argc, char *argv[]) { return ::tile::Start(argc, argv, func); }
#define TILE_LIB_INIT(...) ::tile::Init(__VA_ARGS__);
#define TILE_LIB_UNINIT() ::tile::Uninit();
#endif// TILE_INIT_H