add lock_guard to protect against data races in initialization of scripting environment
rename function names to reduce camel case noise
This commit is contained in:
parent
3dddd16ec7
commit
2e5876d488
@ -155,7 +155,7 @@ int Extractor::Run(int argc, char *argv[])
|
||||
resulting_restrictions;
|
||||
|
||||
// setup restriction parser
|
||||
const RestrictionParser restriction_parser(scripting_environment.getLuaState());
|
||||
const RestrictionParser restriction_parser(scripting_environment.get_lua_state());
|
||||
|
||||
while (const osmium::memory::Buffer buffer = reader.read())
|
||||
{
|
||||
@ -181,12 +181,14 @@ int Extractor::Run(int argc, char *argv[])
|
||||
ExtractionNode result_node;
|
||||
ExtractionWay result_way;
|
||||
|
||||
lua_State * local_state = scripting_environment.get_lua_state();
|
||||
|
||||
switch (entity->type())
|
||||
{
|
||||
case osmium::item_type::node:
|
||||
++number_of_nodes;
|
||||
luabind::call_function<void>(
|
||||
scripting_environment.getLuaState(),
|
||||
local_state,
|
||||
"node_function",
|
||||
boost::cref(static_cast<const osmium::Node &>(*entity)),
|
||||
boost::ref(result_node));
|
||||
@ -195,7 +197,7 @@ int Extractor::Run(int argc, char *argv[])
|
||||
case osmium::item_type::way:
|
||||
++number_of_ways;
|
||||
luabind::call_function<void>(
|
||||
scripting_environment.getLuaState(),
|
||||
local_state,
|
||||
"way_function",
|
||||
boost::cref(static_cast<const osmium::Way &>(*entity)),
|
||||
boost::ref(result_way));
|
||||
|
@ -60,13 +60,13 @@ int lua_error_callback(lua_State *L) // This is so I can use my own function as
|
||||
}
|
||||
|
||||
|
||||
ScriptingEnvironment::ScriptingEnvironment(const char *file_name)
|
||||
ScriptingEnvironment::ScriptingEnvironment(const std::string &file_name)
|
||||
: file_name(file_name)
|
||||
{
|
||||
SimpleLogger().Write() << "Using script " << file_name;
|
||||
}
|
||||
|
||||
void ScriptingEnvironment::initLuaState(lua_State* lua_state)
|
||||
void ScriptingEnvironment::init_lua_state(lua_State* lua_state)
|
||||
{
|
||||
typedef double (osmium::Location::* location_member_ptr_type)() const;
|
||||
|
||||
@ -129,18 +129,18 @@ void ScriptingEnvironment::initLuaState(lua_State* lua_state)
|
||||
}
|
||||
}
|
||||
|
||||
lua_State *ScriptingEnvironment::getLuaState()
|
||||
lua_State *ScriptingEnvironment::get_lua_state()
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(init_mutex);
|
||||
bool initialized = false;
|
||||
auto& ref = script_contexts.local(initialized);
|
||||
if (!initialized)
|
||||
{
|
||||
std::shared_ptr<lua_State> state(luaL_newstate(), lua_close);
|
||||
ref = state;
|
||||
initLuaState(ref.get());
|
||||
init_lua_state(ref.get());
|
||||
}
|
||||
luabind::set_pcall_callback(&lua_error_callback);
|
||||
|
||||
return ref.get();
|
||||
}
|
||||
|
||||
|
@ -30,6 +30,7 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
|
||||
#include <string>
|
||||
#include <memory>
|
||||
#include <mutex>
|
||||
#include <tbb/enumerable_thread_specific.h>
|
||||
|
||||
struct lua_State;
|
||||
@ -38,13 +39,13 @@ class ScriptingEnvironment
|
||||
{
|
||||
public:
|
||||
ScriptingEnvironment() = delete;
|
||||
explicit ScriptingEnvironment(const char *file_name);
|
||||
explicit ScriptingEnvironment(const std::string &file_name);
|
||||
|
||||
lua_State *getLuaState();
|
||||
lua_State *get_lua_state();
|
||||
|
||||
private:
|
||||
void initLuaState(lua_State* lua_state);
|
||||
|
||||
void init_lua_state(lua_State* lua_state);
|
||||
std::mutex init_mutex;
|
||||
std::string file_name;
|
||||
tbb::enumerable_thread_specific<std::shared_ptr<lua_State>> script_contexts;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user