HDFS-10746: libhdfs++: synchronize access to working_directory and bytes_read_. Contributed by Anatoli Shein.

This commit is contained in:
James 2016-08-15 12:21:25 -04:00 committed by James Clampffer
parent cbb3e9fe58
commit b9cf0e932d
3 changed files with 15 additions and 8 deletions

View File

@ -43,18 +43,25 @@ static constexpr tPort kDefaultPort = 8020;
/* Separate the handles used by the C api from the C++ API*/
struct hdfs_internal {
hdfs_internal(FileSystem *p) : filesystem_(p), working_directory("/") {}
hdfs_internal(FileSystem *p) : filesystem_(p), working_directory_("/") {}
hdfs_internal(std::unique_ptr<FileSystem> p)
: filesystem_(std::move(p)), working_directory("/") {}
: filesystem_(std::move(p)), working_directory_("/") {}
virtual ~hdfs_internal(){};
FileSystem *get_impl() { return filesystem_.get(); }
const FileSystem *get_impl() const { return filesystem_.get(); }
std::string get_working_directory() { return working_directory; }
void set_working_directory(std::string new_directory) { working_directory = new_directory; }
std::string get_working_directory() {
std::lock_guard<std::mutex> read_guard(wd_lock_);
return working_directory_;
}
void set_working_directory(std::string new_directory) {
std::lock_guard<std::mutex> write_guard(wd_lock_);
working_directory_ = new_directory;
}
private:
std::unique_ptr<FileSystem> filesystem_;
std::string working_directory; //has to always start and end with '/'
std::string working_directory_; //has to always start and end with '/'
std::mutex wd_lock_; //synchronize access to the working directory
};
struct hdfsFile_internal {

View File

@ -353,8 +353,8 @@ bool FileHandle::ShouldExclude(const Status &s) {
}
}
uint64_t FileHandleImpl::get_bytes_read() { return bytes_read_; }
uint64_t FileHandleImpl::get_bytes_read() { return bytes_read_.load(); }
void FileHandleImpl::clear_bytes_read() { bytes_read_ = 0; }
void FileHandleImpl::clear_bytes_read() { bytes_read_.store(0); }
}

View File

@ -139,7 +139,7 @@ private:
CancelHandle cancel_state_;
ReaderGroup readers_;
std::shared_ptr<LibhdfsEvents> event_handlers_;
uint64_t bytes_read_;
std::atomic<uint64_t> bytes_read_;
};
}