From b038636ff558b4ee8e4fd423a1610d8974f09b01 Mon Sep 17 00:00:00 2001 From: Leonardo de Moura Date: Wed, 27 Nov 2013 15:14:26 -0800 Subject: [PATCH] refactor(util/script_state): remove unsafe unguarded_apply Signed-off-by: Leonardo de Moura --- src/util/script_state.cpp | 69 +++++++++++++++++++-------------------- src/util/script_state.h | 13 +------- 2 files changed, 34 insertions(+), 48 deletions(-) diff --git a/src/util/script_state.cpp b/src/util/script_state.cpp index 28dff8d4b..a4dc5d05a 100644 --- a/src/util/script_state.cpp +++ b/src/util/script_state.cpp @@ -301,9 +301,8 @@ class data_channel { std::condition_variable m_cv; public: data_channel() { - m_channel.unguarded_apply([&](lua_State * channel) { - m_ini = lua_gettop(channel); - }); + lua_State * channel = m_channel.m_ptr->m_state; + m_ini = lua_gettop(channel); } /** @@ -316,12 +315,11 @@ public: if (last < first) return; std::lock_guard lock(m_mutex); - m_channel.unguarded_apply([&](lua_State * channel) { - bool was_empty = lua_gettop(channel) == m_ini; - copy_values(src, first, last, channel); - if (was_empty) - m_cv.notify_one(); - }); + lua_State * channel = m_channel.m_ptr->m_state; + bool was_empty = lua_gettop(channel) == m_ini; + copy_values(src, first, last, channel); + if (was_empty) + m_cv.notify_one(); } /** @@ -330,33 +328,32 @@ public: */ int read(lua_State * tgt, int i) { std::unique_lock lock(m_mutex); - return m_channel.unguarded_apply([&](lua_State * channel) { - if (i > 0) { - // i is the position of the timeout argument - std::chrono::milliseconds dura(luaL_checkinteger(tgt, i)); - if (lua_gettop(channel) == m_ini) - m_cv.wait_for(lock, dura); - if (lua_gettop(channel) == m_ini) { - // timeout... - lua_pushboolean(tgt, false); - lua_pushnil(tgt); - return 2; - } else { - lua_pushboolean(tgt, true); - copy_values(channel, m_ini + 1, m_ini + 1, tgt); - lua_remove(channel, m_ini + 1); - return 2; - } - } else { - while (lua_gettop(channel) == m_ini) { - check_interrupted(); - m_cv.wait_for(lock, g_small_delay); - } - copy_values(channel, m_ini + 1, m_ini + 1, tgt); - lua_remove(channel, m_ini + 1); - return 1; - } - }); + lua_State * channel = m_channel.m_ptr->m_state; + if (i > 0) { + // i is the position of the timeout argument + std::chrono::milliseconds dura(luaL_checkinteger(tgt, i)); + if (lua_gettop(channel) == m_ini) + m_cv.wait_for(lock, dura); + if (lua_gettop(channel) == m_ini) { + // timeout... + lua_pushboolean(tgt, false); + lua_pushnil(tgt); + return 2; + } else { + lua_pushboolean(tgt, true); + copy_values(channel, m_ini + 1, m_ini + 1, tgt); + lua_remove(channel, m_ini + 1); + return 2; + } + } else { + while (lua_gettop(channel) == m_ini) { + check_interrupted(); + m_cv.wait_for(lock, g_small_delay); + } + copy_values(channel, m_ini + 1, m_ini + 1, tgt); + lua_remove(channel, m_ini + 1); + return 1; + } } }; diff --git a/src/util/script_state.h b/src/util/script_state.h index b41cabf78..67a9b3c70 100644 --- a/src/util/script_state.h +++ b/src/util/script_state.h @@ -22,6 +22,7 @@ private: friend script_state to_script_state(lua_State * L); std::recursive_mutex & get_mutex(); lua_State * get_state(); + friend class data_channel; public: script_state(); virtual ~script_state(); @@ -46,18 +47,6 @@ public: return f(get_state()); } - /** - \brief Similar to \c apply, but a lock is not used to guarantee - exclusive access to the lua_State object. - - \warning It is the caller resposability to guarantee that the object is not being - concurrently accessed. - */ - template - typename std::result_of::type unguarded_apply(F && f) { - return f(get_state()); - } - typedef void (*reg_fn)(lua_State *); // NOLINT static void register_module(reg_fn f);