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
|
|
|
|
|
|
|
|
#ifndef PACKAGER_FILE_THREADED_IO_FILE_H_
|
|
|
|
#define PACKAGER_FILE_THREADED_IO_FILE_H_
|
|
|
|
|
2018-07-13 22:06:18 +00:00
|
|
|
#include <atomic>
|
2016-08-17 17:41:40 +00:00
|
|
|
#include <memory>
|
2015-10-16 20:10:42 +00:00
|
|
|
#include "packager/base/synchronization/waitable_event.h"
|
2017-07-10 18:26:22 +00:00
|
|
|
#include "packager/file/file.h"
|
|
|
|
#include "packager/file/file_closer.h"
|
|
|
|
#include "packager/file/io_cache.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
|
|
|
|
|
|
|
/// Declaration of class which implements a thread-safe circular buffer.
|
|
|
|
class ThreadedIoFile : public File {
|
|
|
|
public:
|
2017-07-10 18:26:22 +00:00
|
|
|
enum Mode { kInputMode, kOutputMode };
|
2015-03-19 18:28:04 +00:00
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
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);
|
|
|
|
|
|
|
|
/// @name File implementation overrides.
|
|
|
|
/// @{
|
2015-07-22 23:40:45 +00:00
|
|
|
bool Close() override;
|
|
|
|
int64_t Read(void* buffer, uint64_t length) override;
|
|
|
|
int64_t Write(const void* buffer, uint64_t length) override;
|
|
|
|
int64_t Size() override;
|
|
|
|
bool Flush() override;
|
|
|
|
bool Seek(uint64_t position) override;
|
|
|
|
bool Tell(uint64_t* position) override;
|
2015-03-19 18:28:04 +00:00
|
|
|
/// @}
|
|
|
|
|
|
|
|
protected:
|
2015-07-22 23:40:45 +00:00
|
|
|
~ThreadedIoFile() override;
|
2015-03-19 18:28:04 +00:00
|
|
|
|
2015-07-22 23:40:45 +00:00
|
|
|
bool Open() override;
|
2015-03-19 18:28:04 +00:00
|
|
|
|
2015-12-30 01:09:12 +00:00
|
|
|
private:
|
|
|
|
// Internal task handler implementation. Will dispatch to either
|
|
|
|
// |RunInInputMode| or |RunInOutputMode| depending on |mode_|.
|
|
|
|
void TaskHandler();
|
2015-03-19 18:28:04 +00:00
|
|
|
void RunInInputMode();
|
|
|
|
void RunInOutputMode();
|
|
|
|
|
2016-08-17 17:41:40 +00:00
|
|
|
std::unique_ptr<File, FileCloser> internal_file_;
|
2015-03-19 18:28:04 +00:00
|
|
|
const Mode mode_;
|
|
|
|
IoCache cache_;
|
|
|
|
std::vector<uint8_t> io_buffer_;
|
2015-11-19 18:56:43 +00:00
|
|
|
uint64_t position_;
|
2015-03-19 18:28:04 +00:00
|
|
|
uint64_t size_;
|
2018-07-13 22:06:18 +00:00
|
|
|
std::atomic<bool> eof_;
|
2015-10-16 20:10:42 +00:00
|
|
|
bool flushing_;
|
|
|
|
base::WaitableEvent flush_complete_event_;
|
2018-07-13 22:06:18 +00:00
|
|
|
std::atomic<int32_t> internal_file_error_;
|
2015-12-30 01:09:12 +00:00
|
|
|
// Signalled when thread task exits.
|
|
|
|
base::WaitableEvent task_exit_event_;
|
2015-03-19 18:28:04 +00:00
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ThreadedIoFile);
|
|
|
|
};
|
|
|
|
|
2016-05-20 21:19:33 +00:00
|
|
|
} // namespace shaka
|
2015-03-19 18:28:04 +00:00
|
|
|
|
|
|
|
#endif // PACKAGER_FILE_THREADED_IO_FILE_H
|