Index: runtime/bin/eventhandler_linux.cc |
diff --git a/runtime/bin/eventhandler_linux.cc b/runtime/bin/eventhandler_linux.cc |
index 0f60633aea5805c940af41b66b053e11022ed579..feb01dcf9f1f2746f835aafc24021243fd3bc7a0 100644 |
--- a/runtime/bin/eventhandler_linux.cc |
+++ b/runtime/bin/eventhandler_linux.cc |
@@ -31,12 +31,26 @@ static const int kInfinityTimeout = -1; |
static const int kTimerId = -1; |
+ |
+void SocketData::FillPollEvents(struct pollfd* pollfds) { |
+ // Do not ask for POLLERR and POLLHUP explicitly as they are |
+ // triggered anyway. |
+ if ((_mask & (1 << kInEvent)) != 0) { |
+ pollfds->events |= POLLIN; |
+ } |
+ if ((_mask & (1 << kOutEvent)) != 0) { |
+ pollfds->events |= POLLOUT; |
+ } |
+ pollfds->events |= POLLRDHUP; |
+} |
+ |
+ |
EventHandlerImplementation::EventHandlerImplementation() { |
intptr_t result; |
port_map_entries_ = 0; |
Mads Ager (google)
2011/11/01 09:10:23
Should we either keep the name PortData or call th
Søren Gjesse
2011/11/01 11:34:21
Done.
|
port_map_size_ = kInitialPortMapSize; |
- port_map_ = reinterpret_cast<PortData*>(calloc(port_map_size_, |
- sizeof(PortData))); |
+ port_map_ = reinterpret_cast<SocketData*>(calloc(port_map_size_, |
+ sizeof(SocketData))); |
Mads Ager (google)
2011/11/01 09:10:23
funky indentation.
Søren Gjesse
2011/11/01 11:34:21
Done.
|
ASSERT(port_map_ != NULL); |
result = pipe(interrupt_fds_); |
if (result != 0) { |
@@ -57,47 +71,43 @@ EventHandlerImplementation::~EventHandlerImplementation() { |
// TODO(hpayer): Use hash table instead of array. |
-void EventHandlerImplementation::SetPort(intptr_t fd, |
- Dart_Port dart_port, |
- intptr_t mask) { |
+SocketData* EventHandlerImplementation::GetSocketData(intptr_t fd) { |
ASSERT(fd >= 0); |
if (fd >= port_map_size_) { |
intptr_t new_port_map_size = port_map_size_; |
do { |
new_port_map_size = new_port_map_size * kPortMapGrowingFactor; |
} while (fd >= new_port_map_size); |
- size_t new_port_map_bytes = new_port_map_size * sizeof(PortData); |
- port_map_ = reinterpret_cast<PortData*>(realloc(port_map_, |
- new_port_map_bytes)); |
+ size_t new_port_map_bytes = new_port_map_size * sizeof(SocketData); |
+ port_map_ = reinterpret_cast<SocketData*>(realloc(port_map_, |
+ new_port_map_bytes)); |
ASSERT(port_map_ != NULL); |
- size_t port_map_bytes = port_map_size_ * sizeof(PortData); |
+ size_t port_map_bytes = port_map_size_ * sizeof(SocketData); |
memset(port_map_ + port_map_size_, |
0, |
new_port_map_bytes - port_map_bytes); |
port_map_size_ = new_port_map_size; |
} |
- /* |
- * Only change the port map entries count if SetPort changes |
- * the port map state. |
- */ |
- if (dart_port == 0 && PortFor(fd) != 0) { |
- port_map_entries_--; |
- } else if (dart_port != 0 && PortFor(fd) == 0) { |
- port_map_entries_++; |
- } |
- port_map_[fd].dart_port = dart_port; |
- port_map_[fd].mask = mask; |
+ return port_map_ + fd; |
} |
-Dart_Port EventHandlerImplementation::PortFor(intptr_t fd) { |
- return port_map_[fd].dart_port; |
-} |
+void EventHandlerImplementation::SetPort(intptr_t fd, |
+ Dart_Port dart_port, |
+ intptr_t mask) { |
+ SocketData* sd = GetSocketData(fd); |
+ // Only change the port map entries count if SetPort changes the |
+ // port map state. |
+ if (dart_port == 0 && sd->port() != 0) { |
+ port_map_entries_--; |
+ } else if (dart_port != 0 && sd->port() == 0) { |
+ port_map_entries_++; |
+ } |
-bool EventHandlerImplementation::IsListeningSocket(intptr_t fd) { |
- return (port_map_[fd].mask & (1 << kListeningSocket)) != 0; |
+ sd->set_port(dart_port); |
+ sd->set_mask(mask); |
} |
@@ -139,20 +149,6 @@ void EventHandlerImplementation::WakeupHandler(intptr_t id, |
} |
-void EventHandlerImplementation::SetPollEvents(struct pollfd* pollfds, |
- intptr_t mask) { |
- // Do not ask for POLLERR and POLLHUP explicitly as they are |
- // triggered anyway. |
- if ((mask & (1 << kInEvent)) != 0) { |
- pollfds->events |= POLLIN; |
- } |
- if ((mask & (1 << kOutEvent)) != 0) { |
- pollfds->events |= POLLOUT; |
- } |
- pollfds->events |= POLLRDHUP; |
-} |
- |
- |
struct pollfd* EventHandlerImplementation::GetPollFds(intptr_t* pollfds_size) { |
struct pollfd* pollfds; |
@@ -165,10 +161,11 @@ struct pollfd* EventHandlerImplementation::GetPollFds(intptr_t* pollfds_size) { |
// TODO(hpayer): optimize the following iteration over the hash map |
int j = 1; |
for (int i = 0; i < port_map_size_; i++) { |
- if (port_map_[i].dart_port != 0) { |
+ SocketData* sd = &port_map_[i]; |
+ if (sd->port() != 0) { |
// Fd is added to the poll set. |
pollfds[j].fd = i; |
- SetPollEvents(&pollfds[j], port_map_[i].mask); |
+ sd->FillPollEvents(&pollfds[j]); |
j++; |
} |
} |
@@ -216,7 +213,8 @@ void EventHandlerImplementation::HandleInterruptFd() { |
intptr_t EventHandlerImplementation::GetPollEvents(struct pollfd* pollfd) { |
intptr_t event_mask = 0; |
- if (IsListeningSocket(pollfd->fd)) { |
+ SocketData* sd = GetSocketData(pollfd->fd); |
+ if (sd->IsListeningSocket()) { |
// For listening sockets the POLLIN event indicate that there are |
// connections ready for accept unless accompanied with one of the |
// other flags. |
@@ -260,7 +258,7 @@ void EventHandlerImplementation::HandleEvents(struct pollfd* pollfds, |
intptr_t event_mask = GetPollEvents(&pollfds[i]); |
if (event_mask != 0) { |
intptr_t fd = pollfds[i].fd; |
- Dart_Port port = PortFor(fd); |
+ Dart_Port port = GetSocketData(fd)->port(); |
ASSERT(port != 0); |
UnregisterFd(fd); |
Dart_PostIntArray(port, 1, &event_mask); |