From 9416a1f11d3b23065f71aad8c1d127ca482253d5 Mon Sep 17 00:00:00 2001 From: Coldwings Date: Mon, 12 Aug 2024 10:43:52 +0800 Subject: [PATCH] ObjCache V2 remove reflock --- common/objectcachev2.h | 29 ++--------------------------- common/test/CMakeLists.txt | 1 - 2 files changed, 2 insertions(+), 28 deletions(-) diff --git a/common/objectcachev2.h b/common/objectcachev2.h index 6c1886ed..c8964c45 100644 --- a/common/objectcachev2.h +++ b/common/objectcachev2.h @@ -37,12 +37,6 @@ class ObjectCacheV2 { struct Box : public intrusive_list_node { const K key; std::shared_ptr 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 @@ -58,15 +52,14 @@ class ObjectCacheV2 { : key(std::forward(key)), ref(nullptr) {} std::shared_ptr update(std::shared_ptr 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 reset(uint64_t ts = 0) { return update({nullptr}, ts); } - std::shared_ptr reader() { return ref; } + std::shared_ptr reader() { return std::atomic_load(&ref); } void acquire() { timestamp = photon::now; @@ -116,11 +109,7 @@ class ObjectCacheV2 { return *box; } uint64_t __expire() { -#if __cplusplus < 201703L std::vector> to_release; -#else - intrusive_list to_release; -#endif uint64_t now = photon::now; uint64_t reclaim_before = photon::sat_sub(now, lifespan); { @@ -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; } diff --git a/common/test/CMakeLists.txt b/common/test/CMakeLists.txt index 6fe657bc..1a30526c 100644 --- a/common/test/CMakeLists.txt +++ b/common/test/CMakeLists.txt @@ -6,7 +6,6 @@ target_link_libraries(test-objcache PRIVATE photon_shared) add_test(NAME test-objcache COMMAND $) 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 $)