2019-05-12 08:45:56 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
|
|
#include "stream.h"
|
|
|
|
|
|
|
|
|
|
namespace firesse {
|
|
|
|
|
|
|
|
|
|
// Track live streams
|
2019-05-12 11:18:59 -07:00
|
|
|
// O(1) for Add(), Remove(), and Freshen()
|
|
|
|
|
// Manages this by only supporting insertion of the freshest element,
|
|
|
|
|
// and by requiring an iterator for removal.
|
|
|
|
|
// Avoids allocation and folds in iterators by using an intrusive list
|
|
|
|
|
// inside Stream.
|
2019-05-12 08:45:56 -07:00
|
|
|
class Index {
|
2019-05-18 12:18:26 -07:00
|
|
|
public:
|
|
|
|
|
void Add(Stream* stream);
|
|
|
|
|
void Remove(Stream* stream);
|
|
|
|
|
void Freshen(Stream* stream);
|
2019-05-12 08:45:56 -07:00
|
|
|
|
2019-05-18 12:18:26 -07:00
|
|
|
// Returns time to sleep until next stalest, or min_stale if none
|
|
|
|
|
// Only calls callback if stalest is at least min_stale
|
|
|
|
|
// Handles all locking and marks Stream as fresh after callback
|
|
|
|
|
std::chrono::nanoseconds WithStalest(
|
|
|
|
|
std::function<void(Stream*)> callback,
|
|
|
|
|
const std::chrono::nanoseconds& min_stale);
|
2019-05-12 11:18:59 -07:00
|
|
|
|
2019-05-18 12:18:26 -07:00
|
|
|
private:
|
|
|
|
|
std::recursive_mutex mu_;
|
|
|
|
|
Stream* freshest_ = nullptr;
|
|
|
|
|
Stream* stalest_ = nullptr;
|
2019-05-12 08:45:56 -07:00
|
|
|
};
|
|
|
|
|
|
2019-05-18 12:18:26 -07:00
|
|
|
} // namespace firesse
|