// SPDX-FileCopyrightText: Matteo Settenvini // SPDX-License-Identifier: GPL-3.0-or-later #include "cares_init.hh" #include #include #include using namespace std::literals; auto malcontent::CAresLibrary::instance() -> std::shared_ptr { if (auto ret = _instance.lock()) { return ret; } auto ret = std::make_shared(Private{}); _instance = ret; return ret; } malcontent::CAresLibrary::CAresLibrary(Private) { std::lock_guard guard { _init_mutex }; int ret = ares_library_init(ARES_LIB_INIT_ALL); if (ret != ARES_SUCCESS) { const auto err = "ares_library_init: "s + ares_strerror(ret); throw std::system_error(ret, std::generic_category(), err.c_str()); } } malcontent::CAresLibrary::~CAresLibrary() noexcept { std::lock_guard guard { _init_mutex }; ares_library_cleanup(); } std::mutex malcontent::CAresLibrary::_init_mutex{}; std::weak_ptr malcontent::CAresLibrary::_instance{};