Skip to content

Commit 8dfb653

Browse files
committed
Fix build errors
1 parent 1d2c9b3 commit 8dfb653

3 files changed

Lines changed: 37 additions & 6 deletions

File tree

HTTP/async_http.cpp

Lines changed: 33 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,31 @@
1515

1616
using namespace dmq;
1717

18+
// ---------------------------------------------------------------------------
19+
// Reference-counted curl global init/cleanup — safe for multiple AsyncHttp
20+
// instances in the same process.
21+
// ---------------------------------------------------------------------------
22+
static std::mutex g_curlMutex;
23+
static int g_curlRefCount = 0;
24+
25+
static int curl_global_addref()
26+
{
27+
std::lock_guard<std::mutex> lock(g_curlMutex);
28+
if (g_curlRefCount == 0) {
29+
CURLcode res = curl_global_init(CURL_GLOBAL_DEFAULT);
30+
if (res != CURLE_OK) return static_cast<int>(res);
31+
}
32+
++g_curlRefCount;
33+
return 0;
34+
}
35+
36+
static void curl_global_release()
37+
{
38+
std::lock_guard<std::mutex> lock(g_curlMutex);
39+
if (g_curlRefCount > 0 && --g_curlRefCount == 0)
40+
curl_global_cleanup();
41+
}
42+
1843
namespace async {
1944

2045
// -----------------------------------------------------------------------
@@ -64,7 +89,7 @@ namespace async {
6489

6590
AsyncHttp::~AsyncHttp()
6691
{
67-
if (m_curl || m_thread.GetThreadId() != std::thread::id())
92+
if (m_running)
6893
shutdown();
6994
}
7095

@@ -180,27 +205,29 @@ namespace async {
180205

181206
int AsyncHttp::init()
182207
{
183-
if (m_curl) return 0; // already initialized
208+
if (m_running) return 0; // already initialized
184209

185-
CURLcode res = curl_global_init(CURL_GLOBAL_DEFAULT);
186-
if (res != CURLE_OK) return static_cast<int>(res);
210+
int res = curl_global_addref();
211+
if (res != 0) return res;
187212

188213
m_curl = curl_easy_init();
189214
if (!m_curl) return -1;
190215

191216
m_thread.CreateThread();
217+
m_running = true;
192218
return 0;
193219
}
194220

195221
int AsyncHttp::shutdown(dmq::Duration timeout)
196222
{
197-
if (m_thread.GetThreadId() != std::thread::id()) {
223+
if (m_running) {
198224
auto fn = std::function<void()>([this]() { HttpCleanup(); });
199225
auto delegate = MakeDelegate(fn, m_thread, timeout);
200226
delegate.AsyncInvoke();
201227
m_thread.ExitThread();
228+
m_running = false;
202229
}
203-
curl_global_cleanup();
230+
curl_global_release();
204231
return 0;
205232
}
206233

HTTP/async_http.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ namespace async {
125125
private:
126126
Thread m_thread;
127127
CURL* m_curl = nullptr;
128+
bool m_running = false;
128129

129130
static size_t WriteCallback(char* ptr, size_t size, size_t nmemb, void* userdata);
130131

UnitTest/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ add_library(UnitTestLib STATIC ${SUBDIR_SOURCES})
77
# Include this directory so consumers can #include "async_http_ut.h"
88
target_include_directories(UnitTestLib PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}")
99

10+
# HttpLib is a dependency — pulls in async_http.h and curl headers transitively
11+
target_link_libraries(UnitTestLib PUBLIC HttpLib)
12+
1013
# Add /bigobj flag for MSVC
1114
if (MSVC)
1215
target_compile_options(UnitTestLib PRIVATE /bigobj)

0 commit comments

Comments
 (0)