MXS-2067: Replace most SPINLOCKs
Replaced SPINLOCK with std::mutex where possible, leaving out the more complex cases. The big offenders remaining are the binlogrouter and the gateway.cc OpenSSL locks.
This commit is contained in:
@ -409,11 +409,10 @@ void mxs_crypt(const char* password, const char* salt, char* output)
|
||||
char* pw = crypt_r(password, salt, &cdata);
|
||||
snprintf(output, MXS_CRYPT_SIZE, "%s", pw);
|
||||
#else
|
||||
static SPINLOCK mxs_crypt_lock = SPINLOCK_INIT;
|
||||
spinlock_acquire(&mxs_crypt_lock);
|
||||
static std::mutex mxs_crypt_lock;
|
||||
std::lock_guard<std::mutex> guard(mxs_crypt_lock);
|
||||
char* pw = crypt(password, salt);
|
||||
snprintf(output, MXS_CRYPT_SIZE, "%s", pw);
|
||||
spinlock_release(&mxs_crypt_lock);
|
||||
#endif
|
||||
}
|
||||
|
||||
|
@ -960,17 +960,16 @@ static bool config_load_dir(const char* dir, DUPLICATE_CONTEXT* dcontext, CONFIG
|
||||
// Since there is no way to pass userdata to the callback, we need to store
|
||||
// the current context into a static variable. Consequently, we need lock.
|
||||
// Should not matter since config_load() is called once at startup.
|
||||
static SPINLOCK lock = SPINLOCK_INIT;
|
||||
static std::mutex lock;
|
||||
std::lock_guard<std::mutex> guard(lock);
|
||||
|
||||
int nopenfd = 5; // Maximum concurrently opened directory descriptors
|
||||
|
||||
spinlock_acquire(&lock);
|
||||
current_dcontext = dcontext;
|
||||
current_ccontext = ccontext;
|
||||
int rv = nftw(dir, config_cb, nopenfd, FTW_PHYS);
|
||||
current_ccontext = NULL;
|
||||
current_dcontext = NULL;
|
||||
spinlock_release(&lock);
|
||||
|
||||
return rv == 0;
|
||||
}
|
||||
|
@ -49,7 +49,7 @@ typedef struct modulecmd_domain
|
||||
|
||||
/** The global list of registered domains */
|
||||
static MODULECMD_DOMAIN* modulecmd_domains = NULL;
|
||||
static SPINLOCK modulecmd_lock = SPINLOCK_INIT;
|
||||
static std::mutex modulecmd_lock;
|
||||
|
||||
static inline void prepare_error()
|
||||
{
|
||||
@ -434,7 +434,7 @@ bool modulecmd_register_command(const char* domain,
|
||||
{
|
||||
reset_error();
|
||||
bool rval = false;
|
||||
spinlock_acquire(&modulecmd_lock);
|
||||
std::lock_guard<std::mutex> guard(modulecmd_lock);
|
||||
|
||||
MODULECMD_DOMAIN* dm = get_or_create_domain(domain);
|
||||
|
||||
@ -464,8 +464,6 @@ bool modulecmd_register_command(const char* domain,
|
||||
}
|
||||
}
|
||||
|
||||
spinlock_release(&modulecmd_lock);
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
||||
@ -476,7 +474,7 @@ const MODULECMD* modulecmd_find_command(const char* domain, const char* identifi
|
||||
const char* effective_domain = mxs_module_get_effective_name(domain);
|
||||
|
||||
MODULECMD* rval = NULL;
|
||||
spinlock_acquire(&modulecmd_lock);
|
||||
std::lock_guard<std::mutex> guard(modulecmd_lock);
|
||||
|
||||
for (MODULECMD_DOMAIN* dm = modulecmd_domains; dm; dm = dm->next)
|
||||
{
|
||||
@ -494,8 +492,6 @@ const MODULECMD* modulecmd_find_command(const char* domain, const char* identifi
|
||||
}
|
||||
}
|
||||
|
||||
spinlock_release(&modulecmd_lock);
|
||||
|
||||
if (rval == NULL)
|
||||
{
|
||||
modulecmd_set_error("Command not found: %s::%s", domain, identifier);
|
||||
@ -641,7 +637,7 @@ bool modulecmd_foreach(const char* domain_re,
|
||||
{
|
||||
bool rval = true;
|
||||
bool stop = false;
|
||||
spinlock_acquire(&modulecmd_lock);
|
||||
std::lock_guard<std::mutex> guard(modulecmd_lock);
|
||||
|
||||
for (MODULECMD_DOMAIN* domain = modulecmd_domains; domain && rval && !stop; domain = domain->next)
|
||||
{
|
||||
@ -686,7 +682,6 @@ bool modulecmd_foreach(const char* domain_re,
|
||||
}
|
||||
}
|
||||
|
||||
spinlock_release(&modulecmd_lock);
|
||||
return rval;
|
||||
}
|
||||
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include <strings.h>
|
||||
|
||||
#include <iterator>
|
||||
#include <mutex>
|
||||
|
||||
#include <maxscale/alloc.h>
|
||||
#include <maxscale/buffer.h>
|
||||
@ -30,7 +31,6 @@
|
||||
#include <maxscale/mysql_utils.h>
|
||||
|
||||
/** These are used when converting MySQL wildcards to regular expressions */
|
||||
static SPINLOCK re_lock = SPINLOCK_INIT;
|
||||
static bool pattern_init = false;
|
||||
static pcre2_code* re_percent = NULL;
|
||||
static pcre2_code* re_single = NULL;
|
||||
@ -1113,7 +1113,9 @@ int modutil_count_packets(GWBUF* buffer)
|
||||
*/
|
||||
void prepare_pcre2_patterns()
|
||||
{
|
||||
spinlock_acquire(&re_lock);
|
||||
static std::mutex re_lock;
|
||||
std::lock_guard<std::mutex> guard(re_lock);
|
||||
|
||||
if (!pattern_init)
|
||||
{
|
||||
int err;
|
||||
@ -1158,7 +1160,6 @@ void prepare_pcre2_patterns()
|
||||
re_escape = NULL;
|
||||
}
|
||||
}
|
||||
spinlock_release(&re_lock);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -26,6 +26,7 @@
|
||||
#include <zlib.h>
|
||||
#include <sys/stat.h>
|
||||
#include <vector>
|
||||
#include <mutex>
|
||||
|
||||
#include <maxscale/alloc.h>
|
||||
#include <maxbase/atomic.hh>
|
||||
@ -80,7 +81,7 @@ const char CN_SCRIPT[] = "script";
|
||||
const char CN_SCRIPT_TIMEOUT[] = "script_timeout";
|
||||
|
||||
static MXS_MONITOR* allMonitors = NULL;
|
||||
static SPINLOCK monLock = SPINLOCK_INIT;
|
||||
static std::mutex monLock;
|
||||
|
||||
static void monitor_server_free_all(MXS_MONITORED_SERVER* servers);
|
||||
static void remove_server_journal(MXS_MONITOR* monitor);
|
||||
@ -173,10 +174,9 @@ MXS_MONITOR* monitor_create(const char* name, const char* module, MXS_CONFIG_PAR
|
||||
return NULL;
|
||||
}
|
||||
|
||||
spinlock_acquire(&monLock);
|
||||
std::lock_guard<std::mutex> guard(monLock);
|
||||
mon->next = allMonitors;
|
||||
allMonitors = mon;
|
||||
spinlock_release(&monLock);
|
||||
|
||||
return mon;
|
||||
}
|
||||
@ -191,7 +191,8 @@ void monitor_destroy(MXS_MONITOR* mon)
|
||||
{
|
||||
MXS_MONITOR* ptr;
|
||||
|
||||
spinlock_acquire(&monLock);
|
||||
std::unique_lock<std::mutex> guard(monLock);
|
||||
|
||||
if (allMonitors == mon)
|
||||
{
|
||||
allMonitors = mon->next;
|
||||
@ -208,7 +209,9 @@ void monitor_destroy(MXS_MONITOR* mon)
|
||||
ptr->next = mon->next;
|
||||
}
|
||||
}
|
||||
spinlock_release(&monLock);
|
||||
|
||||
guard.unlock();
|
||||
|
||||
mon->api->destroyInstance(mon->instance);
|
||||
delete mon->disk_space_threshold;
|
||||
config_parameter_free(mon->parameters);
|
||||
@ -271,8 +274,8 @@ void monitor_start(MXS_MONITOR* monitor, const MXS_CONFIG_PARAMETER* params)
|
||||
void monitor_start_all()
|
||||
{
|
||||
MXS_MONITOR* ptr;
|
||||
std::lock_guard<std::mutex> guard(monLock);
|
||||
|
||||
spinlock_acquire(&monLock);
|
||||
ptr = allMonitors;
|
||||
while (ptr)
|
||||
{
|
||||
@ -282,7 +285,6 @@ void monitor_start_all()
|
||||
}
|
||||
ptr = ptr->next;
|
||||
}
|
||||
spinlock_release(&monLock);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -319,9 +321,8 @@ void monitor_stop(MXS_MONITOR* monitor)
|
||||
|
||||
void monitor_deactivate(MXS_MONITOR* monitor)
|
||||
{
|
||||
spinlock_acquire(&monLock);
|
||||
std::lock_guard<std::mutex> guard(monLock);
|
||||
monitor->active = false;
|
||||
spinlock_release(&monLock);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -329,19 +330,15 @@ void monitor_deactivate(MXS_MONITOR* monitor)
|
||||
*/
|
||||
void monitor_stop_all()
|
||||
{
|
||||
spinlock_acquire(&monLock);
|
||||
std::lock_guard<std::mutex> guard(monLock);
|
||||
|
||||
MXS_MONITOR* monitor = allMonitors;
|
||||
while (monitor)
|
||||
for (MXS_MONITOR* monitor = allMonitors; monitor; monitor = monitor->next)
|
||||
{
|
||||
if (monitor->active)
|
||||
{
|
||||
monitor_stop(monitor);
|
||||
}
|
||||
monitor = monitor->next;
|
||||
}
|
||||
|
||||
spinlock_release(&monLock);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -515,19 +512,15 @@ void monitor_add_user(MXS_MONITOR* mon, const char* user, const char* passwd)
|
||||
*/
|
||||
void monitor_show_all(DCB* dcb)
|
||||
{
|
||||
MXS_MONITOR* ptr;
|
||||
std::lock_guard<std::mutex> guard(monLock);
|
||||
|
||||
spinlock_acquire(&monLock);
|
||||
ptr = allMonitors;
|
||||
while (ptr)
|
||||
for (MXS_MONITOR* ptr = allMonitors; ptr; ptr = ptr->next)
|
||||
{
|
||||
if (ptr->active)
|
||||
{
|
||||
monitor_show(dcb, ptr);
|
||||
}
|
||||
ptr = ptr->next;
|
||||
}
|
||||
spinlock_release(&monLock);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -585,7 +578,7 @@ void monitor_list(DCB* dcb)
|
||||
{
|
||||
MXS_MONITOR* ptr;
|
||||
|
||||
spinlock_acquire(&monLock);
|
||||
std::lock_guard<std::mutex> guard(monLock);
|
||||
ptr = allMonitors;
|
||||
dcb_printf(dcb, "---------------------+---------------------\n");
|
||||
dcb_printf(dcb, "%-20s | Status\n", "Monitor");
|
||||
@ -603,7 +596,6 @@ void monitor_list(DCB* dcb)
|
||||
ptr = ptr->next;
|
||||
}
|
||||
dcb_printf(dcb, "---------------------+---------------------\n");
|
||||
spinlock_release(&monLock);
|
||||
}
|
||||
|
||||
/**
|
||||
@ -614,20 +606,17 @@ void monitor_list(DCB* dcb)
|
||||
*/
|
||||
MXS_MONITOR* monitor_find(const char* name)
|
||||
{
|
||||
MXS_MONITOR* ptr;
|
||||
std::lock_guard<std::mutex> guard(monLock);
|
||||
|
||||
spinlock_acquire(&monLock);
|
||||
ptr = allMonitors;
|
||||
while (ptr)
|
||||
for (MXS_MONITOR* ptr = allMonitors; ptr; ptr = ptr->next)
|
||||
{
|
||||
if (!strcmp(ptr->name, name) && ptr->active)
|
||||
{
|
||||
break;
|
||||
return ptr;
|
||||
}
|
||||
ptr = ptr->next;
|
||||
}
|
||||
spinlock_release(&monLock);
|
||||
return ptr;
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
/**
|
||||
* Find a destroyed monitor by name
|
||||
@ -638,8 +627,7 @@ MXS_MONITOR* monitor_find(const char* name)
|
||||
MXS_MONITOR* monitor_repurpose_destroyed(const char* name, const char* module)
|
||||
{
|
||||
MXS_MONITOR* rval = NULL;
|
||||
|
||||
spinlock_acquire(&monLock);
|
||||
std::lock_guard<std::mutex> guard(monLock);
|
||||
|
||||
for (MXS_MONITOR* ptr = allMonitors; ptr; ptr = ptr->next)
|
||||
{
|
||||
@ -651,8 +639,6 @@ MXS_MONITOR* monitor_repurpose_destroyed(const char* name, const char* module)
|
||||
}
|
||||
}
|
||||
|
||||
spinlock_release(&monLock);
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
||||
@ -737,7 +723,7 @@ bool monitor_set_network_timeout(MXS_MONITOR* mon, int type, int value, const ch
|
||||
std::unique_ptr<ResultSet> monitor_get_list()
|
||||
{
|
||||
std::unique_ptr<ResultSet> set = ResultSet::create({"Monitor", "Status"});
|
||||
spinlock_acquire(&monLock);
|
||||
std::lock_guard<std::mutex> guard(monLock);
|
||||
|
||||
for (MXS_MONITOR* ptr = allMonitors; ptr; ptr = ptr->next)
|
||||
{
|
||||
@ -745,7 +731,6 @@ std::unique_ptr<ResultSet> monitor_get_list()
|
||||
set->add_row({ptr->name, state});
|
||||
}
|
||||
|
||||
spinlock_release(&monLock);
|
||||
return set;
|
||||
}
|
||||
|
||||
@ -1557,8 +1542,7 @@ static void mon_log_state_change(MXS_MONITORED_SERVER* ptr)
|
||||
MXS_MONITOR* monitor_server_in_use(const SERVER* server)
|
||||
{
|
||||
MXS_MONITOR* rval = NULL;
|
||||
|
||||
spinlock_acquire(&monLock);
|
||||
std::lock_guard<std::mutex> guard(monLock);
|
||||
|
||||
for (MXS_MONITOR* mon = allMonitors; mon && !rval; mon = mon->next)
|
||||
{
|
||||
@ -1578,8 +1562,6 @@ MXS_MONITOR* monitor_server_in_use(const SERVER* server)
|
||||
spinlock_release(&mon->lock);
|
||||
}
|
||||
|
||||
spinlock_release(&monLock);
|
||||
|
||||
return rval;
|
||||
}
|
||||
|
||||
@ -1865,7 +1847,7 @@ json_t* monitor_list_to_json(const char* host)
|
||||
{
|
||||
json_t* rval = json_array();
|
||||
|
||||
spinlock_acquire(&monLock);
|
||||
std::unique_lock<std::mutex> guard(monLock);
|
||||
|
||||
for (MXS_MONITOR* mon = allMonitors; mon; mon = mon->next)
|
||||
{
|
||||
@ -1880,7 +1862,7 @@ json_t* monitor_list_to_json(const char* host)
|
||||
}
|
||||
}
|
||||
|
||||
spinlock_release(&monLock);
|
||||
guard.unlock();
|
||||
|
||||
return mxs_json_resource(host, MXS_JSON_API_MONITORS, rval);
|
||||
}
|
||||
@ -1888,7 +1870,7 @@ json_t* monitor_list_to_json(const char* host)
|
||||
json_t* monitor_relations_to_server(const SERVER* server, const char* host)
|
||||
{
|
||||
std::vector<std::string> names;
|
||||
spinlock_acquire(&monLock);
|
||||
std::unique_lock<std::mutex> guard(monLock);
|
||||
|
||||
for (MXS_MONITOR* mon = allMonitors; mon; mon = mon->next)
|
||||
{
|
||||
@ -1909,7 +1891,7 @@ json_t* monitor_relations_to_server(const SERVER* server, const char* host)
|
||||
spinlock_release(&mon->lock);
|
||||
}
|
||||
|
||||
spinlock_release(&monLock);
|
||||
guard.unlock();
|
||||
|
||||
json_t* rel = NULL;
|
||||
|
||||
|
@ -983,8 +983,6 @@ void utils_end()
|
||||
replace_values_re = NULL;
|
||||
}
|
||||
|
||||
SPINLOCK tmplock = SPINLOCK_INIT;
|
||||
|
||||
static bool configure_network_socket(int so)
|
||||
{
|
||||
int sndbufsize = MXS_BACKEND_SO_SNDBUF;
|
||||
|
Reference in New Issue
Block a user