#include "common/Mutex.h"
#include "common/Cond.h"
-template <class K, class V>
+template <class K, class V, class C = std::less<K> >
class SharedLRU {
CephContext *cct;
typedef ceph::shared_ptr<V> VPtr;
public:
int waiting;
private:
- map<K, typename list<pair<K, VPtr> >::iterator > contents;
+ map<K, typename list<pair<K, VPtr> >::iterator, C> contents;
list<pair<K, VPtr> > lru;
- map<K, pair<WeakVPtr, V*> > weak_refs;
+ map<K, pair<WeakVPtr, V*>, C> weak_refs;
void trim_cache(list<VPtr> *to_release) {
while (size > max_size) {
}
void lru_remove(const K& key) {
- typename map<K, typename list<pair<K, VPtr> >::iterator>::iterator i =
+ typename map<K, typename list<pair<K, VPtr> >::iterator, C>::iterator i =
contents.find(key);
if (i == contents.end())
return;
}
void lru_add(const K& key, const VPtr& val, list<VPtr> *to_release) {
- typename map<K, typename list<pair<K, VPtr> >::iterator>::iterator i =
+ typename map<K, typename list<pair<K, VPtr> >::iterator, C>::iterator i =
contents.find(key);
if (i != contents.end()) {
lru.splice(lru.begin(), lru, i->second);
void remove(const K& key, V *valptr) {
Mutex::Locker l(lock);
- typename map<K, pair<WeakVPtr, V*> >::iterator i = weak_refs.find(key);
+ typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.find(key);
if (i != weak_refs.end() && i->second.second == valptr) {
weak_refs.erase(i);
}
class Cleanup {
public:
- SharedLRU<K, V> *cache;
+ SharedLRU<K, V, C> *cache;
K key;
- Cleanup(SharedLRU<K, V> *cache, K key) : cache(cache), key(key) {}
+ Cleanup(SharedLRU<K, V, C> *cache, K key) : cache(cache), key(key) {}
void operator()(V *ptr) {
cache->remove(key, ptr);
delete ptr;
}
void dump_weak_refs(ostream& out) {
- for (typename map<K, pair<WeakVPtr, V*> >::iterator p = weak_refs.begin();
+ for (typename map<K, pair<WeakVPtr, V*>, C>::iterator p = weak_refs.begin();
p != weak_refs.end();
++p) {
out << __func__ << " " << this << " weak_refs: "
retry = false;
if (weak_refs.empty())
break;
- typename map<K, pair<WeakVPtr, V*> >::iterator i =
+ typename map<K, pair<WeakVPtr, V*>, C>::iterator i =
weak_refs.lower_bound(key);
if (i == weak_refs.end())
--i;
{
Mutex::Locker l(lock);
VPtr next_val;
- typename map<K, pair<WeakVPtr, V*> >::iterator i = weak_refs.upper_bound(key);
+ typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.upper_bound(key);
while (i != weak_refs.end() &&
!(next_val = i->second.first.lock()))
bool retry = false;
do {
retry = false;
- typename map<K, pair<WeakVPtr, V*> >::iterator i = weak_refs.find(key);
+ typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.find(key);
if (i != weak_refs.end()) {
val = i->second.first.lock();
if (val) {
bool retry = false;
do {
retry = false;
- typename map<K, pair<WeakVPtr, V*> >::iterator i = weak_refs.find(key);
+ typename map<K, pair<WeakVPtr, V*>, C>::iterator i = weak_refs.find(key);
if (i != weak_refs.end()) {
val = i->second.first.lock();
if (val) {
list<VPtr> to_release;
{
Mutex::Locker l(lock);
- typename map<K, pair<WeakVPtr, V*> >::iterator actual =
+ typename map<K, pair<WeakVPtr, V*>, C>::iterator actual =
weak_refs.lower_bound(key);
if (actual != weak_refs.end() && actual->first == key) {
if (existed)