| Index: dart/runtime/bin/eventhandler_macos.cc
|
| diff --git a/dart/runtime/bin/eventhandler_macos.cc b/dart/runtime/bin/eventhandler_macos.cc
|
| index 49ec2477e19b132b0033d078f664a68787d0c207..1cdb72203696608fa4dd0682de79b04b153a1c0f 100644
|
| --- a/dart/runtime/bin/eventhandler_macos.cc
|
| +++ b/dart/runtime/bin/eventhandler_macos.cc
|
| @@ -6,6 +6,7 @@
|
| #if defined(TARGET_OS_MACOS)
|
|
|
| #include "bin/eventhandler.h"
|
| +#include "bin/eventhandler_macos.h"
|
|
|
| #include <errno.h> // NOLINT
|
| #include <pthread.h> // NOLINT
|
| @@ -27,50 +28,45 @@
|
| 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 SocketData::HasReadEvent() {
|
| - return (mask_ & (1 << kInEvent)) != 0;
|
| +bool DescriptorInfoMacOS::HasReadEvent() {
|
| + return (Mask() & (1 << kInEvent)) != 0;
|
| }
|
|
|
|
|
| -bool SocketData::HasWriteEvent() {
|
| - return (mask_ & (1 << kOutEvent)) != 0;
|
| +bool DescriptorInfoMacOS::HasWriteEvent() {
|
| + return (Mask() & (1 << kOutEvent)) != 0;
|
| }
|
|
|
|
|
| // Unregister the file descriptor for a SocketData structure with kqueue.
|
| -static void RemoveFromKqueue(intptr_t kqueue_fd_, SocketData* sd) {
|
| - if (!sd->tracked_by_kqueue()) return;
|
| +static void RemoveFromKqueue(intptr_t kqueue_fd_, DescriptorInfoMacOS* si) {
|
| + if (!si->tracked_by_kqueue()) return;
|
| static const intptr_t kMaxChanges = 2;
|
| struct kevent events[kMaxChanges];
|
| - EV_SET(events, sd->fd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
|
| + EV_SET(events, si->fd(), EVFILT_READ, EV_DELETE, 0, 0, NULL);
|
| VOID_NO_RETRY_EXPECTED(kevent(kqueue_fd_, events, 1, NULL, 0, NULL));
|
| - EV_SET(events, sd->fd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
|
| + EV_SET(events, si->fd(), EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
|
| VOID_NO_RETRY_EXPECTED(kevent(kqueue_fd_, events, 1, NULL, 0, NULL));
|
| - sd->set_tracked_by_kqueue(false);
|
| + si->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_, SocketData* sd) {
|
| - ASSERT(!sd->tracked_by_kqueue());
|
| +static void AddToKqueue(intptr_t kqueue_fd_, DescriptorInfoMacOS* si) {
|
| + ASSERT(!si->tracked_by_kqueue());
|
| static const intptr_t kMaxChanges = 2;
|
| intptr_t changes = 0;
|
| struct kevent events[kMaxChanges];
|
| int flags = EV_ADD;
|
| - if (!sd->IsListeningSocket()) {
|
| + if (!si->IsListeningSocket()) {
|
| flags |= EV_CLEAR;
|
| }
|
| // Register or unregister READ filter if needed.
|
| - if (sd->HasReadEvent()) {
|
| + if (si->HasReadEvent()) {
|
| EV_SET(events + changes,
|
| - sd->fd(),
|
| + si->fd(),
|
| EVFILT_READ,
|
| flags,
|
| 0,
|
| @@ -79,9 +75,9 @@ static void AddToKqueue(intptr_t kqueue_fd_, SocketData* sd) {
|
| ++changes;
|
| }
|
| // Register or unregister WRITE filter if needed.
|
| - if (sd->HasWriteEvent()) {
|
| + if (si->HasWriteEvent()) {
|
| EV_SET(events + changes,
|
| - sd->fd(),
|
| + si->fd(),
|
| EVFILT_WRITE,
|
| flags,
|
| 0,
|
| @@ -98,9 +94,9 @@ static void AddToKqueue(intptr_t kqueue_fd_, SocketData* sd) {
|
| // 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);
|
| } else {
|
| - sd->set_tracked_by_kqueue(true);
|
| + si->set_tracked_by_kqueue(true);
|
| }
|
| }
|
|
|
| @@ -142,20 +138,22 @@ EventHandlerImplementation::~EventHandlerImplementation() {
|
| }
|
|
|
|
|
| -SocketData* EventHandlerImplementation::GetSocketData(intptr_t fd) {
|
| +DescriptorInfoMacOS* EventHandlerImplementation::GetDescriptorInfoMacOS(
|
| + intptr_t fd) {
|
| 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) {
|
| + DescriptorInfoMacOS* si =
|
| + reinterpret_cast<DescriptorInfoMacOS*>(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.
|
| - sd = new SocketData(fd);
|
| - entry->value = sd;
|
| + // new DescriptorInfoMacOS for the file descriptor is inserted.
|
| + si = new DescriptorInfoMacOS(fd);
|
| + entry->value = si;
|
| }
|
| - ASSERT(fd == sd->fd());
|
| - return sd;
|
| + ASSERT(fd == si->fd());
|
| + return si;
|
| }
|
|
|
|
|
| @@ -191,36 +189,58 @@ void EventHandlerImplementation::HandleInterruptFd() {
|
| } else if (msg[i].id == kShutdownId) {
|
| shutdown_ = true;
|
| } else {
|
| - SocketData* sd = GetSocketData(msg[i].id);
|
| + DescriptorInfoMacOS* si = GetDescriptorInfoMacOS(msg[i].id);
|
| if (IS_COMMAND(msg[i].data, kShutdownReadCommand)) {
|
| // Close the socket for reading.
|
| - shutdown(sd->fd(), SHUT_RD);
|
| + shutdown(si->fd(), SHUT_RD);
|
| } else if (IS_COMMAND(msg[i].data, kShutdownWriteCommand)) {
|
| // Close the socket for writing.
|
| - shutdown(sd->fd(), SHUT_WR);
|
| + shutdown(si->fd(), SHUT_WR);
|
| } else if (IS_COMMAND(msg[i].data, kCloseCommand)) {
|
| - // 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;
|
| + // Close the socket and free system resources and move on to next
|
| + // message.
|
| + bool no_more_listeners = si->RemovePort(msg[i].dart_port);
|
| + if (no_more_listeners) {
|
| + RemoveFromKqueue(kqueue_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);
|
| - for (int i = 0; i < count; i++) {
|
| - if (sd->ReturnToken()) {
|
| - AddToKqueue(kqueue_fd_, sd);
|
| - }
|
| + if (si->ReturnTokens(msg[i].dart_port, count)) {
|
| + AddToKqueue(kqueue_fd_, si);
|
| }
|
| } else {
|
| ASSERT_NO_COMMAND(msg[i].data);
|
| // Setup events to wait for.
|
| ASSERT((msg[i].data > 0) && (msg[i].data < kIntptrMax));
|
| - ASSERT(sd->port() == 0);
|
| - sd->SetPortAndMask(msg[i].dart_port,
|
| + ASSERT(si->port() == 0);
|
| + si->SetPortAndMask(msg[i].dart_port,
|
| static_cast<intptr_t>(msg[i].data));
|
| - AddToKqueue(kqueue_fd_, sd);
|
| + AddToKqueue(kqueue_fd_, si);
|
| }
|
| }
|
| }
|
| @@ -248,12 +268,12 @@ static void PrintEventMask(intptr_t fd, struct kevent* event) {
|
|
|
|
|
| intptr_t EventHandlerImplementation::GetEvents(struct kevent* event,
|
| - SocketData* sd) {
|
| + DescriptorInfoMacOS* si) {
|
| #ifdef DEBUG_KQUEUE
|
| - PrintEventMask(sd->fd(), event);
|
| + PrintEventMask(si->fd(), event);
|
| #endif
|
| intptr_t event_mask = 0;
|
| - if (sd->IsListeningSocket()) {
|
| + if (si->IsListeningSocket()) {
|
| // On a listening socket the READ event means that there are
|
| // connections ready to be accepted.
|
| if (event->filter == EVFILT_READ) {
|
| @@ -309,15 +329,16 @@ void EventHandlerImplementation::HandleEvents(struct kevent* events,
|
| if (events[i].udata == NULL) {
|
| interrupt_seen = true;
|
| } else {
|
| - SocketData* sd = reinterpret_cast<SocketData*>(events[i].udata);
|
| - intptr_t event_mask = GetEvents(events + i, sd);
|
| + DescriptorInfoMacOS* si =
|
| + reinterpret_cast<DescriptorInfoMacOS*>(events[i].udata);
|
| + intptr_t event_mask = GetEvents(events + i, si);
|
| if (event_mask != 0) {
|
| - if (sd->TakeToken()) {
|
| - // Took last token, remove from epoll.
|
| - RemoveFromKqueue(kqueue_fd_, sd);
|
| - }
|
| - Dart_Port port = sd->port();
|
| + Dart_Port port = si->NextPort();
|
| ASSERT(port != 0);
|
| + if (si->TakeToken()) {
|
| + // Took last token, remove from kqueue.
|
| + RemoveFromKqueue(kqueue_fd_, si);
|
| + }
|
| DartUtils::PostInt32(port, event_mask);
|
| }
|
| }
|
|
|