prc: prefer global-scope static MutexImpl over local one

MutexImpl guarantees nowadays to be initialized at constant init time, so this is safer on Windows, where we can't rely on magic statics.
This commit is contained in:
rdb 2019-02-11 15:54:56 +01:00
parent b466e77fe3
commit 3df3b27a4e
3 changed files with 16 additions and 21 deletions

View File

@ -21,6 +21,8 @@
using std::string;
static MutexImpl this_prc_dir_lock;
/**
* Use the ConfigPage::make_declaration() interface to create a new
* declaration.
@ -139,13 +141,6 @@ set_double_word(size_t n, double value) {
*/
Filename ConfigDeclaration::
get_filename_value() const {
// Since we are about to set THIS_PRC_DIR globally, we need to ensure that
// no two threads call this method at the same time.
// NB. MSVC doesn't guarantee that this mutex is initialized in a
// thread-safe manner. But chances are that the first time this is called
// is at static init time, when there is no risk of data races.
static MutexImpl lock;
string str = _string_value;
// Are there any variables to be expanded?
@ -153,11 +148,13 @@ get_filename_value() const {
Filename page_filename(_page->get_name());
Filename page_dirname = page_filename.get_dirname();
lock.lock();
// Since we are about to set THIS_PRC_DIR globally, we need to ensure that
// no two threads call this method at the same time.
this_prc_dir_lock.lock();
ExecutionEnvironment::shadow_environment_variable("THIS_PRC_DIR", page_dirname.to_os_specific());
str = ExecutionEnvironment::expand_string(str);
ExecutionEnvironment::clear_shadow("THIS_PRC_DIR");
lock.unlock();
this_prc_dir_lock.unlock();
}
Filename fn;

View File

@ -13,17 +13,16 @@
#include "configVariableFilename.h"
#include "executionEnvironment.h"
#include "mutexImpl.h"
static MutexImpl filename_lock;
/**
* Recopies the config variable into the Filename for returning its value.
*/
void ConfigVariableFilename::
reload_cache() {
// NB. MSVC doesn't guarantee that this mutex is initialized in a
// thread-safe manner. But chances are that the first time this is called
// is at static init time, when there is no risk of data races.
static MutexImpl lock;
lock.lock();
filename_lock.lock();
// We check again for cache validity since another thread may have beaten
// us to the punch while we were waiting for the lock.
@ -34,5 +33,5 @@ reload_cache() {
_cache = decl->get_filename_value();
mark_cache_valid(_local_modified);
}
lock.unlock();
filename_lock.unlock();
}

View File

@ -12,17 +12,16 @@
*/
#include "configVariableString.h"
#include "mutexImpl.h"
static MutexImpl string_lock;
/**
* Refreshes the variable's cached value.
*/
void ConfigVariableString::
reload_cache() {
// NB. MSVC doesn't guarantee that this mutex is initialized in a
// thread-safe manner. But chances are that the first time this is called
// is at static init time, when there is no risk of data races.
static MutexImpl lock;
lock.lock();
string_lock.lock();
// We check again for cache validity since another thread may have beaten
// us to the punch while we were waiting for the lock.
@ -31,5 +30,5 @@ reload_cache() {
mark_cache_valid(_local_modified);
}
lock.unlock();
string_lock.unlock();
}