diff --git a/common/objectcachev2.h b/common/objectcachev2.h index 4fdcfcce..20e7422a 100644 --- a/common/objectcachev2.h +++ b/common/objectcachev2.h @@ -37,7 +37,6 @@ class ObjectCacheV2 { const K key; std::shared_ptr ref; photon::spinlock boxlock; - photon::spinlock reflock; uint64_t lastcreate = 0; uint64_t timestamp = 0; @@ -50,11 +49,12 @@ class ObjectCacheV2 { // other reader will get new one after updated std::shared_ptr update(V* val, uint64_t ts = 0, std::shared_ptr* newptr = nullptr) { - SCOPED_LOCK(box->reflock); auto r = std::shared_ptr(val); - if (newptr) *newptr = r; + if (newptr) + std::atomic_store(newptr, r); + *newptr = r; box->lastcreate = ts; - std::swap(r, box->ref); + r = std::atomic_exchange(&box->ref, r); return r; } @@ -79,8 +79,7 @@ class ObjectCacheV2 { Updater writer() { return Updater(this); } std::shared_ptr reader() { - SCOPED_LOCK(reflock); - return ref; + return std::atomic_load(&ref); } }; struct ItemHash { @@ -173,6 +172,7 @@ class ObjectCacheV2 { Borrow& operator=(Borrow&& rhs) { std::swap(_oc, rhs._oc); std::swap(_item, rhs._item); + _reader = std::atomic_exchange(&rhs._reader, _reader); std::swap(_reader, rhs._reader); std::swap(_recycle, rhs._recycle); return *this; diff --git a/common/test/perf_objcache.cpp b/common/test/perf_objcache.cpp index 257af4f1..71a7e558 100644 --- a/common/test/perf_objcache.cpp +++ b/common/test/perf_objcache.cpp @@ -40,7 +40,7 @@ void *task(void *arg) { for (const auto &x : k) { auto strx = std::to_string(x); auto b = oc->borrow(strx, [&strx] { - photon::thread_usleep(1 * 1000); + // photon::thread_usleep(1 * 1000); // LOG_INFO("CTOR `", photon::now); return new std::string(strx); }); @@ -64,7 +64,7 @@ void test_objcache(OC &oc, const char *name) { template