Skip to content

Commit 58dca4f

Browse files
committed
Revert "Refactor code that gets a mutex and calls into Lua code"
This reverts commit 4dca255. The select_relation_members() reads data from the Lua stack after calling call_lua_function(). It is important that this is still protected by the mutex.
1 parent 368d67d commit 58dca4f

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

src/output-flex.cpp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,9 @@
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()
895898
void 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+
917924
void 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

932939
void 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

11231132
void 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

11341143
void 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

src/output-flex.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,12 +185,15 @@ class output_flex_t : public output_t
185185

186186
/**
187187
* Call a Lua function that was "prepared" earlier with the OSMObject
188-
* as its only parameter. Uses a mutex internally to make access to the
189-
* Lua environment thread safe.
188+
* as its only parameter.
190189
*/
191190
void call_lua_function(prepared_lua_function_t func,
192191
osmium::OSMObject const &object);
193192

193+
/// Aquire the lua_mutex and the call `call_lua_function()`.
194+
void get_mutex_and_call_lua_function(prepared_lua_function_t func,
195+
osmium::OSMObject const &object);
196+
194197
void init_lua(std::string const &filename);
195198

196199
// Get the flex table that is as first parameter on the Lua stack.

0 commit comments

Comments
 (0)