Shaka Packager SDK
io_cache.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_IO_CACHE_H_
8 #define PACKAGER_FILE_IO_CACHE_H_
9 
10 #include <cstdint>
11 #include <vector>
12 
13 #include <absl/synchronization/mutex.h>
14 
15 #include <packager/macros/classes.h>
16 
17 namespace shaka {
18 
20 class IoCache {
21  public:
22  explicit IoCache(uint64_t cache_size);
23  ~IoCache();
24 
31  uint64_t Read(void* buffer, uint64_t size);
32 
40  uint64_t Write(const void* buffer, uint64_t size);
41 
43  void Clear();
44 
47  void Close();
48 
50  bool closed() { return closed_; }
51 
53  void Reopen();
54 
57  uint64_t BytesCached();
58 
61  uint64_t BytesFree();
62 
65 
66  private:
67  uint64_t BytesCachedInternal();
68  uint64_t BytesFreeInternal();
69 
70  const uint64_t cache_size_;
71  absl::Mutex mutex_;
72  absl::CondVar read_event_ ABSL_GUARDED_BY(mutex_);
73  absl::CondVar write_event_ ABSL_GUARDED_BY(mutex_);
74  std::vector<uint8_t> circular_buffer_ ABSL_GUARDED_BY(mutex_);
75  const uint8_t* end_ptr_ ABSL_GUARDED_BY(mutex_);
76  uint8_t* r_ptr_ ABSL_GUARDED_BY(mutex_);
77  uint8_t* w_ptr_ ABSL_GUARDED_BY(mutex_);
78  bool closed_ ABSL_GUARDED_BY(mutex_);
79 
80  DISALLOW_COPY_AND_ASSIGN(IoCache);
81 };
82 
83 } // namespace shaka
84 
85 #endif // PACKAGER_FILE_IO_CACHE_H
Declaration of class which implements a thread-safe circular buffer.
Definition: io_cache.h:20
void Clear()
Empties the cache.
Definition: io_cache.cc:98
void Close()
Definition: io_cache.cc:105
uint64_t Write(const void *buffer, uint64_t size)
Definition: io_cache.cc:60
bool closed()
Definition: io_cache.h:50
uint64_t BytesFree()
Definition: io_cache.cc:124
uint64_t Read(void *buffer, uint64_t size)
Definition: io_cache.cc:33
uint64_t BytesCached()
Definition: io_cache.cc:119
void WaitUntilEmptyOrClosed()
Waits until the cache is empty or has been closed.
Definition: io_cache.cc:139
void Reopen()
Reopens the cache. Any data still in the cache will be lost.
Definition: io_cache.cc:112
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66