Shaka Packager SDK
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 <memory>
12 
13 #include <absl/synchronization/mutex.h>
14 
15 #include <packager/file.h>
16 #include <packager/file/file_closer.h>
17 #include <packager/file/io_cache.h>
18 #include <packager/macros/classes.h>
19 
20 namespace shaka {
21 
23 class ThreadedIoFile : public File {
24  public:
25  enum Mode { kInputMode, kOutputMode };
26 
27  ThreadedIoFile(std::unique_ptr<File, FileCloser> internal_file,
28  Mode mode,
29  uint64_t io_cache_size,
30  uint64_t io_block_size);
31 
34  bool Close() override;
35  int64_t Read(void* buffer, uint64_t length) override;
36  int64_t Write(const void* buffer, uint64_t length) override;
37  void CloseForWriting() override;
38  int64_t Size() override;
39  bool Flush() override;
40  bool Seek(uint64_t position) override;
41  bool Tell(uint64_t* position) override;
43 
44  protected:
45  ~ThreadedIoFile() override;
46 
47  bool Open() override;
48 
49  private:
50  // Internal task handler implementation. Will dispatch to either
51  // |RunInInputMode| or |RunInOutputMode| depending on |mode_|.
52  void TaskHandler();
53  void RunInInputMode();
54  void RunInOutputMode();
55  void WaitForSignal(absl::Mutex* mutex, bool* condition);
56 
57  std::unique_ptr<File, FileCloser> internal_file_;
58  const Mode mode_;
59  IoCache cache_;
60  std::vector<uint8_t> io_buffer_;
61  uint64_t position_;
62  uint64_t size_;
63  std::atomic<bool> eof_;
64  std::atomic<int64_t> internal_file_error_;
65 
66  absl::Mutex flush_mutex_;
67  bool flushing_ ABSL_GUARDED_BY(flush_mutex_);
68  bool flush_complete_ ABSL_GUARDED_BY(flush_mutex_);
69 
70  absl::Mutex task_exited_mutex_;
71  bool task_exited_ ABSL_GUARDED_BY(task_exited_mutex_);
72 
73  DISALLOW_COPY_AND_ASSIGN(ThreadedIoFile);
74 };
75 
76 } // namespace shaka
77 
78 #endif // PACKAGER_FILE_THREADED_IO_FILE_H
Declaration of class which implements a thread-safe circular buffer.
Definition: io_cache.h:20
Declaration of class which implements a thread-safe circular buffer.
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66