Skip to content

Commit

Permalink
Using try_lock to avoid blockage
Browse files Browse the repository at this point in the history
  • Loading branch information
atupone committed Oct 22, 2024
1 parent bc4826d commit f48c953
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions src/net/AresHandler.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -163,19 +163,25 @@ void AresHandler::callback(int callbackStatus, struct hostent *hostent)

const char *AresHandler::getHostname()
{
const std::lock_guard<std::mutex> lock(callback_mutex);

return hostName.c_str();
if (!callback_mutex.try_lock())
return "";
const char *host = hostName.c_str();
callback_mutex.unlock();
return host;
}

AresHandler::ResolutionStatus AresHandler::getHostAddress(struct in_addr
*clientAddr)
{
const std::lock_guard<std::mutex> lock(callback_mutex);
if (!callback_mutex.try_lock())
return HbNPending;

const ResolutionStatus oldStatus = status;
if (status == HbNSucceeded)
memcpy(clientAddr, &hostAddress, sizeof(hostAddress));
return status;
callback_mutex.unlock();

return oldStatus;
}

#if ARES_VERSION_MAJOR > 1 || ARES_VERSION_MINOR >= 26
Expand Down

0 comments on commit f48c953

Please sign in to comment.