Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 10 additions & 19 deletions src/output-flex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,6 @@
#include <stdexcept>
#include <string>

// 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
Expand Down Expand Up @@ -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<std::mutex> const guard{lua_mutex};

m_calling_context = func.context();

lua_pushvalue(lua_state(), func.index()); // the function to call
Expand All @@ -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<std::mutex> const guard{lua_mutex};
call_lua_function(func, object);
}

void output_flex_t::pending_way(osmid_t id)
{
if (!m_process_way) {
Expand All @@ -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()
Expand All @@ -940,7 +933,6 @@ void output_flex_t::select_relation_members()
return;
}

std::lock_guard<std::mutex> 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.
Expand Down Expand Up @@ -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());
}
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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)
Expand All @@ -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,
Expand Down Expand Up @@ -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());
}
}

Expand Down
7 changes: 2 additions & 5 deletions src/output-flex.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down