Make include and bibitem insets threadsafe

Using a mutex to ensure that the generated filenames and ids are still unique.
This commit is contained in:
Georg Baum 2014-07-07 21:35:53 +02:00
parent 8fe58b90c2
commit f4ed3dc6b5
2 changed files with 9 additions and 1 deletions

View File

@ -41,6 +41,7 @@
#include "support/docstream.h" #include "support/docstream.h"
#include "support/gettext.h" #include "support/gettext.h"
#include "support/lstrings.h" #include "support/lstrings.h"
#include "support/mutex.h"
using namespace std; using namespace std;
using namespace lyx::support; using namespace lyx::support;
@ -49,6 +50,7 @@ namespace lyx {
int InsetBibitem::key_counter = 0; int InsetBibitem::key_counter = 0;
static Mutex counter_mutex;
docstring const key_prefix = from_ascii("key-"); docstring const key_prefix = from_ascii("key-");
@ -56,9 +58,11 @@ InsetBibitem::InsetBibitem(Buffer * buf, InsetCommandParams const & p)
: InsetCommand(buf, p) : InsetCommand(buf, p)
{ {
buffer().invalidateBibinfoCache(); buffer().invalidateBibinfoCache();
if (getParam("key").empty()) if (getParam("key").empty()) {
Mutex::Locker lock(&counter_mutex);
setParam("key", key_prefix + convert<docstring>(++key_counter)); setParam("key", key_prefix + convert<docstring>(++key_counter));
} }
}
InsetBibitem::~InsetBibitem() InsetBibitem::~InsetBibitem()
@ -197,6 +201,7 @@ void InsetBibitem::read(Lexer & lex)
if (prefixIs(getParam("key"), key_prefix)) { if (prefixIs(getParam("key"), key_prefix)) {
int const key = convert<int>(getParam("key").substr(key_prefix.length())); int const key = convert<int>(getParam("key").substr(key_prefix.length()));
Mutex::Locker lock(&counter_mutex);
key_counter = max(key_counter, key); key_counter = max(key_counter, key);
} }
} }

View File

@ -61,6 +61,7 @@
#include "support/lassert.h" #include "support/lassert.h"
#include "support/lstrings.h" // contains #include "support/lstrings.h" // contains
#include "support/lyxalgo.h" #include "support/lyxalgo.h"
#include "support/mutex.h"
#include "support/bind.h" #include "support/bind.h"
@ -77,6 +78,8 @@ namespace {
docstring const uniqueID() docstring const uniqueID()
{ {
static unsigned int seed = 1000; static unsigned int seed = 1000;
static Mutex mutex;
Mutex::Locker lock(&mutex);
return "file" + convert<docstring>(++seed); return "file" + convert<docstring>(++seed);
} }