Skip to content

Commit

Permalink
Use tcp_socket_posix to replace tcp_socket_starboard
Browse files Browse the repository at this point in the history
Compile verified for linux/evergeen/win32/xb1

b/302741384
  • Loading branch information
maxz-lab committed Aug 15, 2024
1 parent d4f1e95 commit beebae5
Show file tree
Hide file tree
Showing 59 changed files with 777 additions and 208 deletions.
8 changes: 4 additions & 4 deletions base/files/file_descriptor_watcher_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class FileDescriptorWatcher::Controller::Watcher
friend class FileDescriptorWatcher;

// MessagePumpForIO::FdWatcher:
void OnFileCanReadWithoutBlocking(int fd) override;
void OnFileCanWriteWithoutBlocking(int fd) override;
void OnSocketReadyToRead(int fd) override;
void OnSocketReadyToWrite(int fd) override;

// CurrentThread::DestructionObserver:
void WillDestroyCurrentMessageLoop() override;
Expand Down Expand Up @@ -122,7 +122,7 @@ void FileDescriptorWatcher::Controller::Watcher::StartWatching() {
}
}

void FileDescriptorWatcher::Controller::Watcher::OnFileCanReadWithoutBlocking(
void FileDescriptorWatcher::Controller::Watcher::OnSocketReadyToRead(
int fd) {
DCHECK_EQ(fd_, fd);
DCHECK_EQ(MessagePumpForIO::WATCH_READ, mode_);
Expand All @@ -133,7 +133,7 @@ void FileDescriptorWatcher::Controller::Watcher::OnFileCanReadWithoutBlocking(
FROM_HERE, BindOnce(&Controller::RunCallback, controller_));
}

void FileDescriptorWatcher::Controller::Watcher::OnFileCanWriteWithoutBlocking(
void FileDescriptorWatcher::Controller::Watcher::OnSocketReadyToWrite(
int fd) {
DCHECK_EQ(fd_, fd);
DCHECK_EQ(MessagePumpForIO::WATCH_WRITE, mode_);
Expand Down
11 changes: 11 additions & 0 deletions base/files/file_util_starboard.cc
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,17 @@ FilePath MakeAbsoluteFilePath(const FilePath& input) {
return input;
}

bool SetNonBlocking(int fd) {
const int flags = fcntl(fd, F_GETFL);
if (flags == -1)
return false;
if (flags & O_NONBLOCK)
return true;
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) == -1)
return false;
return true;
}

namespace internal {

bool MoveUnsafe(const FilePath& from_path, const FilePath& to_path) {
Expand Down
38 changes: 19 additions & 19 deletions base/logging.cc
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ typedef HANDLE FileHandle;
#include <mach/mach_time.h>
#include <os/log.h>

#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
#if BUILDFLAG(IS_NACL)
#include <sys/time.h> // timespec doesn't seem to be in <time.h>
#endif
Expand All @@ -84,7 +84,7 @@ typedef HANDLE FileHandle;
#include <android/log.h>
#endif

#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
#include <errno.h>
#include <paths.h>
#include <stdio.h>
Expand Down Expand Up @@ -130,7 +130,7 @@ typedef FILE* FileHandle;
#include "base/win/win_util.h"
#endif

#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
#include "base/posix/safe_strerror.h"
#endif

Expand Down Expand Up @@ -293,7 +293,7 @@ base::stack<LogAssertHandlerFunction>& GetLogAssertHandlerStack() {
LogMessageHandlerFunction g_log_message_handler = nullptr;

uint64_t TickCount() {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
return starboard::CurrentMonotonicTime();
#elif BUILDFLAG(IS_WIN)
return GetTickCount();
Expand All @@ -307,7 +307,7 @@ uint64_t TickCount() {
// NaCl sadly does not have _POSIX_TIMERS enabled in sys/features.h
// So we have to use clock() for now.
return clock();
#elif BUILDFLAG(IS_POSIX)
#elif BUILDFLAG(IS_POSIX) || SB_API_VERSION >= 16
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);

Expand All @@ -319,21 +319,21 @@ uint64_t TickCount() {
}

void DeleteFilePath(const PathString& log_name) {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
unlink(log_name.c_str());
#elif BUILDFLAG(IS_WIN)
DeleteFile(log_name.c_str());
#elif BUILDFLAG(IS_NACL)
// Do nothing; unlink() isn't supported on NaCl.
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
unlink(log_name.c_str());
#else
#error Unsupported platform
#endif
}

PathString GetDefaultLogFile() {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
// On Starboard, we politely ask for the log directory, like a civilized
// platform.
std::vector<char> path(kSbFileMaxPath + 1);
Expand All @@ -352,15 +352,15 @@ PathString GetDefaultLogFile() {
log_name.erase(last_backslash + 1);
log_name += FILE_PATH_LITERAL("debug.log");
return log_name;
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
// On other platforms we just use the current directory.
return PathString("debug.log");
#endif
}

// We don't need locks on Windows for atomically appending to files. The OS
// provides this functionality.
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16

// Provides a lock to synchronize appending to the log file across
// threads. This can be required to support NFS file systems even on OSes that
Expand All @@ -378,7 +378,7 @@ base::Lock& GetLoggingLock() {
return *lock;
}

#endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#endif // BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16

// Called by logging functions to ensure that |g_log_file| is initialized
// and can be used for writing. Returns false if the file could not be
Expand All @@ -393,7 +393,7 @@ bool InitializeLogFileHandle() {
g_log_file_name = new PathString(GetDefaultLogFile());
}

#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
// This seems to get called a lot with an empty filename, at least in
// base_unittests.
if (g_log_file_name->empty()) {
Expand Down Expand Up @@ -508,7 +508,7 @@ SbLogPriority LogLevelToStarboardLogPriority(int level) {
return kSbLogPriorityInfo;
}
}
#endif // defined(STARBOARD)
#endif // defined(STARBOARD) && SB_API_VERSION <= 15

#if BUILDFLAG(IS_FUCHSIA)
inline FuchsiaLogSeverity LogSeverityToFuchsiaLogSeverity(
Expand Down Expand Up @@ -651,7 +651,7 @@ bool BaseInitLoggingImpl(const LoggingSettings& settings) {
if ((g_logging_destination & LOG_TO_FILE) == 0)
return true;

#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
base::AutoLock guard(GetLoggingLock());
#endif

Expand Down Expand Up @@ -1002,7 +1002,7 @@ LogMessage::~LogMessage() {
}

if ((g_logging_destination & LOG_TO_FILE) != 0) {
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
// If the client app did not call InitLogging() and the lock has not
// been created it will be done now on calling GetLoggingLock(). We do this
// on demand, but if two threads try to do this at the same time, there will
Expand Down Expand Up @@ -1181,11 +1181,11 @@ typedef DWORD SystemErrorCode;
#endif

SystemErrorCode GetLastSystemErrorCode() {
#if defined(STARBOARD)
#if defined(STARBOARD) && SB_API_VERSION <= 15
return SbSystemGetLastError();
#elif BUILDFLAG(IS_WIN)
return ::GetLastError();
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#elif BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
return errno;
#endif
}
Expand Down Expand Up @@ -1267,7 +1267,7 @@ ErrnoLogMessage::~ErrnoLogMessage() {
#endif // BUILDFLAG(IS_WIN)

void CloseLogFile() {
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA)
#if BUILDFLAG(IS_POSIX) || BUILDFLAG(IS_FUCHSIA) || SB_API_VERSION >= 16
base::AutoLock guard(GetLoggingLock());
#endif
CloseLogFileUnlocked();
Expand Down Expand Up @@ -1424,7 +1424,7 @@ void ScopedVmoduleSwitches::InitWithSwitches(
CHECK(!scoped_vlog_info_);
{
#if (defined(LEAK_SANITIZER) && !BUILDFLAG(IS_NACL)) || \
(defined(STARBOARD) && defined(ADDRESS_SANITIZER))
(defined(STARBOARD) && (SB_API_VERSION <= 15) && defined(ADDRESS_SANITIZER))
// See comments on |g_vlog_info|.
ScopedLeakSanitizerDisabler lsan_disabler;
#endif // defined(LEAK_SANITIZER)
Expand Down
12 changes: 6 additions & 6 deletions base/message_loop/fd_watch_controller_posix_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ class FdWatchControllerPosixTest : public testing::Test {

class TestHandler : public MessagePumpForIO::FdWatcher {
public:
void OnFileCanReadWithoutBlocking(int fd) override {
void OnSocketReadyToRead(int fd) override {
watcher_to_delete_ = nullptr;
is_readable_ = true;
RunLoop::QuitCurrentWhenIdleDeprecated();
}
void OnFileCanWriteWithoutBlocking(int fd) override {
void OnSocketReadyToWrite(int fd) override {
watcher_to_delete_ = nullptr;
is_writable_ = true;
RunLoop::QuitCurrentWhenIdleDeprecated();
Expand Down Expand Up @@ -102,7 +102,7 @@ class CallClosureHandler : public MessagePumpForIO::FdWatcher {
}

// base::WatchableIOMessagePumpPosix::FdWatcher:
void OnFileCanReadWithoutBlocking(int fd) override {
void OnSocketReadyToRead(int fd) override {
// Empty the pipe buffer to reset the event. Otherwise libevent
// implementation of MessageLoop may call the event handler again even if
// |read_closure_| below quits the RunLoop.
Expand All @@ -118,7 +118,7 @@ class CallClosureHandler : public MessagePumpForIO::FdWatcher {
std::move(read_closure_).Run();
}

void OnFileCanWriteWithoutBlocking(int fd) override {
void OnSocketReadyToWrite(int fd) override {
ASSERT_FALSE(write_closure_.is_null());
std::move(write_closure_).Run();
}
Expand Down Expand Up @@ -209,7 +209,7 @@ class ReaderWriterHandler : public MessagePumpForIO::FdWatcher {
ReaderWriterHandler& operator=(const ReaderWriterHandler&) = delete;

// base::WatchableIOMessagePumpPosix::FdWatcher:
void OnFileCanReadWithoutBlocking(int fd) override {
void OnSocketReadyToRead(int fd) override {
if (when_ == kOnReadEvent) {
DoAction();
} else {
Expand All @@ -218,7 +218,7 @@ class ReaderWriterHandler : public MessagePumpForIO::FdWatcher {
}
}

void OnFileCanWriteWithoutBlocking(int fd) override {
void OnSocketReadyToWrite(int fd) override {
if (when_ == kOnWriteEvent) {
DoAction();
} else {
Expand Down
4 changes: 2 additions & 2 deletions base/message_loop/message_pump_fuchsia.cc
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,9 @@ void MessagePumpFuchsia::FdWatchController::OnZxHandleSignalled(
// work that would touch |this|.
bool* was_stopped = was_stopped_;
if (filtered_events & FDIO_EVT_WRITABLE)
watcher_->OnFileCanWriteWithoutBlocking(fd_);
watcher_->OnSocketReadyToWrite(fd_);
if (!*was_stopped && (filtered_events & FDIO_EVT_READABLE))
watcher_->OnFileCanReadWithoutBlocking(fd_);
watcher_->OnSocketReadyToRead(fd_);

// Don't add additional work here without checking |*was_stopped_| again.
}
Expand Down
4 changes: 2 additions & 2 deletions base/message_loop/message_pump_glib.cc
Original file line number Diff line number Diff line change
Expand Up @@ -488,14 +488,14 @@ void MessagePumpGlib::FdWatchController::NotifyCanRead() {
if (!watcher_)
return;
DCHECK(poll_fd_);
watcher_->OnFileCanReadWithoutBlocking(poll_fd_->fd);
watcher_->OnSocketReadyToRead(poll_fd_->fd);
}

void MessagePumpGlib::FdWatchController::NotifyCanWrite() {
if (!watcher_)
return;
DCHECK(poll_fd_);
watcher_->OnFileCanWriteWithoutBlocking(poll_fd_->fd);
watcher_->OnSocketReadyToWrite(poll_fd_->fd);
}

bool MessagePumpGlib::WatchFileDescriptor(int fd,
Expand Down
22 changes: 11 additions & 11 deletions base/message_loop/message_pump_glib_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -658,8 +658,8 @@ class BaseWatcher : public MessagePumpGlib::FdWatcher {
~BaseWatcher() override = default;

// base:MessagePumpGlib::FdWatcher interface
void OnFileCanReadWithoutBlocking(int /* fd */) override { NOTREACHED(); }
void OnFileCanWriteWithoutBlocking(int /* fd */) override { NOTREACHED(); }
void OnSocketReadyToRead(int /* fd */) override { NOTREACHED(); }
void OnSocketReadyToWrite(int /* fd */) override { NOTREACHED(); }

protected:
raw_ptr<MessagePumpGlib::FdWatchController> controller_;
Expand All @@ -676,7 +676,7 @@ class DeleteWatcher : public BaseWatcher {

bool HasController() const { return !!controller_; }

void OnFileCanWriteWithoutBlocking(int /* fd */) override {
void OnSocketReadyToWrite(int /* fd */) override {
ClearController();
}

Expand All @@ -698,7 +698,7 @@ class StopWatcher : public BaseWatcher {

~StopWatcher() override = default;

void OnFileCanWriteWithoutBlocking(int /* fd */) override {
void OnSocketReadyToWrite(int /* fd */) override {
controller_->StopWatchingFileDescriptor();
}
};
Expand All @@ -717,14 +717,14 @@ class NestedPumpWatcher : public MessagePumpGlib::FdWatcher {
NestedPumpWatcher() = default;
~NestedPumpWatcher() override = default;

void OnFileCanReadWithoutBlocking(int /* fd */) override {
void OnSocketReadyToRead(int /* fd */) override {
RunLoop runloop;
SingleThreadTaskRunner::GetCurrentDefault()->PostTask(
FROM_HERE, BindOnce(&QuitMessageLoopAndStart, runloop.QuitClosure()));
runloop.Run();
}

void OnFileCanWriteWithoutBlocking(int /* fd */) override {}
void OnSocketReadyToWrite(int /* fd */) override {}
};

class QuitWatcher : public DeleteWatcher {
Expand All @@ -734,7 +734,7 @@ class QuitWatcher : public DeleteWatcher {
: DeleteWatcher(std::move(controller)),
quit_closure_(std::move(quit_closure)) {}

void OnFileCanReadWithoutBlocking(int fd) override {
void OnSocketReadyToRead(int fd) override {
ClearController();
if (quit_closure_)
std::move(quit_closure_).Run();
Expand All @@ -753,9 +753,9 @@ void WriteFDWrapper(const int fd,

} // namespace

// Tests that MessagePumpGlib::FdWatcher::OnFileCanReadWithoutBlocking is not
// Tests that MessagePumpGlib::FdWatcher::OnSocketReadyToRead is not
// called for a READ_WRITE event, and that the controller is destroyed in
// OnFileCanWriteWithoutBlocking callback.
// OnSocketReadyToWrite callback.
TEST_F(MessagePumpGLibFdWatchTest, DeleteWatcher) {
auto pump = std::make_unique<MessagePumpGlib>();
auto controller_ptr =
Expand All @@ -771,9 +771,9 @@ TEST_F(MessagePumpGLibFdWatchTest, DeleteWatcher) {
EXPECT_FALSE(watcher.HasController());
}

// Tests that MessagePumpGlib::FdWatcher::OnFileCanReadWithoutBlocking is not
// Tests that MessagePumpGlib::FdWatcher::OnSocketReadyToRead is not
// called for a READ_WRITE event, when the watcher calls
// StopWatchingFileDescriptor in OnFileCanWriteWithoutBlocking callback.
// StopWatchingFileDescriptor in OnSocketReadyToWrite callback.
TEST_F(MessagePumpGLibFdWatchTest, StopWatcher) {
std::unique_ptr<MessagePumpGlib> pump(new MessagePumpGlib);
MessagePumpGlib::FdWatchController controller(FROM_HERE);
Expand Down
Loading

0 comments on commit beebae5

Please sign in to comment.