2012-03-28 04:04:20 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
|
|
|
|
* You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#ifndef FILE_BLOCK_CACHE_H_
|
|
|
|
#define FILE_BLOCK_CACHE_H_
|
|
|
|
|
2013-05-30 00:43:41 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2012-03-28 04:04:20 +04:00
|
|
|
#include "mozilla/Monitor.h"
|
|
|
|
#include "nsTArray.h"
|
2012-11-14 23:46:40 +04:00
|
|
|
#include "MediaCache.h"
|
2012-03-28 04:04:20 +04:00
|
|
|
#include "nsDeque.h"
|
2013-09-06 00:25:17 +04:00
|
|
|
#include "nsThreadUtils.h"
|
|
|
|
|
|
|
|
struct PRFileDesc;
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// Manages file I/O for the media cache. Data comes in over the network
|
|
|
|
// via callbacks on the main thread, however we don't want to write the
|
|
|
|
// incoming data to the media cache on the main thread, as this could block
|
2014-10-13 02:53:42 +04:00
|
|
|
// causing UI jank.
|
2012-03-28 04:04:20 +04:00
|
|
|
//
|
|
|
|
// So FileBlockCache provides an abstraction for a temporary file accessible
|
|
|
|
// as an array of blocks, which supports a block move operation, and
|
|
|
|
// allows synchronous reading and writing from any thread, with writes being
|
|
|
|
// buffered so as not to block.
|
|
|
|
//
|
|
|
|
// Writes and cache block moves (which require reading) are deferred to
|
|
|
|
// their own non-main thread. This object also ensures that data which has
|
|
|
|
// been scheduled to be written, but hasn't actually *been* written, is read
|
|
|
|
// as if it had, i.e. pending writes are cached in readable memory until
|
|
|
|
// they're flushed to file.
|
|
|
|
//
|
|
|
|
// To improve efficiency, writes can only be done at block granularity,
|
|
|
|
// whereas reads can be done with byte granularity.
|
|
|
|
//
|
|
|
|
// Note it's also recommended not to read from the media cache from the main
|
|
|
|
// thread to prevent jank.
|
|
|
|
//
|
|
|
|
// When WriteBlock() or MoveBlock() are called, data about how to complete
|
|
|
|
// the block change is added to mBlockChanges, indexed by block index, and
|
|
|
|
// the block index is appended to the mChangeIndexList. This enables
|
|
|
|
// us to quickly tell if a block has been changed, and ensures we can perform
|
|
|
|
// the changes in the correct order. An event is dispatched to perform the
|
|
|
|
// changes listed in mBlockChanges to file. Read() checks mBlockChanges and
|
|
|
|
// determines the current data to return, reading from file or from
|
|
|
|
// mBlockChanges as necessary.
|
|
|
|
class FileBlockCache : public nsRunnable {
|
|
|
|
public:
|
|
|
|
enum {
|
2012-11-14 23:46:40 +04:00
|
|
|
BLOCK_SIZE = MediaCacheStream::BLOCK_SIZE
|
2012-03-28 04:04:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
FileBlockCache();
|
|
|
|
|
2014-07-15 19:37:45 +04:00
|
|
|
protected:
|
2012-03-28 04:04:20 +04:00
|
|
|
~FileBlockCache();
|
|
|
|
|
2014-07-15 19:37:45 +04:00
|
|
|
public:
|
2012-03-28 04:04:20 +04:00
|
|
|
// Assumes ownership of aFD.
|
|
|
|
nsresult Open(PRFileDesc* aFD);
|
|
|
|
|
|
|
|
// Closes writer, shuts down thread.
|
|
|
|
void Close();
|
|
|
|
|
|
|
|
// Can be called on any thread. This defers to a non-main thread.
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult WriteBlock(uint32_t aBlockIndex, const uint8_t* aData);
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
// Performs block writes and block moves on its own thread.
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Run() override;
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
// Synchronously reads data from file. May read from file or memory
|
|
|
|
// depending on whether written blocks have been flushed to file yet.
|
|
|
|
// Not recommended to be called from the main thread, as can cause jank.
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult Read(int64_t aOffset,
|
|
|
|
uint8_t* aData,
|
|
|
|
int32_t aLength,
|
|
|
|
int32_t* aBytes);
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
// Moves a block asynchronously. Can be called on any thread.
|
|
|
|
// This defers file I/O to a non-main thread.
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult MoveBlock(int32_t aSourceBlockIndex, int32_t aDestBlockIndex);
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
// Represents a change yet to be made to a block in the file. The change
|
|
|
|
// is either a write (and the data to be written is stored in this struct)
|
|
|
|
// or a move (and the index of the source block is stored instead).
|
2015-03-21 19:28:04 +03:00
|
|
|
struct BlockChange final {
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(BlockChange)
|
|
|
|
|
|
|
|
// This block is waiting in memory to be written.
|
|
|
|
// Stores a copy of the block, so we can write it asynchronously.
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit BlockChange(const uint8_t* aData)
|
2012-03-28 04:04:20 +04:00
|
|
|
: mSourceBlockIndex(-1)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
mData = new uint8_t[BLOCK_SIZE];
|
2012-03-28 04:04:20 +04:00
|
|
|
memcpy(mData.get(), aData, BLOCK_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// This block's contents are located in another file
|
|
|
|
// block, i.e. this block has been moved.
|
2014-09-01 07:50:23 +04:00
|
|
|
explicit BlockChange(int32_t aSourceBlockIndex)
|
2012-03-28 04:04:20 +04:00
|
|
|
: mSourceBlockIndex(aSourceBlockIndex) {}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsAutoArrayPtr<uint8_t> mData;
|
|
|
|
const int32_t mSourceBlockIndex;
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
bool IsMove() const {
|
|
|
|
return mSourceBlockIndex != -1;
|
|
|
|
}
|
|
|
|
bool IsWrite() const {
|
|
|
|
return mSourceBlockIndex == -1 &&
|
2012-07-30 18:20:58 +04:00
|
|
|
mData.get() != nullptr;
|
2012-03-28 04:04:20 +04:00
|
|
|
}
|
2014-04-02 20:21:11 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
// Private destructor, to discourage deletion outside of Release():
|
|
|
|
~BlockChange()
|
|
|
|
{
|
|
|
|
}
|
2012-03-28 04:04:20 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
class Int32Queue : private nsDeque {
|
|
|
|
public:
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t PopFront() {
|
|
|
|
int32_t front = ObjectAt(0);
|
2012-03-28 04:04:20 +04:00
|
|
|
nsDeque::PopFront();
|
|
|
|
return front;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
void PushBack(int32_t aValue) {
|
2012-03-28 04:04:20 +04:00
|
|
|
nsDeque::Push(reinterpret_cast<void*>(aValue));
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
bool Contains(int32_t aValue) {
|
|
|
|
for (int32_t i = 0; i < GetSize(); ++i) {
|
2012-03-28 04:04:20 +04:00
|
|
|
if (ObjectAt(i) == aValue) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IsEmpty() {
|
|
|
|
return nsDeque::GetSize() == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t ObjectAt(int32_t aIndex) {
|
2012-03-28 04:04:20 +04:00
|
|
|
void* v = nsDeque::ObjectAt(aIndex);
|
2013-11-05 16:45:00 +04:00
|
|
|
return reinterpret_cast<uintptr_t>(v);
|
2012-03-28 04:04:20 +04:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
private:
|
2013-01-11 00:21:50 +04:00
|
|
|
int64_t BlockIndexToOffset(int32_t aBlockIndex) {
|
|
|
|
return static_cast<int64_t>(aBlockIndex) * BLOCK_SIZE;
|
|
|
|
}
|
|
|
|
|
2012-03-28 04:04:20 +04:00
|
|
|
// Monitor which controls access to mFD and mFDCurrentPos. Don't hold
|
|
|
|
// mDataMonitor while holding mFileMonitor! mFileMonitor must be owned
|
|
|
|
// while accessing any of the following data fields or methods.
|
2012-11-14 23:46:40 +04:00
|
|
|
Monitor mFileMonitor;
|
2012-03-28 04:04:20 +04:00
|
|
|
// Moves a block already committed to file.
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult MoveBlockInFile(int32_t aSourceBlockIndex,
|
|
|
|
int32_t aDestBlockIndex);
|
2012-03-28 04:04:20 +04:00
|
|
|
// Seeks file pointer.
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult Seek(int64_t aOffset);
|
2012-03-28 04:04:20 +04:00
|
|
|
// Reads data from file offset.
|
2013-01-11 00:21:50 +04:00
|
|
|
nsresult ReadFromFile(int64_t aOffset,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint8_t* aDest,
|
|
|
|
int32_t aBytesToRead,
|
|
|
|
int32_t& aBytesRead);
|
|
|
|
nsresult WriteBlockToFile(int32_t aBlockIndex, const uint8_t* aBlockData);
|
2012-03-28 04:04:20 +04:00
|
|
|
// File descriptor we're writing to. This is created externally, but
|
|
|
|
// shutdown by us.
|
|
|
|
PRFileDesc* mFD;
|
|
|
|
// The current file offset in the file.
|
2012-08-22 19:56:38 +04:00
|
|
|
int64_t mFDCurrentPos;
|
2012-03-28 04:04:20 +04:00
|
|
|
|
|
|
|
// Monitor which controls access to all data in this class, except mFD
|
|
|
|
// and mFDCurrentPos. Don't hold mDataMonitor while holding mFileMonitor!
|
|
|
|
// mDataMonitor must be owned while accessing any of the following data
|
|
|
|
// fields or methods.
|
2012-11-14 23:46:40 +04:00
|
|
|
Monitor mDataMonitor;
|
2012-03-28 04:04:20 +04:00
|
|
|
// Ensures we either are running the event to preform IO, or an event
|
|
|
|
// has been dispatched to preform the IO.
|
|
|
|
// mDataMonitor must be owned while calling this.
|
|
|
|
void EnsureWriteScheduled();
|
2012-07-30 18:20:58 +04:00
|
|
|
// Array of block changes to made. If mBlockChanges[offset/BLOCK_SIZE] == nullptr,
|
2012-03-28 04:04:20 +04:00
|
|
|
// then the block has no pending changes to be written, but if
|
2012-07-30 18:20:58 +04:00
|
|
|
// mBlockChanges[offset/BLOCK_SIZE] != nullptr, then either there's a block
|
2012-03-28 04:04:20 +04:00
|
|
|
// cached in memory waiting to be written, or this block is the target of a
|
|
|
|
// block move.
|
|
|
|
nsTArray< nsRefPtr<BlockChange> > mBlockChanges;
|
|
|
|
// Thread upon which block writes and block moves are performed. This is
|
|
|
|
// created upon open, and shutdown (asynchronously) upon close (on the
|
|
|
|
// main thread).
|
|
|
|
nsCOMPtr<nsIThread> mThread;
|
|
|
|
// Queue of pending block indexes that need to be written or moved.
|
2012-08-22 19:56:38 +04:00
|
|
|
//nsAutoTArray<int32_t, 8> mChangeIndexList;
|
2012-03-28 04:04:20 +04:00
|
|
|
Int32Queue mChangeIndexList;
|
|
|
|
// True if we've dispatched an event to commit all pending block changes
|
|
|
|
// to file on mThread.
|
|
|
|
bool mIsWriteScheduled;
|
|
|
|
// True if the writer is ready to write data to file.
|
|
|
|
bool mIsOpen;
|
|
|
|
};
|
|
|
|
|
|
|
|
} // End namespace mozilla.
|
|
|
|
|
|
|
|
#endif /* FILE_BLOCK_CACHE_H_ */
|