summaryrefslogtreecommitdiffstats
path: root/base
diff options
context:
space:
mode:
authorpaivanof@gmail.com <paivanof@gmail.com@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-06 00:29:51 +0000
committerpaivanof@gmail.com <paivanof@gmail.com@0039d316-1c4b-4281-b951-d872f2087c98>2012-11-06 00:29:51 +0000
commitaab1b9ead19f21af4f752c4a52beed65009d96fb (patch)
tree3679bc3c3bd80678a9fb2ab383aa6834679506d0 /base
parent25524c044d4aefd6902d4245b66980c46669e145 (diff)
downloadchromium_src-aab1b9ead19f21af4f752c4a52beed65009d96fb.zip
chromium_src-aab1b9ead19f21af4f752c4a52beed65009d96fb.tar.gz
chromium_src-aab1b9ead19f21af4f752c4a52beed65009d96fb.tar.bz2
net: Implement canceling of all async operations in FileStream.
Canceling of async operations allows to not wait for their completion in FileStream's destructor. Other related changes include: - Got rid of FileStream::Close() and FileStream::CloseSync() methods because reuse of FileStream object doesn't make much sense, it should be destroyed instead. - Changed FileStream to always acquire ownership of the PlatformFile it was given. Fixed usages of FileStream where no ownership was assumed, introduced new helper functions in base/platform_file.h on the way. - FileStream's destructor now always closes the file. If file was opened with PLATFORM_FILE_ASYNC then actual closing is done asynchronously, destructor doesn't wait for that and returns immediately. When file was opened without PLATFORM_FILE_ASYNC closing is done synchronously and the thread doing that should be allowed to do IO operations. - Implementation of FileStream is refactored. FileStream is now just a wrapper around internal object that does all actual work and that can be easily orphaned in the destructor to not block on the actual file descriptor closing. All platform-independent code is extracted into a special file and amount of platform-dependent code is minimized. BUG=115067, 112474 TEST=net_unittests Review URL: https://chromiumcodereview.appspot.com/10701050 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@166091 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r--base/platform_file.h23
-rw-r--r--base/platform_file_posix.cc33
-rw-r--r--base/platform_file_win.cc25
3 files changed, 81 insertions, 0 deletions
diff --git a/base/platform_file.h b/base/platform_file.h
index 1a71359..bcf1915 100644
--- a/base/platform_file.h
+++ b/base/platform_file.h
@@ -79,6 +79,13 @@ enum PlatformFileError {
PLATFORM_FILE_ERROR_INVALID_URL = -15,
};
+// This explicit mapping matches both FILE_ on Windows and SEEK_ on Linux.
+enum PlatformFileWhence {
+ PLATFORM_FILE_FROM_BEGIN = 0,
+ PLATFORM_FILE_FROM_CURRENT = 1,
+ PLATFORM_FILE_FROM_END = 2
+};
+
// Used to hold information about a given file.
// If you add more fields to this structure (platform-specific fields are OK),
// make sure to update all functions that use it in file_util_{win|posix}.cc
@@ -119,6 +126,13 @@ BASE_EXPORT PlatformFile CreatePlatformFile(const FilePath& name,
// Closes a file handle. Returns |true| on success and |false| otherwise.
BASE_EXPORT bool ClosePlatformFile(PlatformFile file);
+// Changes current position in the file to an |offset| relative to an origin
+// defined by |whence|. Returns the resultant current position in the file
+// (relative to the start) or -1 in case of error.
+BASE_EXPORT int64 SeekPlatformFile(PlatformFile file,
+ PlatformFileWhence whence,
+ int64 offset);
+
// Reads the given number of bytes (or until EOF is reached) starting with the
// given offset. Returns the number of bytes read, or -1 on error. Note that
// this function makes a best effort to read all data on all platforms, so it is
@@ -137,6 +151,10 @@ BASE_EXPORT int ReadPlatformFileAtCurrentPos(PlatformFile file,
BASE_EXPORT int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset,
char* data, int size);
+// Same as above but without seek.
+BASE_EXPORT int ReadPlatformFileCurPosNoBestEffort(PlatformFile file,
+ char* data, int size);
+
// Writes the given buffer into the file at the given offset, overwritting any
// data that was previously there. Returns the number of bytes written, or -1
// on error. Note that this function makes a best effort to write all data on
@@ -148,6 +166,11 @@ BASE_EXPORT int WritePlatformFile(PlatformFile file, int64 offset,
BASE_EXPORT int WritePlatformFileAtCurrentPos(PlatformFile file,
const char* data, int size);
+// Save as above but does not make any effort to write all data on all
+// platforms. Returns the number of bytes written, or -1 on error.
+BASE_EXPORT int WritePlatformFileCurPosNoBestEffort(PlatformFile file,
+ const char* data, int size);
+
// Truncates the given file to the given length. If |length| is greater than
// the current size of the file, the file is extended with zeros. If the file
// doesn't exist, |false| is returned.
diff --git a/base/platform_file_posix.cc b/base/platform_file_posix.cc
index 112b6a1..83cf622 100644
--- a/base/platform_file_posix.cc
+++ b/base/platform_file_posix.cc
@@ -21,6 +21,11 @@
namespace base {
+// Make sure our Whence mappings match the system headers.
+COMPILE_ASSERT(PLATFORM_FILE_FROM_BEGIN == SEEK_SET &&
+ PLATFORM_FILE_FROM_CURRENT == SEEK_CUR &&
+ PLATFORM_FILE_FROM_END == SEEK_END, whence_matches_system);
+
#if defined(OS_BSD) || defined(OS_MACOSX)
typedef struct stat stat_wrapper_t;
static int CallFstat(int fd, stat_wrapper_t *sb) {
@@ -158,6 +163,16 @@ bool ClosePlatformFile(PlatformFile file) {
return !HANDLE_EINTR(close(file));
}
+int64 SeekPlatformFile(PlatformFile file,
+ PlatformFileWhence whence,
+ int64 offset) {
+ base::ThreadRestrictions::AssertIOAllowed();
+ if (file < 0 || offset < 0)
+ return -1;
+
+ return lseek(file, static_cast<off_t>(offset), static_cast<int>(whence));
+}
+
int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) {
base::ThreadRestrictions::AssertIOAllowed();
if (file < 0 || size < 0)
@@ -204,6 +219,15 @@ int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset,
return HANDLE_EINTR(pread(file, data, size, offset));
}
+int ReadPlatformFileCurPosNoBestEffort(PlatformFile file,
+ char* data, int size) {
+ base::ThreadRestrictions::AssertIOAllowed();
+ if (file < 0 || size < 0)
+ return -1;
+
+ return HANDLE_EINTR(read(file, data, size));
+}
+
int WritePlatformFile(PlatformFile file, int64 offset,
const char* data, int size) {
base::ThreadRestrictions::AssertIOAllowed();
@@ -243,6 +267,15 @@ int WritePlatformFileAtCurrentPos(PlatformFile file,
return bytes_written ? bytes_written : rv;
}
+int WritePlatformFileCurPosNoBestEffort(PlatformFile file,
+ const char* data, int size) {
+ base::ThreadRestrictions::AssertIOAllowed();
+ if (file < 0 || size < 0)
+ return -1;
+
+ return HANDLE_EINTR(write(file, data, size));
+}
+
bool TruncatePlatformFile(PlatformFile file, int64 length) {
base::ThreadRestrictions::AssertIOAllowed();
return ((file >= 0) && !HANDLE_EINTR(ftruncate(file, length)));
diff --git a/base/platform_file_win.cc b/base/platform_file_win.cc
index b7861e6..92f1c1f 100644
--- a/base/platform_file_win.cc
+++ b/base/platform_file_win.cc
@@ -115,6 +115,21 @@ bool ClosePlatformFile(PlatformFile file) {
return (CloseHandle(file) != 0);
}
+int64 SeekPlatformFile(PlatformFile file,
+ PlatformFileWhence whence,
+ int64 offset) {
+ base::ThreadRestrictions::AssertIOAllowed();
+ if (file < 0 || offset < 0)
+ return -1;
+
+ LARGE_INTEGER distance, res;
+ distance.QuadPart = offset;
+ DWORD move_method = static_cast<DWORD>(whence);
+ if (!SetFilePointerEx(file, distance, &res, move_method))
+ return -1;
+ return res.QuadPart;
+}
+
int ReadPlatformFile(PlatformFile file, int64 offset, char* data, int size) {
base::ThreadRestrictions::AssertIOAllowed();
if (file == kInvalidPlatformFileValue)
@@ -145,6 +160,11 @@ int ReadPlatformFileNoBestEffort(PlatformFile file, int64 offset, char* data,
return ReadPlatformFile(file, offset, data, size);
}
+int ReadPlatformFileCurPosNoBestEffort(PlatformFile file,
+ char* data, int size) {
+ return ReadPlatformFile(file, 0, data, size);
+}
+
int WritePlatformFile(PlatformFile file, int64 offset,
const char* data, int size) {
base::ThreadRestrictions::AssertIOAllowed();
@@ -170,6 +190,11 @@ int WritePlatformFileAtCurrentPos(PlatformFile file, const char* data,
return WritePlatformFile(file, 0, data, size);
}
+int WritePlatformFileCurPosNoBestEffort(PlatformFile file,
+ const char* data, int size) {
+ return WritePlatformFile(file, 0, data, size);
+}
+
bool TruncatePlatformFile(PlatformFile file, int64 length) {
base::ThreadRestrictions::AssertIOAllowed();
if (file == kInvalidPlatformFileValue)