Skip to content

Commit 1f708bd

Browse files
authored
Merge pull request #2016 from joto/refactor-lua-mutex
Refactor code that gets a mutex and calls into Lua code
2 parents b977158 + 4dca255 commit 1f708bd

File tree

2 files changed

+12
-24
lines changed

2 files changed

+12
-24
lines changed

src/output-flex.cpp

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,6 @@
4848
#include <stdexcept>
4949
#include <string>
5050

51-
// Mutex used to coordinate access to Lua code
52-
static std::mutex lua_mutex;
53-
5451
// Lua can't call functions on C++ objects directly. This macro defines simple
5552
// C "trampoline" functions which are called from Lua which get the current
5653
// context (the output_flex_t object) and call the respective function on the
@@ -896,6 +893,9 @@ int output_flex_t::expire_output_table()
896893
void output_flex_t::call_lua_function(prepared_lua_function_t func,
897894
osmium::OSMObject const &object)
898895
{
896+
static std::mutex lua_mutex;
897+
std::lock_guard<std::mutex> const guard{lua_mutex};
898+
899899
m_calling_context = func.context();
900900

901901
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,
912912
m_calling_context = calling_context::main;
913913
}
914914

915-
void output_flex_t::get_mutex_and_call_lua_function(
916-
prepared_lua_function_t func, osmium::OSMObject const &object)
917-
{
918-
std::lock_guard<std::mutex> const guard{lua_mutex};
919-
call_lua_function(func, object);
920-
}
921-
922915
void output_flex_t::pending_way(osmid_t id)
923916
{
924917
if (!m_process_way) {
@@ -931,7 +924,7 @@ void output_flex_t::pending_way(osmid_t id)
931924

932925
way_delete(id);
933926

934-
get_mutex_and_call_lua_function(m_process_way, m_way_cache.get());
927+
call_lua_function(m_process_way, m_way_cache.get());
935928
}
936929

937930
void output_flex_t::select_relation_members()
@@ -940,7 +933,6 @@ void output_flex_t::select_relation_members()
940933
return;
941934
}
942935

943-
std::lock_guard<std::mutex> const guard{lua_mutex};
944936
call_lua_function(m_select_relation_members, m_relation_cache.get());
945937

946938
// If the function returned nil there is nothing to be marked.
@@ -1020,8 +1012,7 @@ void output_flex_t::pending_relation(osmid_t id)
10201012
delete_from_tables(osmium::item_type::relation, id);
10211013

10221014
if (m_process_relation) {
1023-
get_mutex_and_call_lua_function(m_process_relation,
1024-
m_relation_cache.get());
1015+
call_lua_function(m_process_relation, m_relation_cache.get());
10251016
}
10261017
}
10271018

@@ -1036,7 +1027,7 @@ void output_flex_t::pending_relation_stage1c(osmid_t id)
10361027
}
10371028

10381029
m_disable_add_row = true;
1039-
get_mutex_and_call_lua_function(m_process_relation, m_relation_cache.get());
1030+
call_lua_function(m_process_relation, m_relation_cache.get());
10401031
m_disable_add_row = false;
10411032
}
10421033

@@ -1097,7 +1088,7 @@ void output_flex_t::node_add(osmium::Node const &node)
10971088
}
10981089

10991090
m_context_node = &node;
1100-
get_mutex_and_call_lua_function(m_process_node, node);
1091+
call_lua_function(m_process_node, node);
11011092
m_context_node = nullptr;
11021093
}
11031094

@@ -1110,7 +1101,7 @@ void output_flex_t::way_add(osmium::Way *way)
11101101
}
11111102

11121103
m_way_cache.init(way);
1113-
get_mutex_and_call_lua_function(m_process_way, m_way_cache.get());
1104+
call_lua_function(m_process_way, m_way_cache.get());
11141105
}
11151106

11161107
void output_flex_t::relation_add(osmium::Relation const &relation)
@@ -1121,7 +1112,7 @@ void output_flex_t::relation_add(osmium::Relation const &relation)
11211112

11221113
m_relation_cache.init(relation);
11231114
select_relation_members();
1124-
get_mutex_and_call_lua_function(m_process_relation, relation);
1115+
call_lua_function(m_process_relation, relation);
11251116
}
11261117

11271118
void output_flex_t::delete_from_table(table_connection_t *table_connection,
@@ -1511,7 +1502,7 @@ void output_flex_t::reprocess_marked()
15111502
}
15121503
way_delete(id);
15131504
if (m_process_way) {
1514-
get_mutex_and_call_lua_function(m_process_way, m_way_cache.get());
1505+
call_lua_function(m_process_way, m_way_cache.get());
15151506
}
15161507
}
15171508

src/output-flex.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,15 +185,12 @@ 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.
188+
* as its only parameter. Uses a mutex internally to make access to the
189+
* Lua environment thread safe.
189190
*/
190191
void call_lua_function(prepared_lua_function_t func,
191192
osmium::OSMObject const &object);
192193

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-
197194
void init_lua(std::string const &filename);
198195

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

0 commit comments

Comments
 (0)