Index: dart/runtime/bin/eventhandler_linux.cc |
diff --git a/dart/runtime/bin/eventhandler_linux.cc b/dart/runtime/bin/eventhandler_linux.cc |
index ef0a11dfce65c275e6c48db8251e119e7ed47c44..97b305b61e39d05aff432978f25ef07cf846879a 100644 |
--- a/dart/runtime/bin/eventhandler_linux.cc |
+++ b/dart/runtime/bin/eventhandler_linux.cc |
@@ -6,7 +6,6 @@ |
#if defined(TARGET_OS_LINUX) |
#include "bin/eventhandler.h" |
-#include "bin/eventhandler_linux.h" |
#include <errno.h> // NOLINT |
#include <pthread.h> // NOLINT |
@@ -21,7 +20,6 @@ |
#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" |
@@ -30,52 +28,51 @@ |
namespace dart { |
namespace bin { |
+static const int kInterruptMessageSize = sizeof(InterruptMessage); |
+static const int kTimerId = -1; |
+static const int kShutdownId = -2; |
-intptr_t DescriptorInfo::GetPollEvents() { |
+ |
+intptr_t SocketData::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 DescriptorInfo structure with |
-// epoll. |
-static void RemoveFromEpollInstance(intptr_t epoll_fd_, |
- DescriptorInfo* di) { |
+// Unregister the file descriptor for a SocketData structure with epoll. |
+static void RemoveFromEpollInstance(intptr_t epoll_fd_, SocketData* sd) { |
VOID_NO_RETRY_EXPECTED(epoll_ctl(epoll_fd_, |
EPOLL_CTL_DEL, |
- di->fd(), |
+ sd->fd(), |
NULL)); |
} |
-static void AddToEpollInstance(intptr_t epoll_fd_, DescriptorInfo* di) { |
+static void AddToEpollInstance(intptr_t epoll_fd_, SocketData* sd) { |
struct epoll_event event; |
- event.events = EPOLLRDHUP | di->GetPollEvents(); |
- if (!di->IsListeningSocket()) { |
+ event.events = EPOLLRDHUP | sd->GetPollEvents(); |
+ if (!sd->IsListeningSocket()) { |
event.events |= EPOLLET; |
} |
- event.data.ptr = di; |
+ event.data.ptr = sd; |
int status = NO_RETRY_EXPECTED(epoll_ctl(epoll_fd_, |
EPOLL_CTL_ADD, |
- di->fd(), |
+ sd->fd(), |
&event)); |
if (status == -1) { |
- // TODO(kustermann): Verify that the dart end is handling this correctly & |
- // adapt this code to work for multiple listening sockets. |
- |
// 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(di->NextPort(), 1 << kCloseEvent); |
+ DartUtils::PostInt32(sd->port(), 1 << kCloseEvent); |
} |
} |
@@ -136,26 +133,25 @@ EventHandlerImplementation::~EventHandlerImplementation() { |
} |
-DescriptorInfo* EventHandlerImplementation::GetDescriptorInfo( |
- intptr_t fd, bool is_listening) { |
+SocketData* EventHandlerImplementation::GetSocketData(intptr_t fd, |
+ bool is_listening) { |
ASSERT(fd >= 0); |
HashMap::Entry* entry = socket_map_.Lookup( |
GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd), true); |
ASSERT(entry != NULL); |
- DescriptorInfo* di = |
- reinterpret_cast<DescriptorInfo*>(entry->value); |
- if (di == NULL) { |
+ SocketData* sd = reinterpret_cast<SocketData*>(entry->value); |
+ if (sd == NULL) { |
// If there is no data in the hash map for this file descriptor a |
- // new DescriptorInfo for the file descriptor is inserted. |
+ // new SocketData for the file descriptor is inserted. |
if (is_listening) { |
- di = new DescriptorInfoMultiple(fd); |
+ sd = new ListeningSocketData(fd); |
} else { |
- di = new DescriptorInfoSingle(fd); |
+ sd = new SocketData(fd); |
} |
- entry->value = di; |
+ entry->value = sd; |
} |
- ASSERT(fd == di->fd()); |
- return di; |
+ ASSERT(fd == sd->fd()); |
+ return sd; |
} |
@@ -201,62 +197,38 @@ void EventHandlerImplementation::HandleInterruptFd() { |
} else if (msg[i].id == kShutdownId) { |
shutdown_ = true; |
} else { |
- DescriptorInfo* di = GetDescriptorInfo( |
+ SocketData* sd = GetSocketData( |
msg[i].id, (msg[i].data & (1 << kListeningSocket)) != 0); |
if (IS_COMMAND(msg[i].data, kShutdownReadCommand)) { |
- ASSERT(!di->IsListeningSocket()); |
+ ASSERT(!sd->IsListeningSocket()); |
// Close the socket for reading. |
- VOID_NO_RETRY_EXPECTED(shutdown(di->fd(), SHUT_RD)); |
+ VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_RD)); |
} else if (IS_COMMAND(msg[i].data, kShutdownWriteCommand)) { |
- ASSERT(!di->IsListeningSocket()); |
+ ASSERT(!sd->IsListeningSocket()); |
// Close the socket for writing. |
- VOID_NO_RETRY_EXPECTED(shutdown(di->fd(), SHUT_WR)); |
+ VOID_NO_RETRY_EXPECTED(shutdown(sd->fd(), SHUT_WR)); |
} else if (IS_COMMAND(msg[i].data, kCloseCommand)) { |
// Close the socket and free system resources and move on to next |
// message. |
- bool no_more_listeners = di->RemovePort(msg[i].dart_port); |
- if (no_more_listeners) { |
- RemoveFromEpollInstance(epoll_fd_, di); |
- } |
- |
- intptr_t fd = di->fd(); |
- if (di->IsListeningSocket()) { |
- // 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(fd)) { |
- socket_map_.Remove( |
- GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); |
- di->Close(); |
- delete di; |
- } |
- } |
- } else { |
- ASSERT(no_more_listeners); |
- socket_map_.Remove( |
- GetHashmapKeyFromFd(fd), GetHashmapHashFromFd(fd)); |
- di->Close(); |
- delete di; |
+ 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; |
} |
DartUtils::PostInt32(msg[i].dart_port, 1 << kDestroyedEvent); |
} else if (IS_COMMAND(msg[i].data, kReturnTokenCommand)) { |
int count = TOKEN_COUNT(msg[i].data); |
- if (di->ReturnTokens(msg[i].dart_port, count)) { |
- AddToEpollInstance(epoll_fd_, di); |
+ if (sd->ReturnToken(msg[i].dart_port, count)) { |
+ AddToEpollInstance(epoll_fd_, sd); |
} |
} else { |
ASSERT_NO_COMMAND(msg[i].data); |
- bool had_listeners = di->HasNextPort(); |
- di->SetPortAndMask(msg[i].dart_port, msg[i].data & EVENT_MASK); |
- bool has_listeners = di->HasNextPort(); |
- |
- // Add/Remove from epoll set depending on previous and current state. |
- if (!had_listeners && has_listeners) { |
- AddToEpollInstance(epoll_fd_, di); |
- } else if (had_listeners && !has_listeners) { |
- RemoveFromEpollInstance(epoll_fd_, di); |
+ // Setup events to wait for. |
+ if (sd->AddPort(msg[i].dart_port)) { |
+ sd->SetMask(msg[i].data); |
+ AddToEpollInstance(epoll_fd_, sd); |
} |
} |
} |
@@ -284,9 +256,9 @@ static void PrintEventMask(intptr_t fd, intptr_t events) { |
#endif |
intptr_t EventHandlerImplementation::GetPollEvents(intptr_t events, |
- DescriptorInfo* di) { |
+ SocketData* sd) { |
#ifdef DEBUG_POLL |
- PrintEventMask(di->fd(), events); |
+ PrintEventMask(sd->fd(), events); |
#endif |
if (events & EPOLLERR) { |
// Return error only if EPOLLIN is present. |
@@ -315,16 +287,15 @@ void EventHandlerImplementation::HandleEvents(struct epoll_event* events, |
timeout_queue_.RemoveCurrent(); |
} |
} else { |
- DescriptorInfo* di = |
- reinterpret_cast<DescriptorInfo*>(events[i].data.ptr); |
- intptr_t event_mask = GetPollEvents(events[i].events, di); |
+ SocketData* sd = reinterpret_cast<SocketData*>(events[i].data.ptr); |
+ intptr_t event_mask = GetPollEvents(events[i].events, sd); |
if (event_mask != 0) { |
- Dart_Port port = di->NextPort(); |
- ASSERT(port != 0); |
- if (di->TakeToken()) { |
+ Dart_Port port = sd->port(); |
+ if (sd->TakeToken()) { |
// Took last token, remove from epoll. |
- RemoveFromEpollInstance(epoll_fd_, di); |
+ RemoveFromEpollInstance(epoll_fd_, sd); |
} |
+ ASSERT(port != 0); |
DartUtils::PostInt32(port, event_mask); |
} |
} |
@@ -362,7 +333,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); |
} |