memgraph/include/mvcc/version.hpp

45 lines
956 B
C++
Raw Normal View History

#pragma once
2015-07-31 18:31:08 +08:00
#include <atomic>
namespace mvcc
{
2015-07-31 18:31:08 +08:00
template <class T>
class Version
{
public:
Version() = default;
Version(T *older) : older(older) {}
2015-07-31 18:31:08 +08:00
~Version() { delete older.load(std::memory_order_seq_cst); }
// return a pointer to an older version stored in this record
T *next(std::memory_order order = std::memory_order_seq_cst)
{
return older.load(order);
}
2015-07-31 18:31:08 +08:00
const T *next(std::memory_order order = std::memory_order_seq_cst) const
2015-07-31 18:31:08 +08:00
{
return older.load(order);
2015-07-31 18:31:08 +08:00
}
// set the older version of this record
void next(T *value, std::memory_order order = std::memory_order_seq_cst)
2015-07-31 18:31:08 +08:00
{
older.store(value, order);
2015-07-31 18:31:08 +08:00
}
// sets if as expected
bool cas(T *expected, T *set,
std::memory_order order = std::memory_order_seq_cst)
{
return older.compare_exchange_strong(expected, set, order);
}
2015-07-31 18:31:08 +08:00
private:
std::atomic<T *> older{nullptr};
2015-07-31 18:31:08 +08:00
};
}