From: Yehuda Sadeh Date: Fri, 4 Oct 2013 17:36:13 +0000 (-0700) Subject: lru_map: add find_and_update() X-Git-Tag: v0.72-rc1~65^2~14 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=6ce762f6412d2855e8e7f328fc75bf3463ba1fae;p=ceph.git lru_map: add find_and_update() A new find_and_update() call to make atomic changes. Signed-off-by: Yehuda Sadeh --- diff --git a/src/common/lru_map.h b/src/common/lru_map.h index 6e7f7b3786fa..62182dd26e83 100644 --- a/src/common/lru_map.h +++ b/src/common/lru_map.h @@ -20,42 +20,66 @@ class lru_map { size_t max; +public: + class UpdateContext { + public: + virtual ~UpdateContext() {} + virtual void update(V& v) = 0; + }; + + bool _find(const K& key, V *value, UpdateContext *ctx); + void _add(const K& key, V& value); + public: lru_map(int _max) : lock("lru_map"), max(_max) {} virtual ~lru_map() {} bool find(const K& key, V& value); + bool find_and_update(const K& key, V *value, UpdateContext *ctx); void add(const K& key, V& value); void erase(const K& key); }; template -bool lru_map::find(const K& key, V& value) +bool lru_map::_find(const K& key, V *value, UpdateContext *ctx) { - lock.Lock(); typename std::map::iterator iter = entries.find(key); if (iter == entries.end()) { - lock.Unlock(); return false; } entry& e = iter->second; entries_lru.erase(e.lru_iter); - value = e.value; + if (ctx) + ctx->update(e.value); + + if (value) + *value = e.value; entries_lru.push_front(key); e.lru_iter = entries_lru.begin(); - lock.Unlock(); - return true; } template -void lru_map::add(const K& key, V& value) +bool lru_map::find(const K& key, V& value) +{ + Mutex::Locker l(lock); + return _find(key, &value, NULL); +} + +template +bool lru_map::find_and_update(const K& key, V *value, UpdateContext *ctx) +{ + Mutex::Locker l(lock); + return _find(key, value, ctx); +} + +template +void lru_map::_add(const K& key, V& value) { - lock.Lock(); typename std::map::iterator iter = entries.find(key); if (iter != entries.end()) { entry& e = iter->second; @@ -74,8 +98,14 @@ void lru_map::add(const K& key, V& value) entries.erase(iter); entries_lru.pop_back(); } - - lock.Unlock(); +} + + +template +void lru_map::add(const K& key, V& value) +{ + Mutex::Locker l(lock); + _add(key, value); } template