]>
git.proxmox.com Git - ceph.git/blob - ceph/src/common/item_history.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
11 Keep a history of item values so that readers can dereference the pointer to
12 the latest value and continue using it as long as they want. This container
13 is only appropriate for values that are updated a handful of times over their
16 There is a prune() method to throw out old values, but it should only be used
17 if the caller has some way of knowing all readers are done.
22 class mutable_item_history
{
29 mutable_item_history() {
30 history
.emplace_back(T());
31 current
= &history
.back();
34 // readers are lock-free
35 const T
& operator*() const {
38 const T
*operator->() const {
42 // non-const variants (be careful!)
50 // writes are serialized
51 const T
& operator=(const T
& other
) {
52 std::lock_guard
l(lock
);
53 history
.push_back(other
);
54 current
= &history
.back();
59 // note: this is not necessarily thread-safe wrt readers
60 std::lock_guard
l(lock
);
61 while (history
.size() > 1) {
68 class safe_item_history
{
76 history
.emplace_back(T());
77 current
= &history
.back();
80 // readers are lock-free
81 const T
& operator*() const {
84 const T
*operator->() const {
88 // writes are serialized
89 const T
& operator=(const T
& other
) {
90 std::lock_guard
l(lock
);
91 history
.push_back(other
);
92 current
= &history
.back();
97 // note: this is not necessarily thread-safe wrt readers
98 std::lock_guard
l(lock
);
99 while (history
.size() > 1) {