Skip to content

Commit

Permalink
ObjCache V2 remove reflock
Browse files Browse the repository at this point in the history
  • Loading branch information
Coldwings committed Aug 12, 2024
1 parent daa6a18 commit 9416a1f
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 28 deletions.
29 changes: 2 additions & 27 deletions common/objectcachev2.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,6 @@ class ObjectCacheV2 {
struct Box : public intrusive_list_node<Box> {
const K key;
std::shared_ptr<V> ref;
// protect ref with mutex
// If multiple threads of execution access the same shared_ptr object
// without synchronization and any of those accesses uses a non-const
// member function of shared_ptr then a data race will occur
// DTOR of V may cause photon thread yield.
photon::mutex reflock;
// prevent create multiple time when borrow
photon::spinlock createlock;
// create timestamp, for cool-down of borrow
Expand All @@ -58,15 +52,14 @@ class ObjectCacheV2 {
: key(std::forward<KeyType>(key)), ref(nullptr) {}

std::shared_ptr<V> update(std::shared_ptr<V> r, uint64_t ts = 0) {
SCOPED_LOCK(reflock);
lastcreate = ts;
std::swap(r, ref);
r = std::atomic_exchange(&ref, r);
return r;
}
std::shared_ptr<V> reset(uint64_t ts = 0) {
return update({nullptr}, ts);
}
std::shared_ptr<V> reader() { return ref; }
std::shared_ptr<V> reader() { return std::atomic_load(&ref); }

void acquire() {
timestamp = photon::now;
Expand Down Expand Up @@ -116,11 +109,7 @@ class ObjectCacheV2 {
return *box;
}
uint64_t __expire() {
#if __cplusplus < 201703L
std::vector<std::shared_ptr<V>> to_release;
#else
intrusive_list<Box> to_release;
#endif
uint64_t now = photon::now;
uint64_t reclaim_before = photon::sat_sub(now, lifespan);
{
Expand All @@ -136,27 +125,13 @@ class ObjectCacheV2 {
if (x->rc == 0) {
// make vector holds those shared_ptr
// prevent object destroy in critical zone
#if __cplusplus < 201703L
to_release.push_back(x->reset());
map.erase(*x);
#else
// Here is a hacked implementation
auto node = map.extract(*x);
to_release.push_back(&node.value());
/// node_handle should be just key&val pointer
// Force make it empty can prevent destroy object
// when node_handle goes to destroy
memset((void*)&node, 0, sizeof(node));
#endif
}
x = lru_list.front();
}
}
#if __cplusplus < 201703L
to_release.clear();
#else
to_release.delete_all();
#endif
return 0;
}

Expand Down
1 change: 0 additions & 1 deletion common/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ target_link_libraries(test-objcache PRIVATE photon_shared)
add_test(NAME test-objcache COMMAND $<TARGET_FILE:test-objcache>)

add_executable(perf-objcache perf_objcache.cpp)
set_target_properties(perf-objcache PROPERTIES CXX_STANDARD 17)
target_link_libraries(perf-objcache PRIVATE photon_shared)
add_test(NAME perf-objcache COMMAND $<TARGET_FILE:test-objcache>)

Expand Down

0 comments on commit 9416a1f

Please sign in to comment.