From bea1bb2cd0c5dce357f3f52dcf565e8bdf11a592 Mon Sep 17 00:00:00 2001 From: Garry Boyer Date: Thu, 23 Aug 2007 02:29:33 +0000 Subject: [PATCH] made some fixes to thor's file access --- fastlib/thor/blockdev.cc | 7 +++++++ fastlib/thor/blockdev.h | 2 ++ fastlib/thor/distribcache.cc | 1 + 3 files changed, 10 insertions(+) diff --git a/fastlib/thor/blockdev.cc b/fastlib/thor/blockdev.cc index de638d2791..b33c45a245 100644 --- a/fastlib/thor/blockdev.cc +++ b/fastlib/thor/blockdev.cc @@ -6,6 +6,7 @@ #include "blockdev.h" +#define _XOPEN_SOURCE 600 #include #include #include @@ -59,6 +60,8 @@ void RandomAccessFile::Init(const char *fname, BlockDevice::mode_t mode) { FATAL("Could not open file '%s'.", fname); } + posix_fadvise(fd_, 0, 0, POSIX_FADV_RANDOM); + mode_ = mode; } @@ -72,6 +75,7 @@ void RandomAccessFile::Close() { void RandomAccessFile::Write(off_t pos, size_t len, const char *buffer) { off_t rv; + mutex_.Lock(); rv = lseek(fd_, pos, SEEK_SET); assert(rv >= 0); @@ -89,11 +93,13 @@ void RandomAccessFile::Write(off_t pos, size_t len, const char *buffer) { buffer += written; } + mutex_.Unlock(); } void RandomAccessFile::Read(off_t pos, size_t len, char *buffer) { off_t rv; + mutex_.Lock(); rv = lseek(fd_, pos, SEEK_SET); assert(rv >= 0); @@ -116,6 +122,7 @@ void RandomAccessFile::Read(off_t pos, size_t len, char *buffer) { buffer += amount_read; } + mutex_.Unlock(); } off_t RandomAccessFile::FindSize() const { diff --git a/fastlib/thor/blockdev.h b/fastlib/thor/blockdev.h index e88db27334..dbd4eaec62 100644 --- a/fastlib/thor/blockdev.h +++ b/fastlib/thor/blockdev.h @@ -250,6 +250,8 @@ class RandomAccessFile { mode_t mode_; /** The filename open. */ String fname_; + /** Mutex to protect the lseek-read sequence. */ + Mutex mutex_; public: RandomAccessFile() {} diff --git a/fastlib/thor/distribcache.cc b/fastlib/thor/distribcache.cc index 05a4e8d1c0..e7d73f229a 100644 --- a/fastlib/thor/distribcache.cc +++ b/fastlib/thor/distribcache.cc @@ -607,6 +607,7 @@ void DistributedCache::HandleRealMiss_(blockid_t blockid) { mutex_.Lock(); int is_reading = block->is_reading; + DEBUG_ASSERT(block->data == NULL); block->is_reading = NOT_READING; block->data = data; if (is_reading == WAITING) {