Shaka Packager SDK
Loading...
Searching...
No Matches
threaded_io_file.h
1// Copyright 2015 Google LLC. All rights reserved.
2//
3// Use of this source code is governed by a BSD-style
4// license that can be found in the LICENSE file or at
5// https://developers.google.com/open-source/licenses/bsd
6
7#ifndef PACKAGER_FILE_THREADED_IO_FILE_H_
8#define PACKAGER_FILE_THREADED_IO_FILE_H_
9
10#include <atomic>
11#include <cstdint>
12#include <memory>
13#include <vector>
14
15#include <absl/base/thread_annotations.h>
16#include <absl/synchronization/mutex.h>
17
18#include <packager/file.h>
19#include <packager/file/file_closer.h>
20#include <packager/file/io_cache.h>
21#include <packager/macros/classes.h>
22
23namespace shaka {
24
26class ThreadedIoFile : public File {
27 public:
28 enum Mode { kInputMode, kOutputMode };
29
30 ThreadedIoFile(std::unique_ptr<File, FileCloser> internal_file,
31 Mode mode,
32 uint64_t io_cache_size,
33 uint64_t io_block_size);
34
37 bool Close() override;
38 int64_t Read(void* buffer, uint64_t length) override;
39 int64_t Write(const void* buffer, uint64_t length) override;
40 void CloseForWriting() override;
41 int64_t Size() override;
42 bool Flush() override;
43 bool Seek(uint64_t position) override;
44 bool Tell(uint64_t* position) override;
46
47 protected:
48 ~ThreadedIoFile() override;
49
50 bool Open() override;
51
52 private:
53 // Internal task handler implementation. Will dispatch to either
54 // |RunInInputMode| or |RunInOutputMode| depending on |mode_|.
55 void TaskHandler();
56 void RunInInputMode();
57 void RunInOutputMode();
58 void WaitForSignal(absl::Mutex* mutex, bool* condition);
59
60 std::unique_ptr<File, FileCloser> internal_file_;
61 const Mode mode_;
62 IoCache cache_;
63 std::vector<uint8_t> io_buffer_;
64 uint64_t position_;
65 uint64_t size_;
66 std::atomic<bool> eof_;
67 std::atomic<int64_t> internal_file_error_;
68
69 absl::Mutex flush_mutex_;
70 bool flushing_ ABSL_GUARDED_BY(flush_mutex_);
71 bool flush_complete_ ABSL_GUARDED_BY(flush_mutex_);
72
73 absl::Mutex task_exited_mutex_;
74 bool task_exited_ ABSL_GUARDED_BY(task_exited_mutex_);
75
76 DISALLOW_COPY_AND_ASSIGN(ThreadedIoFile);
77};
78
79} // namespace shaka
80
81#endif // PACKAGER_FILE_THREADED_IO_FILE_H
Declaration of class which implements a thread-safe circular buffer.
Definition io_cache.h:21
Declaration of class which implements a thread-safe circular buffer.
All the methods that are virtual are virtual for mocking.