mirror of
https://github.com/Zygo/bees.git
synced 2025-05-17 21:35:45 +02:00
bees: clean up statistics class
Some whitespace fixes. Remove some duplicate code. Don't lock two BeesStats objects in the - operator method. Get the locking for T& at(const K&) right to avoid locking a mutex recursively. Make the non-const version of the function private. Signed-off-by: Zygo Blaxell <bees@furryterror.org>
This commit is contained in:
parent
db8ea92133
commit
5713fcd770
18
src/bees.cc
18
src/bees.cc
@ -86,11 +86,10 @@ thread_local string BeesNote::tl_name;
|
|||||||
BeesNote::~BeesNote()
|
BeesNote::~BeesNote()
|
||||||
{
|
{
|
||||||
tl_next = m_prev;
|
tl_next = m_prev;
|
||||||
|
unique_lock<mutex> lock(s_mutex);
|
||||||
if (tl_next) {
|
if (tl_next) {
|
||||||
unique_lock<mutex> lock(s_mutex);
|
|
||||||
s_status[gettid()] = tl_next;
|
s_status[gettid()] = tl_next;
|
||||||
} else {
|
} else {
|
||||||
unique_lock<mutex> lock(s_mutex);
|
|
||||||
s_status.erase(gettid());
|
s_status.erase(gettid());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -203,11 +202,10 @@ template <class T>
|
|||||||
T&
|
T&
|
||||||
BeesStatTmpl<T>::at(string idx)
|
BeesStatTmpl<T>::at(string idx)
|
||||||
{
|
{
|
||||||
unique_lock<mutex> lock(m_mutex);
|
if (!m_stats_map.count(idx)) {
|
||||||
if (!m_stats_map.count(idx)) {
|
|
||||||
m_stats_map[idx] = 0;
|
m_stats_map[idx] = 0;
|
||||||
}
|
}
|
||||||
return m_stats_map[idx];
|
return m_stats_map[idx];
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
@ -215,7 +213,8 @@ T
|
|||||||
BeesStatTmpl<T>::at(string idx) const
|
BeesStatTmpl<T>::at(string idx) const
|
||||||
{
|
{
|
||||||
unique_lock<mutex> lock(m_mutex);
|
unique_lock<mutex> lock(m_mutex);
|
||||||
return m_stats_map.at(idx);
|
auto rv = m_stats_map.at(idx);
|
||||||
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
template <class T>
|
template <class T>
|
||||||
@ -223,7 +222,7 @@ void
|
|||||||
BeesStatTmpl<T>::add_count(string idx, size_t amount)
|
BeesStatTmpl<T>::add_count(string idx, size_t amount)
|
||||||
{
|
{
|
||||||
unique_lock<mutex> lock(m_mutex);
|
unique_lock<mutex> lock(m_mutex);
|
||||||
if (!m_stats_map.count(idx)) {
|
if (!m_stats_map.count(idx)) {
|
||||||
m_stats_map[idx] = 0;
|
m_stats_map[idx] = 0;
|
||||||
}
|
}
|
||||||
m_stats_map.at(idx) += amount;
|
m_stats_map.at(idx) += amount;
|
||||||
@ -255,14 +254,17 @@ BeesStats
|
|||||||
BeesStats::operator-(const BeesStats &that) const
|
BeesStats::operator-(const BeesStats &that) const
|
||||||
{
|
{
|
||||||
if (&that == this) return BeesStats();
|
if (&that == this) return BeesStats();
|
||||||
|
|
||||||
unique_lock<mutex> this_lock(m_mutex);
|
unique_lock<mutex> this_lock(m_mutex);
|
||||||
BeesStats this_copy;
|
BeesStats this_copy;
|
||||||
this_copy.m_stats_map = m_stats_map;
|
this_copy.m_stats_map = m_stats_map;
|
||||||
|
this_lock.unlock();
|
||||||
|
|
||||||
unique_lock<mutex> that_lock(that.m_mutex);
|
unique_lock<mutex> that_lock(that.m_mutex);
|
||||||
BeesStats that_copy;
|
BeesStats that_copy;
|
||||||
that_copy.m_stats_map = that.m_stats_map;
|
that_copy.m_stats_map = that.m_stats_map;
|
||||||
this_lock.unlock();
|
|
||||||
that_lock.unlock();
|
that_lock.unlock();
|
||||||
|
|
||||||
for (auto i : that.m_stats_map) {
|
for (auto i : that.m_stats_map) {
|
||||||
if (i.second != 0) {
|
if (i.second != 0) {
|
||||||
this_copy.at(i.first) -= i.second;
|
this_copy.at(i.first) -= i.second;
|
||||||
|
@ -155,12 +155,12 @@ class BeesStatTmpl {
|
|||||||
map<string, T> m_stats_map;
|
map<string, T> m_stats_map;
|
||||||
mutable mutex m_mutex;
|
mutable mutex m_mutex;
|
||||||
|
|
||||||
|
T& at(string idx);
|
||||||
public:
|
public:
|
||||||
BeesStatTmpl() = default;
|
BeesStatTmpl() = default;
|
||||||
BeesStatTmpl(const BeesStatTmpl &that);
|
BeesStatTmpl(const BeesStatTmpl &that);
|
||||||
BeesStatTmpl &operator=(const BeesStatTmpl &that);
|
BeesStatTmpl &operator=(const BeesStatTmpl &that);
|
||||||
void add_count(string idx, size_t amount = 1);
|
void add_count(string idx, size_t amount = 1);
|
||||||
T& at(string idx);
|
|
||||||
T at(string idx) const;
|
T at(string idx) const;
|
||||||
|
|
||||||
friend ostream& operator<< <>(ostream &os, const BeesStatTmpl<T> &bs);
|
friend ostream& operator<< <>(ostream &os, const BeesStatTmpl<T> &bs);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user