From dcd2c021a5ac91d38187d397914e5f51e2fc8819 Mon Sep 17 00:00:00 2001 From: Horst Schirmeier Date: Tue, 10 Sep 2013 18:16:17 +0200 Subject: [PATCH] util: global lock for certain MySQL operations Even the reentrant libmysqlclient_r has some non-threadsafe operations, which need to be protected by a global mutex. Change-Id: I444e42f82cf982a6c8f8f2596e8991d0a5009b28 --- src/core/util/Database.cc | 11 +++++++++++ src/core/util/Database.hpp | 1 + 2 files changed, 12 insertions(+) diff --git a/src/core/util/Database.cc b/src/core/util/Database.cc index b723be0c..dc0b8073 100644 --- a/src/core/util/Database.cc +++ b/src/core/util/Database.cc @@ -8,7 +8,15 @@ static fail::Logger LOG("Database", true); using namespace fail; +#ifndef __puma +boost::mutex Database::m_global_lock; +#endif + Database::Database(const std::string &username, const std::string &host, const std::string &database) { +#ifndef __puma + boost::lock_guard guard(m_global_lock); +#endif + handle = mysql_init(0); last_result = 0; mysql_options(handle, MYSQL_READ_DEFAULT_FILE, "~/.my.cnf"); @@ -26,6 +34,9 @@ Database::~Database() // flush cached INSERTs if available insert_multiple(); +#ifndef __puma + boost::lock_guard guard(m_global_lock); +#endif mysql_close(handle); } diff --git a/src/core/util/Database.hpp b/src/core/util/Database.hpp index ff234f7f..66c0710b 100644 --- a/src/core/util/Database.hpp +++ b/src/core/util/Database.hpp @@ -23,6 +23,7 @@ namespace fail { MYSQL_RES *last_result; // !< Used for mysql_result_free #ifndef __puma boost::mutex m_handle_lock; + static boost::mutex m_global_lock; #endif std::string m_insertquery; std::vector m_insertquery_values;