Commit bb46a529 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook GitHub Bot

revisions to win32 SocketFileDescriptorMap

Summary:
Make the map and the mutex leaky singletons, grouping them together.

Tweak variable names.

Avoid double work in overloads of `close`.

Reviewed By: Orvid

Differential Revision: D31065456

fbshipit-source-id: eec1474909706df3330bd3464ebeb90ad768607b
parent cc9032a0
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#ifdef _WIN32 #ifdef _WIN32
#include <optional>
#include <shared_mutex> #include <shared_mutex>
#include <unordered_map> #include <unordered_map>
...@@ -30,8 +31,19 @@ namespace folly { ...@@ -30,8 +31,19 @@ namespace folly {
namespace netops { namespace netops {
namespace detail { namespace detail {
static std::unordered_map<SOCKET, int> socketMap; namespace {
static std::shared_mutex socketMapMutex;
struct SyncSocketMap {
std::unordered_map<SOCKET, int> map;
std::shared_mutex mutex;
};
SyncSocketMap& getSyncSocketMap() {
static auto& instance = *new SyncSocketMap();
return instance;
}
} // namespace
static int closeOnlyFileDescriptor(int fd) { static int closeOnlyFileDescriptor(int fd) {
HANDLE h = (HANDLE)_get_osfhandle(fd); HANDLE h = (HANDLE)_get_osfhandle(fd);
...@@ -79,11 +91,10 @@ static int closeOnlyFileDescriptor(int fd) { ...@@ -79,11 +91,10 @@ static int closeOnlyFileDescriptor(int fd) {
int SocketFileDescriptorMap::close(int fd) noexcept { int SocketFileDescriptorMap::close(int fd) noexcept {
auto hand = SocketFileDescriptorMap::fdToSocket(fd); auto hand = SocketFileDescriptorMap::fdToSocket(fd);
auto& smap = getSyncSocketMap();
{ {
std::unique_lock<std::shared_mutex> lock{socketMapMutex}; std::unique_lock lock{smap.mutex};
if (socketMap.find(hand) != socketMap.end()) { smap.map.erase(hand);
socketMap.erase(hand);
}
} }
auto r = closeOnlyFileDescriptor(fd); auto r = closeOnlyFileDescriptor(fd);
if (r != 0) { if (r != 0) {
...@@ -93,19 +104,18 @@ int SocketFileDescriptorMap::close(int fd) noexcept { ...@@ -93,19 +104,18 @@ int SocketFileDescriptorMap::close(int fd) noexcept {
} }
int SocketFileDescriptorMap::close(SOCKET sock) noexcept { int SocketFileDescriptorMap::close(SOCKET sock) noexcept {
bool found = false; std::optional<int> fd{};
int fd = 0; auto& smap = getSyncSocketMap();
{ {
std::unique_lock<std::shared_mutex> lock{socketMapMutex}; std::shared_lock lock{smap.mutex};
auto lookup = socketMap.find(sock); auto it = smap.map.find(sock);
found = lookup != socketMap.end(); if (it != smap.map.end()) {
if (found) { fd = it->second;
fd = lookup->second;
} }
} }
if (found) { if (fd) {
return SocketFileDescriptorMap::close(fd); return SocketFileDescriptorMap::close(fd.value());
} }
return closesocket(sock); return closesocket(sock);
...@@ -124,22 +134,23 @@ int SocketFileDescriptorMap::socketToFd(SOCKET sock) noexcept { ...@@ -124,22 +134,23 @@ int SocketFileDescriptorMap::socketToFd(SOCKET sock) noexcept {
return -1; return -1;
} }
auto& smap = getSyncSocketMap();
{ {
std::shared_lock<std::shared_mutex> lock{socketMapMutex}; std::shared_lock lock{smap.mutex};
auto const found = socketMap.find(sock); auto const it = smap.map.find(sock);
if (found != socketMap.end()) { if (it != smap.map.end()) {
return found->second; return it->second;
} }
} }
std::unique_lock<std::shared_mutex> lock{socketMapMutex}; std::unique_lock lock{smap.mutex};
auto const found = socketMap.find(sock); auto const it = smap.map.find(sock);
if (found != socketMap.end()) { if (it != smap.map.end()) {
return found->second; return it->second;
} }
int fd = _open_osfhandle((intptr_t)sock, O_RDWR | O_BINARY); int fd = _open_osfhandle((intptr_t)sock, O_RDWR | O_BINARY);
socketMap.emplace(sock, fd); smap.map.emplace(sock, fd);
return fd; return fd;
} }
} // namespace detail } // namespace detail
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment