Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
jellefoks committed Nov 14, 2024
1 parent 3567d18 commit e4093f8
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 36 deletions.
24 changes: 13 additions & 11 deletions base/message_loop/message_pump_io_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,26 @@ MessagePumpIOStarboard::SocketWatcher::~SocketWatcher() {
}
}

bool MessagePumpIOStarboard::SocketWatcher::StopWatchingSocket() {
watcher_ = nullptr;
interests_ = kSbSocketWaiterInterestNone;

SbSocket socket = Release();
bool MessagePumpIOStarboard::SocketWatcher::UnregisterInterest(int interests) {
bool result = true;
if (SbSocketIsValid(socket)) {
DCHECK(pump_);
if (SbSocketIsValid(socket_)) {
// This may get called multiple times from TCPSocketStarboard.
if (pump_) {
result = pump_->UnregisterInterest(
socket, kSbSocketWaiterInterestRead || kSbSocketWaiterInterestWrite,
this);
result = pump_->UnregisterInterest(socket_, interests, this);
}
}
pump_ = nullptr;
if (!interests_) {
pump_ = nullptr;
watcher_ = nullptr;
}
return result;
}

bool MessagePumpIOStarboard::SocketWatcher::StopWatchingSocket() {
return UnregisterInterest(kSbSocketWaiterInterestRead ||
kSbSocketWaiterInterestWrite);
}

void MessagePumpIOStarboard::SocketWatcher::Init(SbSocket socket,
bool persistent) {
DCHECK(socket);
Expand Down Expand Up @@ -137,6 +138,7 @@ bool MessagePumpIOStarboard::UnregisterInterest(SbSocket socket,
} else {
interests = kSbSocketWaiterInterestNone;
}
controller->set_interests(interests);

if (!SbSocketIsValid(socket)) {
NOTREACHED() << "Invalid socket" << socket;
Expand Down
4 changes: 4 additions & 0 deletions base/message_loop/message_pump_io_starboard.h
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ class BASE_EXPORT MessagePumpIOStarboard : public MessagePump {
// NOTE: These methods aren't called StartWatching()/StopWatching() to avoid
// confusion with the win32 ObjectWatcher class.

// Unregisters the interests for watching the socket, always safe to call.
// No-op if there's nothing to do.
bool UnregisterInterest(int interests);

// Stops watching the socket, always safe to call. No-op if there's nothing
// to do.
bool StopWatchingSocket();
Expand Down
29 changes: 20 additions & 9 deletions base/message_loop/message_pump_io_starboard_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,21 @@ class MessagePumpIOStarboardTest : public testing::Test {
}

void SimulateIOEvent(MessagePumpIOStarboard::SocketWatcher* controller) {
MessagePumpIOStarboard::OnSocketWaiterNotification(nullptr,
nullptr,
controller,
(kSbSocketWaiterInterestRead | kSbSocketWaiterInterestWrite));
MessagePumpIOStarboard::OnSocketWaiterNotification(
nullptr, nullptr, controller,
(kSbSocketWaiterInterestRead | kSbSocketWaiterInterestWrite));
}

void SimulateIOReadEvent(MessagePumpIOStarboard::SocketWatcher* controller) {
MessagePumpIOStarboard::OnSocketWaiterNotification(
nullptr, nullptr, controller,
kSbSocketWaiterInterestRead);
}

void SimulateIOWriteEvent(MessagePumpIOStarboard::SocketWatcher* controller) {
MessagePumpIOStarboard::OnSocketWaiterNotification(
nullptr, nullptr, controller,
kSbSocketWaiterInterestWrite);
}

std::unique_ptr<test::SingleThreadTaskEnvironment> task_environment_;
Expand All @@ -95,7 +106,7 @@ class StupidWatcher : public MessagePumpIOStarboard::Watcher {
};

// Death tests not supported.
TEST_F(MessagePumpIOStarboardTest, DISABLED_QuitOutsideOfRun) {
TEST_F(MessagePumpIOStarboardTest, QuitOutsideOfRun) {
std::unique_ptr<MessagePumpIOStarboard> pump = CreateMessagePump();
ASSERT_DCHECK_DEATH(pump->Quit());
}
Expand Down Expand Up @@ -132,7 +143,7 @@ class DeleteWatcher : public BaseWatcher {
};

// Fails on some platforms.
TEST_F(MessagePumpIOStarboardTest, DISABLED_DeleteWatcher) {
TEST_F(MessagePumpIOStarboardTest, DeleteWatcher) {
DeleteWatcher delegate(
std::make_unique<MessagePumpIOStarboard::SocketWatcher>(FROM_HERE));
std::unique_ptr<MessagePumpIOStarboard> pump = CreateMessagePump();
Expand All @@ -159,7 +170,7 @@ class StopWatcher : public BaseWatcher {
};

// Fails on some platforms.
TEST_F(MessagePumpIOStarboardTest, DISABLED_StopWatcher) {
TEST_F(MessagePumpIOStarboardTest, StopWatcher) {
std::unique_ptr<MessagePumpIOStarboard> pump = CreateMessagePump();
MessagePumpIOStarboard::SocketWatcher controller(FROM_HERE);
StopWatcher delegate(&controller);
Expand Down Expand Up @@ -195,7 +206,7 @@ class NestedPumpWatcher : public MessagePumpIOStarboard::Watcher {
};

// Fails on some platforms.
TEST_F(MessagePumpIOStarboardTest, DISABLED_NestedPumpWatcher) {
TEST_F(MessagePumpIOStarboardTest, NestedPumpWatcher) {
NestedPumpWatcher delegate;
std::unique_ptr<MessagePumpIOStarboard> pump = CreateMessagePump();
MessagePumpIOStarboard::SocketWatcher controller(FROM_HERE);
Expand Down Expand Up @@ -233,7 +244,7 @@ void WriteSocketWrapper(MessagePumpIOStarboard* pump,
}

// Fails on some platforms.
TEST_F(MessagePumpIOStarboardTest, DISABLED_QuitWatcher) {
TEST_F(MessagePumpIOStarboardTest, QuitWatcher) {
// Delete the old TaskEnvironment so that we can manage our own one here.
task_environment_.reset();

Expand Down
14 changes: 5 additions & 9 deletions net/socket/tcp_socket_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -252,9 +252,8 @@ void TCPSocketStarboard::Close() {
}

void TCPSocketStarboard::StopWatchingAndCleanUp() {
bool ok = base::CurrentIOThread::Get()->UnregisterInterest(
socket_, kSbSocketWaiterInterestRead | kSbSocketWaiterInterestWrite,
&socket_watcher_);
bool ok = socket_watcher_.UnregisterInterest(kSbSocketWaiterInterestRead |
kSbSocketWaiterInterestWrite);
DCHECK(ok);

if (!accept_callback_.is_null()) {
Expand Down Expand Up @@ -470,8 +469,7 @@ int TCPSocketStarboard::ReadIfReady(IOBuffer* buf,
int TCPSocketStarboard::CancelReadIfReady() {
DCHECK(read_if_ready_callback_);

bool ok = base::CurrentIOThread::Get()->UnregisterInterest(
socket_, kSbSocketWaiterInterestRead, &socket_watcher_);
bool ok = socket_watcher_.UnregisterInterest(kSbSocketWaiterInterestRead);
DCHECK(ok);

read_if_ready_callback_.Reset();
Expand Down Expand Up @@ -669,16 +667,14 @@ void TCPSocketStarboard::ApplySocketTag(const SocketTag& tag) {

void TCPSocketStarboard::ClearReadWatcherIfOperationsNotPending() {
if (!read_pending() && !accept_pending() && !connect_pending()) {
bool ok = base::CurrentIOThread::Get()->UnregisterInterest(
socket_, kSbSocketWaiterInterestRead, &socket_watcher_);
bool ok = socket_watcher_.UnregisterInterest(kSbSocketWaiterInterestRead);
DCHECK(ok);
}
}

void TCPSocketStarboard::ClearWriteWatcherIfOperationsNotPending() {
if (!write_pending()) {
bool ok = base::CurrentIOThread::Get()->UnregisterInterest(
socket_, kSbSocketWaiterInterestWrite, &socket_watcher_);
bool ok = socket_watcher_.UnregisterInterest(kSbSocketWaiterInterestWrite);
DCHECK(ok);
}
}
Expand Down
11 changes: 4 additions & 7 deletions net/socket/udp_socket_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,8 @@ void UDPSocketStarboard::Close() {
write_callback_.Reset();
send_to_address_.reset();

bool ok = base::CurrentIOThread::Get()->UnregisterInterest(
socket_, kSbSocketWaiterInterestRead | kSbSocketWaiterInterestWrite,
&socket_watcher_);
bool ok = socket_watcher_.UnregisterInterest(kSbSocketWaiterInterestRead |
kSbSocketWaiterInterestWrite);
DCHECK(ok);

is_connected_ = false;
Expand Down Expand Up @@ -1018,16 +1017,14 @@ void UDPSocketStarboard::StopWatchingSocketForWriting() {
return;
write_async_watcher_->set_watching(false);
if (!write_buf_) {
bool ok = base::CurrentIOThread::Get()->UnregisterInterest(
socket_, kSbSocketWaiterInterestWrite, &socket_watcher_);
bool ok = socket_watcher_.UnregisterInterest(kSbSocketWaiterInterestWrite);
DCHECK(ok);
}
}

void UDPSocketStarboard::StopWatchingSocketForReading() {
if (!read_buf_) {
bool ok = base::CurrentIOThread::Get()->UnregisterInterest(
socket_, kSbSocketWaiterInterestRead, &socket_watcher_);
bool ok = socket_watcher_.UnregisterInterest(kSbSocketWaiterInterestRead);
DCHECK(ok);
}
}
Expand Down

0 comments on commit e4093f8

Please sign in to comment.