Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(939)

Unified Diff: device/serial/serial_io_handler_posix.cc

Issue 2057973002: [serial] When a read is requested on posix platforms, try the read immediately. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Removed unnecessary watch cancel. Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « device/serial/serial_io_handler_posix.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: device/serial/serial_io_handler_posix.cc
diff --git a/device/serial/serial_io_handler_posix.cc b/device/serial/serial_io_handler_posix.cc
index 158c374a03b0858cc74f94205704e6682ea87026..3685ec8510d03c96ecde7239fa7c896cad675dd4 100644
--- a/device/serial/serial_io_handler_posix.cc
+++ b/device/serial/serial_io_handler_posix.cc
@@ -124,6 +124,12 @@ void SerialIoHandlerPosix::ReadImpl() {
DCHECK(file().IsValid());
EnsureWatchingReads();
+
+ // Try to read immediately. This is needed because on some platforms
+ // (e.g., OSX) there may not be a notification from the message loop
+ // when the fd is ready to read immediately after it is opened. There
+ // is no danger of blocking because the fd is opened with async flag.
+ AttemptRead(true);
}
void SerialIoHandlerPosix::WriteImpl() {
@@ -294,18 +300,25 @@ void SerialIoHandlerPosix::OnFileCanReadWithoutBlocking(int fd) {
DCHECK(CalledOnValidThread());
DCHECK_EQ(fd, file().GetPlatformFile());
+ AttemptRead(false);
+}
+
+void SerialIoHandlerPosix::AttemptRead(bool within_read) {
if (pending_read_buffer()) {
int bytes_read = HANDLE_EINTR(read(file().GetPlatformFile(),
pending_read_buffer(),
pending_read_buffer_len()));
if (bytes_read < 0) {
- if (errno == ENXIO) {
- ReadCompleted(0, serial::ReceiveError::DEVICE_LOST);
+ if (errno == EAGAIN) {
+ // The fd does not have data to read yet so continue waiting.
+ return;
+ } else if (errno == ENXIO) {
+ RunReadCompleted(within_read, 0, serial::ReceiveError::DEVICE_LOST);
} else {
- ReadCompleted(0, serial::ReceiveError::SYSTEM_ERROR);
+ RunReadCompleted(within_read, 0, serial::ReceiveError::SYSTEM_ERROR);
}
} else if (bytes_read == 0) {
- ReadCompleted(0, serial::ReceiveError::DEVICE_LOST);
+ RunReadCompleted(within_read, 0, serial::ReceiveError::DEVICE_LOST);
} else {
bool break_detected = false;
bool parity_error_detected = false;
@@ -314,11 +327,14 @@ void SerialIoHandlerPosix::OnFileCanReadWithoutBlocking(int fd) {
bytes_read, break_detected, parity_error_detected);
if (break_detected) {
- ReadCompleted(new_bytes_read, serial::ReceiveError::BREAK);
+ RunReadCompleted(within_read, new_bytes_read,
+ serial::ReceiveError::BREAK);
} else if (parity_error_detected) {
- ReadCompleted(new_bytes_read, serial::ReceiveError::PARITY_ERROR);
+ RunReadCompleted(within_read, new_bytes_read,
+ serial::ReceiveError::PARITY_ERROR);
} else {
- ReadCompleted(new_bytes_read, serial::ReceiveError::NONE);
+ RunReadCompleted(within_read, new_bytes_read,
+ serial::ReceiveError::NONE);
}
}
} else {
@@ -329,6 +345,15 @@ void SerialIoHandlerPosix::OnFileCanReadWithoutBlocking(int fd) {
}
}
+void SerialIoHandlerPosix::RunReadCompleted(bool within_read,
+ int bytes_read,
+ serial::ReceiveError error) {
+ if (within_read)
+ QueueReadCompleted(bytes_read, error);
+ else
+ ReadCompleted(bytes_read, error);
+}
+
void SerialIoHandlerPosix::OnFileCanWriteWithoutBlocking(int fd) {
DCHECK(CalledOnValidThread());
DCHECK_EQ(fd, file().GetPlatformFile());
« no previous file with comments | « device/serial/serial_io_handler_posix.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698