2015-03-19 18:28:04 +00:00
|
|
|
// Copyright 2015 Google Inc. All rights reserved.
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license that can be found in the LICENSE file or at
|
|
|
|
// https://developers.google.com/open-source/licenses/bsd
|
|
|
|
|
2017-07-10 18:26:22 +00:00
|
|
|
#include "packager/file/threaded_io_file.h"
|
2015-03-19 18:28:04 +00:00
|
|
|
|
|
|
|
#include "packager/base/bind.h"
|
|
|
|
#include "packager/base/bind_helpers.h"
|
2015-12-30 01:09:12 +00:00
|
|
|
#include "packager/base/location.h"
|
|
|
|
#include "packager/base/threading/worker_pool.h"
|
2015-03-19 18:28:04 +00:00
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
namespace shaka {
|
2015-03-19 18:28:04 +00:00
|
|
|
|
2015-11-14 02:43:25 +00:00
|
|
|
using base::subtle::NoBarrier_Load;
|
|
|
|
using base::subtle::NoBarrier_Store;
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
ThreadedIoFile::ThreadedIoFile(std::unique_ptr<File, FileCloser> internal_file,
|
2015-03-19 18:28:04 +00:00
|
|
|
Mode mode,
|
|
|
|
uint64_t io_cache_size,
|
|
|
|
uint64_t io_block_size)
|
|
|
|
: File(internal_file->file_name()),
|
2016-08-17 17:41:40 +00:00
|
|
|
internal_file_(std::move(internal_file)),
|
2015-03-19 18:28:04 +00:00
|
|
|
mode_(mode),
|
|
|
|
cache_(io_cache_size),
|
|
|
|
io_buffer_(io_block_size),
|
2015-11-19 18:56:43 +00:00
|
|
|
position_(0),
|
2015-03-19 18:28:04 +00:00
|
|
|
size_(0),
|
|
|
|
eof_(false),
|
2015-10-16 20:10:42 +00:00
|
|
|
flushing_(false),
|
2016-08-17 21:42:23 +00:00
|
|
|
flush_complete_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
|
|
|
|
base::WaitableEvent::InitialState::NOT_SIGNALED),
|
2015-12-30 01:09:12 +00:00
|
|
|
internal_file_error_(0),
|
2016-08-17 21:42:23 +00:00
|
|
|
task_exit_event_(base::WaitableEvent::ResetPolicy::AUTOMATIC,
|
|
|
|
base::WaitableEvent::InitialState::NOT_SIGNALED) {
|
2015-03-19 18:28:04 +00:00
|
|
|
DCHECK(internal_file_);
|
|
|
|
}
|
|
|
|
|
|
|
|
ThreadedIoFile::~ThreadedIoFile() {}
|
|
|
|
|
|
|
|
bool ThreadedIoFile::Open() {
|
|
|
|
DCHECK(internal_file_);
|
|
|
|
|
|
|
|
if (!internal_file_->Open())
|
|
|
|
return false;
|
|
|
|
|
2015-11-19 18:56:43 +00:00
|
|
|
position_ = 0;
|
2015-03-19 18:28:04 +00:00
|
|
|
size_ = internal_file_->Size();
|
|
|
|
|
2017-07-10 18:26:22 +00:00
|
|
|
base::WorkerPool::PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&ThreadedIoFile::TaskHandler, base::Unretained(this)),
|
|
|
|
true /* task_is_slow */);
|
2015-03-19 18:28:04 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ThreadedIoFile::Close() {
|
|
|
|
DCHECK(internal_file_);
|
|
|
|
|
2017-09-07 23:05:20 +00:00
|
|
|
bool result = true;
|
2015-03-19 18:28:04 +00:00
|
|
|
if (mode_ == kOutputMode)
|
2017-09-07 23:05:20 +00:00
|
|
|
result = Flush();
|
2015-03-19 18:28:04 +00:00
|
|
|
|
|
|
|
cache_.Close();
|
2015-12-30 01:09:12 +00:00
|
|
|
task_exit_event_.Wait();
|
2015-03-19 18:28:04 +00:00
|
|
|
|
2017-09-07 23:05:20 +00:00
|
|
|
result &= internal_file_.release()->Close();
|
2015-03-19 18:28:04 +00:00
|
|
|
delete this;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
int64_t ThreadedIoFile::Read(void* buffer, uint64_t length) {
|
|
|
|
DCHECK(internal_file_);
|
|
|
|
DCHECK_EQ(kInputMode, mode_);
|
|
|
|
|
2015-11-14 02:43:25 +00:00
|
|
|
if (NoBarrier_Load(&eof_) && !cache_.BytesCached())
|
2015-03-19 18:28:04 +00:00
|
|
|
return 0;
|
|
|
|
|
2015-11-14 02:43:25 +00:00
|
|
|
if (NoBarrier_Load(&internal_file_error_))
|
|
|
|
return NoBarrier_Load(&internal_file_error_);
|
|
|
|
|
2015-11-19 18:56:43 +00:00
|
|
|
uint64_t bytes_read = cache_.Read(buffer, length);
|
|
|
|
position_ += bytes_read;
|
|
|
|
|
|
|
|
return bytes_read;
|
2015-03-19 18:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t ThreadedIoFile::Write(const void* buffer, uint64_t length) {
|
|
|
|
DCHECK(internal_file_);
|
|
|
|
DCHECK_EQ(kOutputMode, mode_);
|
|
|
|
|
2015-11-14 02:43:25 +00:00
|
|
|
if (NoBarrier_Load(&internal_file_error_))
|
|
|
|
return NoBarrier_Load(&internal_file_error_);
|
2015-03-19 18:28:04 +00:00
|
|
|
|
2015-11-19 18:56:43 +00:00
|
|
|
uint64_t bytes_written = cache_.Write(buffer, length);
|
|
|
|
position_ += bytes_written;
|
|
|
|
if (position_ > size_)
|
|
|
|
size_ = position_;
|
|
|
|
|
|
|
|
return bytes_written;
|
2015-03-19 18:28:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int64_t ThreadedIoFile::Size() {
|
|
|
|
DCHECK(internal_file_);
|
|
|
|
|
|
|
|
return size_;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ThreadedIoFile::Flush() {
|
|
|
|
DCHECK(internal_file_);
|
|
|
|
DCHECK_EQ(kOutputMode, mode_);
|
|
|
|
|
2017-09-07 23:05:20 +00:00
|
|
|
if (NoBarrier_Load(&internal_file_error_))
|
|
|
|
return false;
|
|
|
|
|
2015-10-16 20:10:42 +00:00
|
|
|
flushing_ = true;
|
|
|
|
cache_.Close();
|
|
|
|
flush_complete_event_.Wait();
|
2015-03-19 18:28:04 +00:00
|
|
|
return internal_file_->Flush();
|
|
|
|
}
|
|
|
|
|
2015-11-19 18:56:43 +00:00
|
|
|
bool ThreadedIoFile::Seek(uint64_t position) {
|
|
|
|
if (mode_ == kOutputMode) {
|
|
|
|
// Writing. Just flush the cache and seek.
|
2017-07-10 18:26:22 +00:00
|
|
|
if (!Flush())
|
|
|
|
return false;
|
|
|
|
if (!internal_file_->Seek(position))
|
|
|
|
return false;
|
2015-11-19 18:56:43 +00:00
|
|
|
} else {
|
2015-12-30 01:09:12 +00:00
|
|
|
// Reading. Close cache, wait for thread task to exit, seek, and re-post
|
|
|
|
// the task.
|
2015-11-19 18:56:43 +00:00
|
|
|
cache_.Close();
|
2015-12-30 01:09:12 +00:00
|
|
|
task_exit_event_.Wait();
|
2015-11-19 18:56:43 +00:00
|
|
|
bool result = internal_file_->Seek(position);
|
|
|
|
if (!result) {
|
|
|
|
// Seek failed. Seek to logical position instead.
|
|
|
|
if (!internal_file_->Seek(position_) && (position != position_)) {
|
|
|
|
LOG(WARNING) << "Seek failed. ThreadedIoFile left in invalid state.";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
cache_.Reopen();
|
|
|
|
eof_ = false;
|
2015-12-30 01:09:12 +00:00
|
|
|
base::WorkerPool::PostTask(
|
|
|
|
FROM_HERE,
|
|
|
|
base::Bind(&ThreadedIoFile::TaskHandler, base::Unretained(this)),
|
|
|
|
true /* task_is_slow */);
|
2017-07-10 18:26:22 +00:00
|
|
|
if (!result)
|
|
|
|
return false;
|
2015-11-19 18:56:43 +00:00
|
|
|
}
|
|
|
|
position_ = position;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ThreadedIoFile::Tell(uint64_t* position) {
|
|
|
|
DCHECK(position);
|
|
|
|
|
|
|
|
*position = position_;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-12-30 01:09:12 +00:00
|
|
|
void ThreadedIoFile::TaskHandler() {
|
|
|
|
if (mode_ == kInputMode)
|
|
|
|
RunInInputMode();
|
|
|
|
else
|
|
|
|
RunInOutputMode();
|
|
|
|
task_exit_event_.Signal();
|
|
|
|
}
|
|
|
|
|
2015-03-19 18:28:04 +00:00
|
|
|
void ThreadedIoFile::RunInInputMode() {
|
|
|
|
DCHECK(internal_file_);
|
|
|
|
DCHECK_EQ(kInputMode, mode_);
|
|
|
|
|
|
|
|
while (true) {
|
2017-07-10 18:26:22 +00:00
|
|
|
int64_t read_result =
|
|
|
|
internal_file_->Read(&io_buffer_[0], io_buffer_.size());
|
2015-03-19 18:28:04 +00:00
|
|
|
if (read_result <= 0) {
|
2015-11-14 02:43:25 +00:00
|
|
|
NoBarrier_Store(&eof_, read_result == 0);
|
|
|
|
NoBarrier_Store(&internal_file_error_, read_result);
|
2015-03-19 18:28:04 +00:00
|
|
|
cache_.Close();
|
|
|
|
return;
|
|
|
|
}
|
2015-11-19 18:56:43 +00:00
|
|
|
if (cache_.Write(&io_buffer_[0], read_result) == 0) {
|
|
|
|
return;
|
|
|
|
}
|
2015-03-19 18:28:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ThreadedIoFile::RunInOutputMode() {
|
|
|
|
DCHECK(internal_file_);
|
|
|
|
DCHECK_EQ(kOutputMode, mode_);
|
|
|
|
|
|
|
|
while (true) {
|
|
|
|
uint64_t write_bytes = cache_.Read(&io_buffer_[0], io_buffer_.size());
|
2015-10-16 20:10:42 +00:00
|
|
|
if (write_bytes == 0) {
|
|
|
|
if (flushing_) {
|
|
|
|
cache_.Reopen();
|
|
|
|
flushing_ = false;
|
|
|
|
flush_complete_event_.Signal();
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
2015-11-14 02:43:25 +00:00
|
|
|
uint64_t bytes_written(0);
|
|
|
|
while (bytes_written < write_bytes) {
|
|
|
|
int64_t write_result = internal_file_->Write(
|
|
|
|
&io_buffer_[bytes_written], write_bytes - bytes_written);
|
|
|
|
if (write_result < 0) {
|
|
|
|
NoBarrier_Store(&internal_file_error_, write_result);
|
|
|
|
cache_.Close();
|
2017-09-07 23:05:20 +00:00
|
|
|
if (flushing_) {
|
|
|
|
flushing_ = false;
|
|
|
|
flush_complete_event_.Signal();
|
|
|
|
}
|
2015-11-14 02:43:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
bytes_written += write_result;
|
2015-10-16 20:10:42 +00:00
|
|
|
}
|
2015-03-19 18:28:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|