-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #578 from Barenboim/master
remove mutex from LRUCache
- Loading branch information
Showing
3 changed files
with
16 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ | |
limitations under the License. | ||
Authors: Wu Jiaxu ([email protected]) | ||
Xie Han ([email protected]) | ||
*/ | ||
|
||
#include <stdint.h> | ||
|
@@ -27,40 +28,30 @@ | |
|
||
const DnsCache::DnsHandle *DnsCache::get_inner(const HostPort& host_port, int type) | ||
{ | ||
int64_t cur_time = GET_CURRENT_SECOND; | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
const DnsHandle *handle = cache_pool_.get(host_port); | ||
|
||
if (handle) | ||
{ | ||
int64_t cur_time = GET_CURRENT_SECOND; | ||
|
||
switch (type) | ||
{ | ||
case GET_TYPE_TTL: | ||
if (cur_time > handle->value.expire_time) | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
|
||
if (cur_time > handle->value.expire_time) | ||
{ | ||
const_cast<DnsHandle *>(handle)->value.expire_time += TTL_INC; | ||
cache_pool_.release(handle); | ||
return NULL; | ||
} | ||
const_cast<DnsHandle *>(handle)->value.expire_time += TTL_INC; | ||
cache_pool_.release(handle); | ||
return NULL; | ||
} | ||
|
||
break; | ||
|
||
case GET_TYPE_CONFIDENT: | ||
if (cur_time > handle->value.confident_time) | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
|
||
if (cur_time > handle->value.confident_time) | ||
{ | ||
const_cast<DnsHandle *>(handle)->value.confident_time += CONFIDENT_INC; | ||
cache_pool_.release(handle); | ||
return NULL; | ||
} | ||
const_cast<DnsHandle *>(handle)->value.confident_time += CONFIDENT_INC; | ||
cache_pool_.release(handle); | ||
return NULL; | ||
} | ||
|
||
break; | ||
|
@@ -96,7 +87,6 @@ const DnsCache::DnsHandle *DnsCache::put(const HostPort& host_port, | |
expire_time = cur_time + dns_ttl_default; | ||
|
||
std::lock_guard<std::mutex> lock(mutex_); | ||
|
||
return cache_pool_.put(host_port, {addrinfo, confident_time, expire_time}); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,14 +14,14 @@ | |
limitations under the License. | ||
Authors: Wu Jiaxu ([email protected]) | ||
Xie Han ([email protected]) -- remove mutex | ||
*/ | ||
|
||
#ifndef _LRUCACHE_H_ | ||
#define _LRUCACHE_H_ | ||
|
||
#include <assert.h> | ||
#include <map> | ||
#include <mutex> | ||
//#include <unordered_map> | ||
|
||
/** | ||
* @file LRUCache.h | ||
|
@@ -101,8 +101,6 @@ typedef typename Map::const_iterator MapConstIterator; | |
|
||
~LRUCache() | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
|
||
// Error if caller has an unreleased handle | ||
assert(in_use_.next == &in_use_); | ||
for (Handle *e = not_use_.next; e != ¬_use_; ) | ||
|
@@ -121,8 +119,6 @@ typedef typename Map::const_iterator MapConstIterator; | |
// max_size means max cache number of key-value pairs | ||
void set_max_size(size_t max_size) | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
|
||
max_size_ = max_size; | ||
} | ||
|
||
|
@@ -132,8 +128,6 @@ typedef typename Map::const_iterator MapConstIterator; | |
// Remove all cache that are not actively in use. | ||
void prune() | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
|
||
while (not_use_.next != ¬_use_) | ||
{ | ||
Handle *e = not_use_.next; | ||
|
@@ -147,12 +141,7 @@ typedef typename Map::const_iterator MapConstIterator; | |
// release handle by get/put | ||
void release(Handle *handle) | ||
{ | ||
if (handle) | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
|
||
unref(handle); | ||
} | ||
unref(handle); | ||
} | ||
|
||
void release(const Handle *handle) | ||
|
@@ -164,7 +153,6 @@ typedef typename Map::const_iterator MapConstIterator; | |
// Need call release when handle no longer needed | ||
const Handle *get(const KEY& key) | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
MapConstIterator it = cache_map_.find(key); | ||
|
||
if (it != cache_map_.end()) | ||
|
@@ -183,8 +171,6 @@ typedef typename Map::const_iterator MapConstIterator; | |
Handle *e = new Handle(key, value); | ||
|
||
e->ref = 1; | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
|
||
size_++; | ||
e->in_cache = true; | ||
e->ref++; | ||
|
@@ -216,7 +202,6 @@ typedef typename Map::const_iterator MapConstIterator; | |
// delete from cache, deleter delay called when all inuse-handle release. | ||
void del(const KEY& key) | ||
{ | ||
std::lock_guard<std::mutex> lock(mutex_); | ||
MapConstIterator it = cache_map_.find(key); | ||
|
||
if (it != cache_map_.end()) | ||
|
@@ -280,7 +265,6 @@ typedef typename Map::const_iterator MapConstIterator; | |
unref(e); | ||
} | ||
|
||
std::mutex mutex_; | ||
size_t max_size_; | ||
size_t size_; | ||
|
||
|
b1db18c
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's the comparison: https://github.com/chanchann/workflow_annotation/blob/main/src_analysis/10_cache_lock.md