Merge "Add Lock to ValueMetric"
This commit is contained in:
@@ -6,6 +6,7 @@
|
|||||||
#define WVCDM_METRICS_VALUE_METRIC_H_
|
#define WVCDM_METRICS_VALUE_METRIC_H_
|
||||||
|
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
#include "metrics.pb.h"
|
#include "metrics.pb.h"
|
||||||
@@ -46,6 +47,7 @@ class ValueMetric {
|
|||||||
|
|
||||||
// Record the value of the metric.
|
// Record the value of the metric.
|
||||||
void Record(const T &value) {
|
void Record(const T &value) {
|
||||||
|
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||||
value_ = value;
|
value_ = value;
|
||||||
has_value_ = true;
|
has_value_ = true;
|
||||||
has_error_ = false;
|
has_error_ = false;
|
||||||
@@ -53,19 +55,33 @@ class ValueMetric {
|
|||||||
|
|
||||||
// Set the error code if an error was encountered.
|
// Set the error code if an error was encountered.
|
||||||
void SetError(int error_code) {
|
void SetError(int error_code) {
|
||||||
|
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||||
error_code_ = error_code;
|
error_code_ = error_code;
|
||||||
has_value_ = false;
|
has_value_ = false;
|
||||||
has_error_ = true;
|
has_error_ = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool HasValue() const { return has_value_; }
|
bool HasValue() const {
|
||||||
const T &GetValue() const { return value_; }
|
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||||
|
return has_value_;
|
||||||
|
}
|
||||||
|
const T &GetValue() const {
|
||||||
|
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||||
|
return value_;
|
||||||
|
}
|
||||||
|
|
||||||
bool HasError() const { return has_error_; }
|
bool HasError() const {
|
||||||
int GetError() const { return error_code_; }
|
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||||
|
return has_error_;
|
||||||
|
}
|
||||||
|
int GetError() const {
|
||||||
|
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||||
|
return error_code_;
|
||||||
|
}
|
||||||
|
|
||||||
// Clears the indicators that the metric or error was set.
|
// Clears the indicators that the metric or error was set.
|
||||||
void Clear() {
|
void Clear() {
|
||||||
|
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||||
has_value_ = false;
|
has_value_ = false;
|
||||||
has_error_ = false;
|
has_error_ = false;
|
||||||
}
|
}
|
||||||
@@ -73,6 +89,7 @@ class ValueMetric {
|
|||||||
// Returns a new ValueMetric proto containing the metric value or the
|
// Returns a new ValueMetric proto containing the metric value or the
|
||||||
// error code. If neither the error or value are set, it returns nullptr.
|
// error code. If neither the error or value are set, it returns nullptr.
|
||||||
drm_metrics::ValueMetric *ToProto() const {
|
drm_metrics::ValueMetric *ToProto() const {
|
||||||
|
std::unique_lock<std::mutex> lock(internal_lock_);
|
||||||
if (has_error_) {
|
if (has_error_) {
|
||||||
drm_metrics::ValueMetric *value_proto = new drm_metrics::ValueMetric;
|
drm_metrics::ValueMetric *value_proto = new drm_metrics::ValueMetric;
|
||||||
value_proto->set_error_code(error_code_);
|
value_proto->set_error_code(error_code_);
|
||||||
@@ -91,6 +108,16 @@ class ValueMetric {
|
|||||||
int error_code_;
|
int error_code_;
|
||||||
bool has_error_;
|
bool has_error_;
|
||||||
bool has_value_;
|
bool has_value_;
|
||||||
|
|
||||||
|
/*
|
||||||
|
* This locks the internal state of the value metric to ensure safety
|
||||||
|
* across multiple threads preventing the caller from worrying about
|
||||||
|
* locking.
|
||||||
|
*
|
||||||
|
* This field must be declared mutable because the lock must be takeable even
|
||||||
|
* in const methods.
|
||||||
|
*/
|
||||||
|
mutable std::mutex internal_lock_;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace metrics
|
} // namespace metrics
|
||||||
|
|||||||
Reference in New Issue
Block a user