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,9 @@ void output_flex_t::select_relation_members()
935942 return ;
936943 }
937944
945+ // We can not use get_mutex_and_call_lua_function() here, because we need
946+ // the mutex to stick around as long as we are looking at the Lua stack.
947+ std::lock_guard<std::mutex> const guard{lua_mutex};
938948 call_lua_function (m_select_relation_members, m_relation_cache.get ());
939949
940950 // If the function returned nil there is nothing to be marked.
@@ -1014,7 +1024,8 @@ void output_flex_t::pending_relation(osmid_t id)
10141024 delete_from_tables (osmium::item_type::relation, id);
10151025
10161026 if (m_process_relation) {
1017- call_lua_function (m_process_relation, m_relation_cache.get ());
1027+ get_mutex_and_call_lua_function (m_process_relation,
1028+ m_relation_cache.get ());
10181029 }
10191030}
10201031
@@ -1029,7 +1040,7 @@ void output_flex_t::pending_relation_stage1c(osmid_t id)
10291040 }
10301041
10311042 m_disable_add_row = true ;
1032- call_lua_function (m_process_relation, m_relation_cache.get ());
1043+ get_mutex_and_call_lua_function (m_process_relation, m_relation_cache.get ());
10331044 m_disable_add_row = false ;
10341045}
10351046
@@ -1104,7 +1115,7 @@ void output_flex_t::node_add(osmium::Node const &node)
11041115 }
11051116
11061117 m_context_node = &node;
1107- call_lua_function (m_process_node, node);
1118+ get_mutex_and_call_lua_function (m_process_node, node);
11081119 m_context_node = nullptr ;
11091120}
11101121
@@ -1117,7 +1128,7 @@ void output_flex_t::way_add(osmium::Way *way)
11171128 }
11181129
11191130 m_way_cache.init (way);
1120- call_lua_function (m_process_way, m_way_cache.get ());
1131+ get_mutex_and_call_lua_function (m_process_way, m_way_cache.get ());
11211132}
11221133
11231134void output_flex_t::relation_add (osmium::Relation const &relation)
@@ -1128,7 +1139,7 @@ void output_flex_t::relation_add(osmium::Relation const &relation)
11281139
11291140 m_relation_cache.init (relation);
11301141 select_relation_members ();
1131- call_lua_function (m_process_relation, relation);
1142+ get_mutex_and_call_lua_function (m_process_relation, relation);
11321143}
11331144
11341145void output_flex_t::delete_from_table (table_connection_t *table_connection,
@@ -1518,7 +1529,7 @@ void output_flex_t::reprocess_marked()
15181529 }
15191530 way_delete (id);
15201531 if (m_process_way) {
1521- call_lua_function (m_process_way, m_way_cache.get ());
1532+ get_mutex_and_call_lua_function (m_process_way, m_way_cache.get ());
15221533 }
15231534 }
15241535
0 commit comments