MXS-2005: Use std::mutex instead of Spinlock in log manager
Changed Spinlocks and SPINLOCKs to std::mutex. Store Stats inside a unique_ptr to avoid the copying of the lock.
This commit is contained in:
@ -25,7 +25,6 @@
|
|||||||
#include <maxscale/debug.h>
|
#include <maxscale/debug.h>
|
||||||
#include <maxscale/json_api.h>
|
#include <maxscale/json_api.h>
|
||||||
#include <maxscale/session.h>
|
#include <maxscale/session.h>
|
||||||
#include <maxscale/spinlock.hh>
|
|
||||||
#include <maxscale/utils.h>
|
#include <maxscale/utils.h>
|
||||||
|
|
||||||
#include "internal/logger.hh"
|
#include "internal/logger.hh"
|
||||||
@ -159,8 +158,7 @@ class MessageRegistryStats
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
MessageRegistryStats()
|
MessageRegistryStats()
|
||||||
: m_lock(SPINLOCK_INIT)
|
: m_first_ms(time_monotonic_ms())
|
||||||
, m_first_ms(time_monotonic_ms())
|
|
||||||
, m_last_ms(0)
|
, m_last_ms(0)
|
||||||
, m_count(0)
|
, m_count(0)
|
||||||
{
|
{
|
||||||
@ -172,7 +170,7 @@ public:
|
|||||||
|
|
||||||
uint64_t now_ms = time_monotonic_ms();
|
uint64_t now_ms = time_monotonic_ms();
|
||||||
|
|
||||||
spinlock_acquire(&m_lock);
|
std::lock_guard<std::mutex> guard(m_lock);
|
||||||
|
|
||||||
++m_count;
|
++m_count;
|
||||||
|
|
||||||
@ -226,13 +224,11 @@ public:
|
|||||||
|
|
||||||
m_last_ms = now_ms;
|
m_last_ms = now_ms;
|
||||||
|
|
||||||
spinlock_release(&m_lock);
|
|
||||||
|
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
SPINLOCK m_lock;
|
std::mutex m_lock;
|
||||||
uint64_t m_first_ms; /** The time when the error was logged the first time in this window. */
|
uint64_t m_first_ms; /** The time when the error was logged the first time in this window. */
|
||||||
uint64_t m_last_ms; /** The time when the error was logged the last time. */
|
uint64_t m_last_ms; /** The time when the error was logged the last time. */
|
||||||
size_t m_count; /** How many times the error has been reported within this window. */
|
size_t m_count; /** How many times the error has been reported within this window. */
|
||||||
@ -288,22 +284,12 @@ public:
|
|||||||
|
|
||||||
Stats& get_stats(const Key& key)
|
Stats& get_stats(const Key& key)
|
||||||
{
|
{
|
||||||
mxs::SpinLockGuard guard(m_lock);
|
std::lock_guard<std::mutex> guard(m_lock);
|
||||||
|
return m_registry[key];
|
||||||
auto i = m_registry.find(key);
|
|
||||||
|
|
||||||
if (i == m_registry.end())
|
|
||||||
{
|
|
||||||
Stats stats;
|
|
||||||
|
|
||||||
i = m_registry.insert(std::make_pair(key, stats)).first;
|
|
||||||
}
|
|
||||||
|
|
||||||
return i->second;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
mxs::SpinLock m_lock;
|
std::mutex m_lock;
|
||||||
std::unordered_map<Key, Stats> m_registry;
|
std::unordered_map<Key, Stats> m_registry;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user