Add locking to FlipperState

Summary: FlipperState may be updated from multiple threads, so it needs synchronization. Note the comments in the diff about why we drop the lock before calling out to the update listener -- this can be changed if necessary in the future.

Reviewed By: jknoxville

Differential Revision: D18095471

fbshipit-source-id: 95d558394ae1a9b7583e5a61969e1eeda6448cff
This commit is contained in:
Scott Wolchok
2019-10-24 09:28:27 -07:00
committed by Facebook Github Bot
parent f00f0ad4b9
commit 8dbf60e82e
2 changed files with 54 additions and 23 deletions

View File

@@ -7,10 +7,11 @@
#pragma once
#include <map>
#include <memory>
#include <mutex>
#include <string>
#include <vector>
#include <map>
class FlipperStep;
class FlipperStateUpdateListener;
@@ -21,20 +22,23 @@ namespace flipper {
enum State { success, in_progress, failed };
class StateElement {
public:
StateElement(std::string name, State state): name_(name), state_(state) {};
public:
StateElement(std::string name, State state) : name_(name), state_(state){};
std::string name_;
State state_;
};
}
}
} // namespace flipper
} // namespace facebook
class FlipperState {
friend FlipperStep;
public:
FlipperState();
// Update listeners are responsible for their own
// synchronization. There is no guarantee about which thread they
// may be called on.
void setUpdateListener(std::shared_ptr<FlipperStateUpdateListener>);
std::string getState();
std::vector<facebook::flipper::StateElement> getStateElements();
@@ -50,6 +54,7 @@ class FlipperState {
void failed(std::string, std::string);
void started(std::string);
std::mutex mutex; // Protects all our member variables.
std::shared_ptr<FlipperStateUpdateListener> mListener = nullptr;
std::string logs;
std::vector<std::string> insertOrder;