1
0
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:
Zygo Blaxell 2017-01-22 21:50:22 -05:00
parent db8ea92133
commit 5713fcd770
2 changed files with 11 additions and 9 deletions

View File

@ -86,11 +86,10 @@ thread_local string BeesNote::tl_name;
BeesNote::~BeesNote() BeesNote::~BeesNote()
{ {
tl_next = m_prev; tl_next = m_prev;
if (tl_next) {
unique_lock<mutex> lock(s_mutex); unique_lock<mutex> lock(s_mutex);
if (tl_next) {
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,7 +202,6 @@ 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;
} }
@ -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>
@ -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;

View File

@ -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);