mirror of
https://github.com/ton-blockchain/ton
synced 2025-03-09 15:40:10 +00:00
integrating the existing state of TON Storage / TON Payments / CPS Fift development branches
This commit is contained in:
parent
040df63c98
commit
4e2624459b
153 changed files with 10760 additions and 1695 deletions
48
storage/SharedState.h
Normal file
48
storage/SharedState.h
Normal file
|
@ -0,0 +1,48 @@
|
|||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
#include "td/utils/MovableValue.h"
|
||||
#include <memory>
|
||||
|
||||
namespace td {
|
||||
template <class T>
|
||||
class SharedState {
|
||||
public:
|
||||
friend class Guard;
|
||||
class Guard {
|
||||
public:
|
||||
Guard(Guard &&) = default;
|
||||
Guard(SharedState<T> *self) : self(self) {
|
||||
CHECK(!self->data_->is_locked.exchange(true));
|
||||
}
|
||||
~Guard() {
|
||||
if (self.get()) {
|
||||
CHECK(self.get()->data_->is_locked.exchange(false));
|
||||
}
|
||||
}
|
||||
T *get() {
|
||||
return &self.get()->data_->data;
|
||||
}
|
||||
T *operator->() {
|
||||
return get();
|
||||
}
|
||||
|
||||
private:
|
||||
td::MovableValue<SharedState<T> *> self;
|
||||
};
|
||||
|
||||
auto lock() {
|
||||
return Guard{this};
|
||||
}
|
||||
auto unsafe() {
|
||||
return &data_->data;
|
||||
}
|
||||
|
||||
private:
|
||||
struct Data {
|
||||
std::atomic<bool> is_locked{};
|
||||
T data;
|
||||
};
|
||||
std::shared_ptr<Data> data_{std::make_shared<Data>()};
|
||||
};
|
||||
} // namespace td
|
Loading…
Add table
Add a link
Reference in a new issue