Shaka Packager SDK
io_cache.h
1 // Copyright 2015 Google Inc. 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 <stdint.h>
11 #include <vector>
12 #include "packager/base/macros.h"
13 #include "packager/base/synchronization/lock.h"
14 #include "packager/base/synchronization/waitable_event.h"
15 
16 namespace shaka {
17 
19 class IoCache {
20  public:
21  explicit IoCache(uint64_t cache_size);
22  ~IoCache();
23 
30  uint64_t Read(void* buffer, uint64_t size);
31 
39  uint64_t Write(const void* buffer, uint64_t size);
40 
42  void Clear();
43 
46  void Close();
47 
49  bool closed() { return closed_; }
50 
52  void Reopen();
53 
56  uint64_t BytesCached();
57 
60  uint64_t BytesFree();
61 
64 
65  private:
66  uint64_t BytesCachedInternal();
67  uint64_t BytesFreeInternal();
68 
69  const uint64_t cache_size_;
70  base::Lock lock_;
71  base::WaitableEvent read_event_;
72  base::WaitableEvent write_event_;
73  std::vector<uint8_t> circular_buffer_;
74  const uint8_t* end_ptr_;
75  uint8_t* r_ptr_;
76  uint8_t* w_ptr_;
77  bool closed_;
78 
79  DISALLOW_COPY_AND_ASSIGN(IoCache);
80 };
81 
82 } // namespace shaka
83 
84 #endif // PACKAGER_FILE_IO_CACHE_H
Declaration of class which implements a thread-safe circular buffer.
Definition: io_cache.h:19
void Reopen()
Reopens the cache. Any data still in the cache will be lost.
Definition: io_cache.cc:116
void Clear()
Empties the cache.
Definition: io_cache.cc:102
void Close()
Definition: io_cache.cc:109
All the methods that are virtual are virtual for mocking.
bool closed()
Definition: io_cache.h:49
uint64_t Write(const void *buffer, uint64_t size)
Definition: io_cache.cc:66
void WaitUntilEmptyOrClosed()
Waits until the cache is empty or has been closed.
Definition: io_cache.cc:145
uint64_t Read(void *buffer, uint64_t size)
Definition: io_cache.cc:38
uint64_t BytesFree()
Definition: io_cache.cc:130
uint64_t BytesCached()
Definition: io_cache.cc:125