diff --git a/src/output-flex.cpp b/src/output-flex.cpp index 60f01a3ed..0f21f23f8 100644 --- a/src/output-flex.cpp +++ b/src/output-flex.cpp @@ -48,9 +48,6 @@ #include #include -// Mutex used to coordinate access to Lua code -static std::mutex lua_mutex; - // Lua can't call functions on C++ objects directly. This macro defines simple // C "trampoline" functions which are called from Lua which get the current // context (the output_flex_t object) and call the respective function on the @@ -896,6 +893,9 @@ int output_flex_t::expire_output_table() void output_flex_t::call_lua_function(prepared_lua_function_t func, osmium::OSMObject const &object) { + static std::mutex lua_mutex; + std::lock_guard const guard{lua_mutex}; + m_calling_context = func.context(); lua_pushvalue(lua_state(), func.index()); // the function to call @@ -912,13 +912,6 @@ void output_flex_t::call_lua_function(prepared_lua_function_t func, m_calling_context = calling_context::main; } -void output_flex_t::get_mutex_and_call_lua_function( - prepared_lua_function_t func, osmium::OSMObject const &object) -{ - std::lock_guard const guard{lua_mutex}; - call_lua_function(func, object); -} - void output_flex_t::pending_way(osmid_t id) { if (!m_process_way) { @@ -931,7 +924,7 @@ void output_flex_t::pending_way(osmid_t id) way_delete(id); - get_mutex_and_call_lua_function(m_process_way, m_way_cache.get()); + call_lua_function(m_process_way, m_way_cache.get()); } void output_flex_t::select_relation_members() @@ -940,7 +933,6 @@ void output_flex_t::select_relation_members() return; } - std::lock_guard const guard{lua_mutex}; call_lua_function(m_select_relation_members, m_relation_cache.get()); // If the function returned nil there is nothing to be marked. @@ -1020,8 +1012,7 @@ void output_flex_t::pending_relation(osmid_t id) delete_from_tables(osmium::item_type::relation, id); if (m_process_relation) { - get_mutex_and_call_lua_function(m_process_relation, - m_relation_cache.get()); + call_lua_function(m_process_relation, m_relation_cache.get()); } } @@ -1036,7 +1027,7 @@ void output_flex_t::pending_relation_stage1c(osmid_t id) } m_disable_add_row = true; - get_mutex_and_call_lua_function(m_process_relation, m_relation_cache.get()); + call_lua_function(m_process_relation, m_relation_cache.get()); m_disable_add_row = false; } @@ -1097,7 +1088,7 @@ void output_flex_t::node_add(osmium::Node const &node) } m_context_node = &node; - get_mutex_and_call_lua_function(m_process_node, node); + call_lua_function(m_process_node, node); m_context_node = nullptr; } @@ -1110,7 +1101,7 @@ void output_flex_t::way_add(osmium::Way *way) } m_way_cache.init(way); - get_mutex_and_call_lua_function(m_process_way, m_way_cache.get()); + call_lua_function(m_process_way, m_way_cache.get()); } void output_flex_t::relation_add(osmium::Relation const &relation) @@ -1121,7 +1112,7 @@ void output_flex_t::relation_add(osmium::Relation const &relation) m_relation_cache.init(relation); select_relation_members(); - get_mutex_and_call_lua_function(m_process_relation, relation); + call_lua_function(m_process_relation, relation); } void output_flex_t::delete_from_table(table_connection_t *table_connection, @@ -1511,7 +1502,7 @@ void output_flex_t::reprocess_marked() } way_delete(id); if (m_process_way) { - get_mutex_and_call_lua_function(m_process_way, m_way_cache.get()); + call_lua_function(m_process_way, m_way_cache.get()); } } diff --git a/src/output-flex.hpp b/src/output-flex.hpp index 436237b32..ed457f9d5 100644 --- a/src/output-flex.hpp +++ b/src/output-flex.hpp @@ -185,15 +185,12 @@ class output_flex_t : public output_t /** * Call a Lua function that was "prepared" earlier with the OSMObject - * as its only parameter. + * as its only parameter. Uses a mutex internally to make access to the + * Lua environment thread safe. */ void call_lua_function(prepared_lua_function_t func, osmium::OSMObject const &object); - /// Aquire the lua_mutex and the call `call_lua_function()`. - void get_mutex_and_call_lua_function(prepared_lua_function_t func, - osmium::OSMObject const &object); - void init_lua(std::string const &filename); // Get the flex table that is as first parameter on the Lua stack.