Index: dart/runtime/bin/eventhandler_linux.cc |
diff --git a/dart/runtime/bin/eventhandler_linux.cc b/dart/runtime/bin/eventhandler_linux.cc |
index 97b305b61e39d05aff432978f25ef07cf846879a..64d19df06b4928e21d74c5ab2077ad7f44dd7738 100644 |
--- a/dart/runtime/bin/eventhandler_linux.cc |
+++ b/dart/runtime/bin/eventhandler_linux.cc |
@@ -6,6 +6,7 @@ |
#if defined(TARGET_OS_LINUX) |
#include "bin/eventhandler.h" |
+#include "bin/eventhandler_linux.h" |
#include <errno.h> // NOLINT |
#include <pthread.h> // NOLINT |
@@ -20,6 +21,7 @@ |
#include "bin/dartutils.h" |
#include "bin/fdutils.h" |
#include "bin/log.h" |
+#include "bin/lockers.h" |
#include "bin/socket.h" |
#include "bin/thread.h" |
#include "platform/utils.h" |
@@ -28,51 +30,49 @@ |
namespace dart { |
namespace bin { |
-static const int kInterruptMessageSize = sizeof(InterruptMessage); |
-static const int kTimerId = -1; |
-static const int kShutdownId = -2; |
- |
-intptr_t SocketData::GetPollEvents() { |
+intptr_t DescriptorInfoLinux::GetPollEvents() { |
// Do not ask for EPOLLERR and EPOLLHUP explicitly as they are |
// triggered anyway. |
intptr_t events = 0; |
- if ((mask_ & (1 << kInEvent)) != 0) { |
+ if ((Mask() & (1 << kInEvent)) != 0) { |
events |= EPOLLIN; |
} |
- if ((mask_ & (1 << kOutEvent)) != 0) { |
+ if ((Mask() & (1 << kOutEvent)) != 0) { |
events |= EPOLLOUT; |
} |
return events; |
} |
-// Unregister the file descriptor for a SocketData structure with epoll. |
-static void RemoveFromEpollInstance(intptr_t epoll_fd_, SocketData* sd) { |
+// Unregister the file descriptor for a DescriptorInfoLinux structure with |
+// epoll. |
+static void RemoveFromEpollInstance(intptr_t epoll_fd_, |
+ DescriptorInfoLinux* si) { |
VOID_NO_RETRY_EXPECTED(epoll_ctl(epoll_fd_, |
EPOLL_CTL_DEL, |
- sd->fd(), |
+ si->fd(), |
NULL)); |
} |
-static void AddToEpollInstance(intptr_t epoll_fd_, SocketData* sd) { |
+static void AddToEpollInstance(intptr_t epoll_fd_, DescriptorInfoLinux* si) { |
struct epoll_event event; |
- event.events = EPOLLRDHUP | sd->GetPollEvents(); |
- if (!sd->IsListeningSocket()) { |
+ event.events = EPOLLRDHUP | si->GetPollEvents(); |
+ if (!si->IsListeningSocket()) { |
event.events |= EPOLLET; |
} |
- event.data.ptr = sd; |
+ event.data.ptr = si; |
int status = NO_RETRY_EXPECTED(epoll_ctl(epoll_fd_, |
EPOLL_CTL_ADD, |
- sd->fd(), |
+ si->fd(), |
&event)); |
if (status == -1) { |
// Epoll does not accept the file descriptor. It could be due to |
// already closed file descriptor, or unuspported devices, such |
// as /dev/null. In such case, mark the file descriptor as closed, |
// so dart will handle it accordingly. |
- DartUtils::PostInt32(sd->port(), 1 << kCloseEvent); |
+ DartUtils::PostInt32(si->NextPort(), 1 << kCloseEvent); |
} |
} |
@@ -133,25 +133,26 @@ EventHandlerImplementation::~EventHandlerImplementation() { |
} |
-SocketData* EventHandlerImplementation::GetSocketData(intptr_t fd, |
- bool is_listening) { |
+DescriptorInfoLinux* EventHandlerImplementation::GetDescriptorInfoLinux( |
+ intptr_t fd, bool is_listening) { |
ASSERT(fd >= 0); |
HashMap::Entry* entry = socket_map_.Lookup( |
GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd), true); |
ASSERT(entry != NULL); |
- SocketData* sd = reinterpret_cast<SocketData*>(entry->value); |
- if (sd == NULL) { |
+ DescriptorInfoLinux * si = |
+ reinterpret_cast<DescriptorInfoLinux*>(entry->value); |
+ if (si == NULL) { |
// If there is no data in the hash map for this file descriptor a |
- // new SocketData for the file descriptor is inserted. |
+ // new DescriptorInfoLinux for the file descriptor is inserted. |
if (is_listening) { |
- sd = new ListeningSocketData(fd); |
+ si = new DescriptorInfoMultipleLinux(fd); |
} else { |
- sd = new SocketData(fd); |
+ si = new DescriptorInfoSingleLinux(fd); |
} |
- entry->value = sd; |
+ entry->value = si; |
} |
- ASSERT(fd == sd->fd()); |
- return sd; |
+ ASSERT(fd == si->fd()); |
+ return si; |
} |
@@ -197,38 +198,60 @@ void EventHandlerImplementation::HandleInterruptFd() { |
} else if (msg[i].id == kShutdownId) { |
shutdown_ = true; |
} else { |
- SocketData* sd = GetSocketData( |
+ DescriptorInfoLinux* si = GetDescriptorInfoLinux( |
msg[i].id, (msg[i].data & (1 << kListeningSocket)) != 0); |
if (IS_COMMAND(msg[i].data, kShutdownReadCommand)) { |
- ASSERT(!sd->IsListeningSocket()); |
+ ASSERT(!si->IsListeningSocket()); |
// Close the socket for reading. |
- VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_RD)); |
+ VOID_NO_RETRY_EXPECTED(shutdown(si->fd(), SHUT_RD)); |
} else if (IS_COMMAND(msg[i].data, kShutdownWriteCommand)) { |
- ASSERT(!sd->IsListeningSocket()); |
+ ASSERT(!si->IsListeningSocket()); |
// Close the socket for writing. |
- VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_WR)); |
+ VOID_NO_RETRY_EXPECTED(shutdown(si->fd(), SHUT_WR)); |
} else if (IS_COMMAND(msg[i].data, kCloseCommand)) { |
// Close the socket and free system resources and move on to next |
// message. |
- if (sd->RemovePort(msg[i].dart_port)) { |
- RemoveFromEpollInstance(epoll_fd_, sd); |
- intptr_t fd = sd->fd(); |
- sd->Close(); |
- socket_map_.Remove(GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); |
- delete sd; |
+ bool no_more_listeners = si->RemovePort(msg[i].dart_port); |
+ if (no_more_listeners) { |
+ RemoveFromEpollInstance(epoll_fd_, si); |
+ } |
+ |
+ if (si->IsListeningSocket()) { |
+ intptr_t fd = si->fd(); |
+ // We only close the socket file descriptor from the operating |
+ // system if there are no other dart socket objects which |
+ // are listening on the same (address, port) combination. |
+ { |
+ MutexLocker ml(globalTcpListeningSocketRegistry.mutex()); |
+ if (globalTcpListeningSocketRegistry.CloseSafe(si->fd())) { |
+ ASSERT(no_more_listeners); |
+ socket_map_.Remove( |
+ GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); |
+ si->Close(); |
+ delete si; |
+ } |
+ } |
+ } else { |
+ if (no_more_listeners) { |
+ intptr_t fd = si->fd(); |
+ socket_map_.Remove( |
+ GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); |
+ si->Close(); |
+ delete si; |
+ } |
} |
DartUtils::PostInt32(msg[i].dart_port, 1 << kDestroyedEvent); |
} else if (IS_COMMAND(msg[i].data, kReturnTokenCommand)) { |
int count = TOKEN_COUNT(msg[i].data); |
- if (sd->ReturnToken(msg[i].dart_port, count)) { |
- AddToEpollInstance(epoll_fd_, sd); |
+ if (si->ReturnTokens(msg[i].dart_port, count)) { |
+ AddToEpollInstance(epoll_fd_, si); |
} |
} else { |
ASSERT_NO_COMMAND(msg[i].data); |
// Setup events to wait for. |
- if (sd->AddPort(msg[i].dart_port)) { |
- sd->SetMask(msg[i].data); |
- AddToEpollInstance(epoll_fd_, sd); |
+ if (si->AddPort(msg[i].dart_port)) { |
+ si->SetMask(msg[i].data); |
+ AddToEpollInstance(epoll_fd_, si); |
} |
} |
} |
@@ -256,9 +279,9 @@ static void PrintEventMask(intptr_t fd, intptr_t events) { |
#endif |
intptr_t EventHandlerImplementation::GetPollEvents(intptr_t events, |
- SocketData* sd) { |
+ DescriptorInfoLinux* si) { |
#ifdef DEBUG_POLL |
- PrintEventMask(sd->fd(), events); |
+ PrintEventMask(si->fd(), events); |
#endif |
if (events & EPOLLERR) { |
// Return error only if EPOLLIN is present. |
@@ -287,15 +310,16 @@ void EventHandlerImplementation::HandleEvents(struct epoll_event* events, |
timeout_queue_.RemoveCurrent(); |
} |
} else { |
- SocketData* sd = reinterpret_cast<SocketData*>(events[i].data.ptr); |
- intptr_t event_mask = GetPollEvents(events[i].events, sd); |
+ DescriptorInfoLinux* si = |
+ reinterpret_cast<DescriptorInfoLinux*>(events[i].data.ptr); |
+ intptr_t event_mask = GetPollEvents(events[i].events, si); |
if (event_mask != 0) { |
- Dart_Port port = sd->port(); |
- if (sd->TakeToken()) { |
+ Dart_Port port = si->NextPort(); |
+ ASSERT(port != 0); |
+ if (si->TakeToken()) { |
// Took last token, remove from epoll. |
- RemoveFromEpollInstance(epoll_fd_, sd); |
+ RemoveFromEpollInstance(epoll_fd_, si); |
} |
- ASSERT(port != 0); |
DartUtils::PostInt32(port, event_mask); |
} |
} |
@@ -333,7 +357,7 @@ void EventHandlerImplementation::Poll(uword args) { |
void EventHandlerImplementation::Start(EventHandler* handler) { |
int result = Thread::Start(&EventHandlerImplementation::Poll, |
- reinterpret_cast<uword>(handler)); |
+ reinterpret_cast<uword>(handler)); |
if (result != 0) { |
FATAL1("Failed to start event handler thread %d", result); |
} |