40 lines
1.1 KiB
C++
40 lines
1.1 KiB
C++
// SPDX-FileCopyrightText: Matteo Settenvini <matteo.settenvini@montecristosoftware.eu>
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
#include "cares_init.hh"
|
|
|
|
#include <ares.h>
|
|
|
|
#include <string>
|
|
#include <system_error>
|
|
|
|
using namespace std::literals;
|
|
|
|
auto malcontent::CAresLibrary::instance() -> std::shared_ptr<CAresLibrary> {
|
|
if (auto ret = _instance.lock()) {
|
|
return ret;
|
|
}
|
|
|
|
auto ret = std::make_shared<CAresLibrary>(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> malcontent::CAresLibrary::_instance{};
|