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