Skip to content

Commit

Permalink
Make borrow/update always get new created object
Browse files Browse the repository at this point in the history
Signed-off-by: Coldwings <coldwings@me.com>
  • Loading branch information
Coldwings committed Jul 9, 2024
1 parent 090e442 commit e8bf383
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions common/objectcachev2.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,12 @@ class ObjectCacheV2 {

// Returned RefPtr is the old one
// other reader will get new one after updated
std::shared_ptr<V> update(V* val, uint64_t ts = 0) {
while (box->reflock.try_lock() != 0) {
photon::thread_yield();
}
DEFER(box->reflock.unlock());
std::shared_ptr<V> update(V* val, uint64_t ts = 0,
std::shared_ptr<V>* newptr = nullptr) {
SCOPED_LOCK(box->reflock);
auto r = std::shared_ptr<V>(val);
if (ts) box->lastcreate = ts;
if (newptr) *newptr = r;
box->lastcreate = ts;
std::swap(r, box->ref);
return r;
}
Expand Down Expand Up @@ -103,9 +102,11 @@ class ObjectCacheV2 {
uint64_t lifespan;
photon::Timer _timer;

void __update(Box* item, V* val) {
reclaim_queue.template send<PhotonPause>(
new std::shared_ptr<V>(item->writer().update(val, photon::now)));
std::shared_ptr<V> __update(Box* item, V* val) {
std::shared_ptr<V> ret;
reclaim_queue.template send<PhotonPause>(new std::shared_ptr<V>(
item->writer().update(val, photon::now, &ret)));
return ret;
}

template <typename KeyType>
Expand Down Expand Up @@ -199,19 +200,19 @@ class ObjectCacheV2 {
while (!r) {
if (item->boxlock.try_lock() == 0) {
DEFER(item->boxlock.unlock());
auto lr = item->reader();
if (!lr) {
r = item->reader();
if (!r) {
if (photon::sat_add(item->lastcreate, cooldown) <=
photon::now) {
__update(item, ctor());
r = __update(item, ctor());
}
return Borrow(this, item, item->reader());
return Borrow(this, item, std::move(r));
}
}
photon::thread_yield();
r = item->reader();
}
return Borrow(this, item, item->reader());
return Borrow(this, item, std::move(r));
}

template <typename KeyType>
Expand All @@ -222,8 +223,8 @@ class ObjectCacheV2 {
template <typename KeyType, typename Ctor>
Borrow update(KeyType&& key, Ctor&& ctor) {
auto item = __find_or_create_item(std::forward<KeyType>(key));
__update(item, ctor());
return Borrow(this, item, item->reader());
auto r = __update(item, ctor());
return Borrow(this, item, std::move(r));
}

ObjectCacheV2(uint64_t lifespan)
Expand Down

0 comments on commit e8bf383

Please sign in to comment.