made ActorLineage thread safe

This commit is contained in:
Markus Pilman 2021-04-12 09:43:45 -06:00
parent 2efcf8efec
commit 13e00e8408
1 changed files with 14 additions and 2 deletions

View File

@ -42,6 +42,7 @@
#include <utility> #include <utility>
#include <algorithm> #include <algorithm>
#include <memory> #include <memory>
#include <mutex>
#include "flow/Platform.h" #include "flow/Platform.h"
#include "flow/FastAlloc.h" #include "flow/FastAlloc.h"
@ -453,14 +454,23 @@ struct ActorLineage : ReferenceCounted<ActorLineage> {
private: private:
std::unordered_map<std::string_view, LineagePropertiesBase*> properties; std::unordered_map<std::string_view, LineagePropertiesBase*> properties;
Reference<ActorLineage> parent; Reference<ActorLineage> parent;
mutable std::mutex mutex;
using Lock = std::unique_lock<std::mutex>;
public: public:
ActorLineage(); ActorLineage();
~ActorLineage(); ~ActorLineage();
bool isRoot() const { return parent.getPtr() == nullptr; } bool isRoot() const {
void makeRoot() { parent.clear(); } Lock _{ mutex };
return parent.getPtr() == nullptr;
}
void makeRoot() {
Lock _{ mutex };
parent.clear();
}
template <class T, class V> template <class T, class V>
V& modify(V T::*member) { V& modify(V T::*member) {
Lock _{ mutex };
auto& res = properties[T::name]; auto& res = properties[T::name];
if (!res) { if (!res) {
res = new T{}; res = new T{};
@ -470,6 +480,7 @@ public:
} }
template <class T, class V> template <class T, class V>
std::optional<V> get(V T::*member) const { std::optional<V> get(V T::*member) const {
Lock _{ mutex };
auto current = this; auto current = this;
while (current != nullptr) { while (current != nullptr) {
auto iter = current->properties.find(T::name); auto iter = current->properties.find(T::name);
@ -485,6 +496,7 @@ public:
} }
template <class T, class V> template <class T, class V>
std::vector<V> stack(V T::*member) const { std::vector<V> stack(V T::*member) const {
Lock _{ mutex };
auto current = this; auto current = this;
std::vector<V> res; std::vector<V> res;
while (current != nullptr) { while (current != nullptr) {