Skip to content

Commit f48c953

Browse files
committed
Using try_lock to avoid blockage
1 parent bc4826d commit f48c953

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

src/net/AresHandler.cxx

+11-5
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,25 @@ void AresHandler::callback(int callbackStatus, struct hostent *hostent)
163163

164164
const char *AresHandler::getHostname()
165165
{
166-
const std::lock_guard<std::mutex> lock(callback_mutex);
167-
168-
return hostName.c_str();
166+
if (!callback_mutex.try_lock())
167+
return "";
168+
const char *host = hostName.c_str();
169+
callback_mutex.unlock();
170+
return host;
169171
}
170172

171173
AresHandler::ResolutionStatus AresHandler::getHostAddress(struct in_addr
172174
*clientAddr)
173175
{
174-
const std::lock_guard<std::mutex> lock(callback_mutex);
176+
if (!callback_mutex.try_lock())
177+
return HbNPending;
175178

179+
const ResolutionStatus oldStatus = status;
176180
if (status == HbNSucceeded)
177181
memcpy(clientAddr, &hostAddress, sizeof(hostAddress));
178-
return status;
182+
callback_mutex.unlock();
183+
184+
return oldStatus;
179185
}
180186

181187
#if ARES_VERSION_MAJOR > 1 || ARES_VERSION_MINOR >= 26

0 commit comments

Comments
 (0)