| Index: dart/runtime/bin/eventhandler_macos.cc
|
| diff --git a/dart/runtime/bin/eventhandler_macos.cc b/dart/runtime/bin/eventhandler_macos.cc
|
| index f8c1dea8c4983fff4db80bb6f5d9799b3925cf9f..49ec2477e19b132b0033d078f664a68787d0c207 100644
|
| --- a/dart/runtime/bin/eventhandler_macos.cc
|
| +++ b/dart/runtime/bin/eventhandler_macos.cc
|
| @@ -6,7 +6,6 @@
|
| #if defined(TARGET_OS_MACOS)
|
|
|
| #include "bin/eventhandler.h"
|
| -#include "bin/eventhandler_macos.h"
|
|
|
| #include <errno.h> // NOLINT
|
| #include <pthread.h> // NOLINT
|
| @@ -18,9 +17,7 @@
|
|
|
| #include "bin/dartutils.h"
|
| #include "bin/fdutils.h"
|
| -#include "bin/lockers.h"
|
| #include "bin/log.h"
|
| -#include "bin/socket.h"
|
| #include "bin/thread.h"
|
| #include "bin/utils.h"
|
| #include "platform/hashmap.h"
|
| @@ -30,61 +27,66 @@
|
| namespace dart {
|
| namespace bin {
|
|
|
| +static const int kInterruptMessageSize = sizeof(InterruptMessage);
|
| +static const int kInfinityTimeout = -1;
|
| +static const int kTimerId = -1;
|
| +static const int kShutdownId = -2;
|
|
|
| -bool DescriptorInfo::HasReadEvent() {
|
| - return (Mask() & (1 << kInEvent)) != 0;
|
| +
|
| +bool SocketData::HasReadEvent() {
|
| + return (mask_ & (1 << kInEvent)) != 0;
|
| }
|
|
|
|
|
| -bool DescriptorInfo::HasWriteEvent() {
|
| - return (Mask() & (1 << kOutEvent)) != 0;
|
| +bool SocketData::HasWriteEvent() {
|
| + return (mask_ & (1 << kOutEvent)) != 0;
|
| }
|
|
|
|
|
| // Unregister the file descriptor for a SocketData structure with kqueue.
|
| -static void RemoveFromKqueue(intptr_t kqueue_fd_, DescriptorInfo* di) {
|
| - if (!di->tracked_by_kqueue()) return;
|
| +static void RemoveFromKqueue(intptr_t kqueue_fd_, SocketData* sd) {
|
| + if (!sd->tracked_by_kqueue()) return;
|
| static const intptr_t kMaxChanges = 2;
|
| struct kevent events[kMaxChanges];
|
| - EV_SET(events, di->fd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
|
| + EV_SET(events, sd->fd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
|
| VOID_NO_RETRY_EXPECTED(kevent(kqueue_fd_, events, 1, NULL, 0, NULL));
|
| - EV_SET(events, di->fd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
|
| + EV_SET(events, sd->fd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
|
| VOID_NO_RETRY_EXPECTED(kevent(kqueue_fd_, events, 1, NULL, 0, NULL));
|
| - di->set_tracked_by_kqueue(false);
|
| + sd->set_tracked_by_kqueue(false);
|
| }
|
|
|
|
|
| // Update the kqueue registration for SocketData structure to reflect
|
| // the events currently of interest.
|
| -static void AddToKqueue(intptr_t kqueue_fd_, DescriptorInfo* di) {
|
| - ASSERT(!di->tracked_by_kqueue());
|
| +static void AddToKqueue(intptr_t kqueue_fd_, SocketData* sd) {
|
| + ASSERT(!sd->tracked_by_kqueue());
|
| static const intptr_t kMaxChanges = 2;
|
| intptr_t changes = 0;
|
| struct kevent events[kMaxChanges];
|
| int flags = EV_ADD;
|
| - if (!di->IsListeningSocket()) {
|
| + if (!sd->IsListeningSocket()) {
|
| flags |= EV_CLEAR;
|
| }
|
| // Register or unregister READ filter if needed.
|
| - if (di->HasReadEvent()) {
|
| + if (sd->HasReadEvent()) {
|
| EV_SET(events + changes,
|
| - di->fd(),
|
| + sd->fd(),
|
| EVFILT_READ,
|
| flags,
|
| 0,
|
| 0,
|
| - di);
|
| + sd);
|
| ++changes;
|
| }
|
| // Register or unregister WRITE filter if needed.
|
| - if (di->HasWriteEvent()) {
|
| + if (sd->HasWriteEvent()) {
|
| EV_SET(events + changes,
|
| - di->fd(),
|
| + sd->fd(),
|
| EVFILT_WRITE,
|
| flags,
|
| 0,
|
| 0,
|
| - di);
|
| + sd);
|
| ++changes;
|
| }
|
| ASSERT(changes > 0);
|
| @@ -92,16 +94,13 @@ static void AddToKqueue(intptr_t kqueue_fd_, DescriptorInfo* di) {
|
| int status =
|
| NO_RETRY_EXPECTED(kevent(kqueue_fd_, events, changes, NULL, 0, NULL));
|
| if (status == -1) {
|
| - // TODO(kustermann): Verify that the dart end is handling this correctly &
|
| - // adapt this code to work for multiple listening sockets.
|
| -
|
| // kQueue 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);
|
| } else {
|
| - di->set_tracked_by_kqueue(true);
|
| + sd->set_tracked_by_kqueue(true);
|
| }
|
| }
|
|
|
| @@ -143,26 +142,20 @@ EventHandlerImplementation::~EventHandlerImplementation() {
|
| }
|
|
|
|
|
| -DescriptorInfo* EventHandlerImplementation::GetDescriptorInfo(
|
| - intptr_t fd, bool is_listening) {
|
| +SocketData* EventHandlerImplementation::GetSocketData(intptr_t fd) {
|
| 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.
|
| - if (is_listening) {
|
| - di = new DescriptorInfoMultiple(fd);
|
| - } else {
|
| - di = new DescriptorInfoSingle(fd);
|
| - }
|
| - entry->value = di;
|
| + // new SocketData for the file descriptor is inserted.
|
| + sd = new SocketData(fd);
|
| + entry->value = sd;
|
| }
|
| - ASSERT(fd == di->fd());
|
| - return di;
|
| + ASSERT(fd == sd->fd());
|
| + return sd;
|
| }
|
|
|
|
|
| @@ -198,64 +191,36 @@ void EventHandlerImplementation::HandleInterruptFd() {
|
| } else if (msg[i].id == kShutdownId) {
|
| shutdown_ = true;
|
| } else {
|
| - DescriptorInfo* di = GetDescriptorInfo(
|
| - msg[i].id, (msg[i].data & (1 << kListeningSocket)) != 0);
|
| + SocketData* sd = GetSocketData(msg[i].id);
|
| if (IS_COMMAND(msg[i].data, kShutdownReadCommand)) {
|
| - ASSERT(!di->IsListeningSocket());
|
| // Close the socket for reading.
|
| - VOID_NO_RETRY_EXPECTED(shutdown(di->fd(), SHUT_RD));
|
| + shutdown(sd->fd(), SHUT_RD);
|
| } else if (IS_COMMAND(msg[i].data, kShutdownWriteCommand)) {
|
| - ASSERT(!di->IsListeningSocket());
|
| // Close the socket for writing.
|
| - VOID_NO_RETRY_EXPECTED(shutdown(di->fd(), SHUT_WR));
|
| + 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) {
|
| - RemoveFromKqueue(kqueue_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;
|
| - }
|
| + // Close the socket and free system resources.
|
| + RemoveFromKqueue(kqueue_fd_, sd);
|
| + intptr_t fd = sd->fd();
|
| + VOID_TEMP_FAILURE_RETRY(close(fd));
|
| + 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)) {
|
| - AddToKqueue(kqueue_fd_, di);
|
| + for (int i = 0; i < count; i++) {
|
| + if (sd->ReturnToken()) {
|
| + AddToKqueue(kqueue_fd_, sd);
|
| + }
|
| }
|
| } else {
|
| ASSERT_NO_COMMAND(msg[i].data);
|
| + // Setup events to wait for.
|
| ASSERT((msg[i].data > 0) && (msg[i].data < kIntptrMax));
|
| - 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) {
|
| - AddToKqueue(kqueue_fd_, di);
|
| - } else if (had_listeners && !has_listeners) {
|
| - RemoveFromKqueue(kqueue_fd_, di);
|
| - }
|
| + ASSERT(sd->port() == 0);
|
| + sd->SetPortAndMask(msg[i].dart_port,
|
| + static_cast<intptr_t>(msg[i].data));
|
| + AddToKqueue(kqueue_fd_, sd);
|
| }
|
| }
|
| }
|
| @@ -283,12 +248,12 @@ static void PrintEventMask(intptr_t fd, struct kevent* event) {
|
|
|
|
|
| intptr_t EventHandlerImplementation::GetEvents(struct kevent* event,
|
| - DescriptorInfo* di) {
|
| + SocketData* sd) {
|
| #ifdef DEBUG_KQUEUE
|
| - PrintEventMask(di->fd(), event);
|
| + PrintEventMask(sd->fd(), event);
|
| #endif
|
| intptr_t event_mask = 0;
|
| - if (di->IsListeningSocket()) {
|
| + if (sd->IsListeningSocket()) {
|
| // On a listening socket the READ event means that there are
|
| // connections ready to be accepted.
|
| if (event->filter == EVFILT_READ) {
|
| @@ -344,16 +309,15 @@ void EventHandlerImplementation::HandleEvents(struct kevent* events,
|
| if (events[i].udata == NULL) {
|
| interrupt_seen = true;
|
| } else {
|
| - DescriptorInfo* di =
|
| - reinterpret_cast<DescriptorInfo*>(events[i].udata);
|
| - intptr_t event_mask = GetEvents(events + i, di);
|
| + SocketData* sd = reinterpret_cast<SocketData*>(events[i].udata);
|
| + intptr_t event_mask = GetEvents(events + i, sd);
|
| if (event_mask != 0) {
|
| - Dart_Port port = di->NextPort();
|
| - ASSERT(port != 0);
|
| - if (di->TakeToken()) {
|
| - // Took last token, remove from kqueue.
|
| - RemoveFromKqueue(kqueue_fd_, di);
|
| + if (sd->TakeToken()) {
|
| + // Took last token, remove from epoll.
|
| + RemoveFromKqueue(kqueue_fd_, sd);
|
| }
|
| + Dart_Port port = sd->port();
|
| + ASSERT(port != 0);
|
| DartUtils::PostInt32(port, event_mask);
|
| }
|
| }
|
|
|