From 50929b5b8a294079de7b81617a119a51dcb3142c Mon Sep 17 00:00:00 2001 From: Georg Baum Date: Sat, 5 Jul 2014 12:09:49 +0200 Subject: [PATCH] Make include and bibitem insets threadsafe Using a mutex to ensure that the generated filenames and ids are still unique. --- src/insets/InsetBibitem.cpp | 8 ++++++-- src/insets/InsetInclude.cpp | 4 +++- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/insets/InsetBibitem.cpp b/src/insets/InsetBibitem.cpp index 4f263e99de..d0a28e73fe 100644 --- a/src/insets/InsetBibitem.cpp +++ b/src/insets/InsetBibitem.cpp @@ -41,6 +41,7 @@ #include "support/docstream.h" #include "support/gettext.h" #include "support/lstrings.h" +#include "support/mutex.h" using namespace std; using namespace lyx::support; @@ -48,8 +49,8 @@ using namespace lyx::support; namespace lyx { -// FIXME THREAD int InsetBibitem::key_counter = 0; +static Mutex counter_mutex; docstring const key_prefix = from_ascii("key-"); @@ -57,8 +58,10 @@ InsetBibitem::InsetBibitem(Buffer * buf, InsetCommandParams const & p) : InsetCommand(buf, p) { buffer().invalidateBibinfoCache(); - if (getParam("key").empty()) + if (getParam("key").empty()) { + Mutex::Locker lock(&counter_mutex); setParam("key", key_prefix + convert(++key_counter)); + } } @@ -196,6 +199,7 @@ void InsetBibitem::read(Lexer & lex) if (prefixIs(getParam("key"), key_prefix)) { int const key = convert(getParam("key").substr(key_prefix.length())); + Mutex::Locker lock(&counter_mutex); key_counter = max(key_counter, key); } } diff --git a/src/insets/InsetInclude.cpp b/src/insets/InsetInclude.cpp index ff4a3291eb..7494650443 100644 --- a/src/insets/InsetInclude.cpp +++ b/src/insets/InsetInclude.cpp @@ -60,6 +60,7 @@ #include "support/lassert.h" #include "support/lstrings.h" // contains #include "support/lyxalgo.h" +#include "support/mutex.h" #include "support/bind.h" @@ -75,8 +76,9 @@ namespace { docstring const uniqueID() { - // FIXME THREAD static unsigned int seed = 1000; + static Mutex mutex; + Mutex::Locker lock(&mutex); return "file" + convert(++seed); }