4848#include < stdexcept>
4949#include < string>
5050
51+ // Mutex used to coordinate access to Lua code
52+ static std::mutex lua_mutex;
53+
5154// Lua can't call functions on C++ objects directly. This macro defines simple
5255// C "trampoline" functions which are called from Lua which get the current
5356// context (the output_flex_t object) and call the respective function on the
@@ -895,9 +898,6 @@ int output_flex_t::expire_output_table()
895898void output_flex_t::call_lua_function (prepared_lua_function_t func,
896899 osmium::OSMObject const &object)
897900{
898- static std::mutex lua_mutex;
899- std::lock_guard<std::mutex> const guard{lua_mutex};
900-
901901 m_calling_context = func.context ();
902902
903903 lua_pushvalue (lua_state (), func.index ()); // the function to call
@@ -914,6 +914,13 @@ void output_flex_t::call_lua_function(prepared_lua_function_t func,
914914 m_calling_context = calling_context::main;
915915}
916916
917+ void output_flex_t::get_mutex_and_call_lua_function (
918+ prepared_lua_function_t func, osmium::OSMObject const &object)
919+ {
920+ std::lock_guard<std::mutex> const guard{lua_mutex};
921+ call_lua_function (func, object);
922+ }
923+
917924void output_flex_t::pending_way (osmid_t id)
918925{
919926 if (!m_process_way) {
@@ -926,7 +933,7 @@ void output_flex_t::pending_way(osmid_t id)
926933
927934 way_delete (id);
928935
929- call_lua_function (m_process_way, m_way_cache.get ());
936+ get_mutex_and_call_lua_function (m_process_way, m_way_cache.get ());
930937}
931938
932939void output_flex_t::select_relation_members ()
@@ -935,6 +942,7 @@ void output_flex_t::select_relation_members()
935942 return ;
936943 }
937944
945+ std::lock_guard<std::mutex> const guard{lua_mutex};
938946 call_lua_function (m_select_relation_members, m_relation_cache.get ());
939947
940948 // If the function returned nil there is nothing to be marked.
@@ -1014,7 +1022,8 @@ void output_flex_t::pending_relation(osmid_t id)
10141022 delete_from_tables (osmium::item_type::relation, id);
10151023
10161024 if (m_process_relation) {
1017- call_lua_function (m_process_relation, m_relation_cache.get ());
1025+ get_mutex_and_call_lua_function (m_process_relation,
1026+ m_relation_cache.get ());
10181027 }
10191028}
10201029
@@ -1029,7 +1038,7 @@ void output_flex_t::pending_relation_stage1c(osmid_t id)
10291038 }
10301039
10311040 m_disable_add_row = true ;
1032- call_lua_function (m_process_relation, m_relation_cache.get ());
1041+ get_mutex_and_call_lua_function (m_process_relation, m_relation_cache.get ());
10331042 m_disable_add_row = false ;
10341043}
10351044
@@ -1104,7 +1113,7 @@ void output_flex_t::node_add(osmium::Node const &node)
11041113 }
11051114
11061115 m_context_node = &node;
1107- call_lua_function (m_process_node, node);
1116+ get_mutex_and_call_lua_function (m_process_node, node);
11081117 m_context_node = nullptr ;
11091118}
11101119
@@ -1117,7 +1126,7 @@ void output_flex_t::way_add(osmium::Way *way)
11171126 }
11181127
11191128 m_way_cache.init (way);
1120- call_lua_function (m_process_way, m_way_cache.get ());
1129+ get_mutex_and_call_lua_function (m_process_way, m_way_cache.get ());
11211130}
11221131
11231132void output_flex_t::relation_add (osmium::Relation const &relation)
@@ -1128,7 +1137,7 @@ void output_flex_t::relation_add(osmium::Relation const &relation)
11281137
11291138 m_relation_cache.init (relation);
11301139 select_relation_members ();
1131- call_lua_function (m_process_relation, relation);
1140+ get_mutex_and_call_lua_function (m_process_relation, relation);
11321141}
11331142
11341143void output_flex_t::delete_from_table (table_connection_t *table_connection,
@@ -1518,7 +1527,7 @@ void output_flex_t::reprocess_marked()
15181527 }
15191528 way_delete (id);
15201529 if (m_process_way) {
1521- call_lua_function (m_process_way, m_way_cache.get ());
1530+ get_mutex_and_call_lua_function (m_process_way, m_way_cache.get ());
15221531 }
15231532 }
15241533
0 commit comments