feat(util/script_state): use recursive_mutex instead of mutex

Signed-off-by: Leonardo de Moura <leonardo@microsoft.com>
This commit is contained in:
Leonardo de Moura 2014-05-01 12:27:12 -07:00
parent 686c307976
commit 9452d164ec
4 changed files with 19 additions and 12 deletions

View file

@ -40,8 +40,8 @@ void open_extra(lua_State * L);
static char g_weak_ptr_key; // key for Lua registry (used at get_weak_ptr and save_weak_ptr) static char g_weak_ptr_key; // key for Lua registry (used at get_weak_ptr and save_weak_ptr)
struct script_state::imp { struct script_state::imp {
lua_State * m_state; lua_State * m_state;
mutex m_mutex; recursive_mutex m_mutex;
std::unordered_set<std::string> m_imported_modules; std::unordered_set<std::string> m_imported_modules;
static std::weak_ptr<imp> * get_weak_ptr(lua_State * L) { static std::weak_ptr<imp> * get_weak_ptr(lua_State * L) {
@ -100,12 +100,12 @@ struct script_state::imp {
} }
void dofile(char const * fname) { void dofile(char const * fname) {
lock_guard<mutex> lock(m_mutex); lock_guard<recursive_mutex> lock(m_mutex);
::lean::dofile(m_state, fname); ::lean::dofile(m_state, fname);
} }
void dostring(char const * str) { void dostring(char const * str) {
lock_guard<mutex> lock(m_mutex); lock_guard<recursive_mutex> lock(m_mutex);
::lean::dostring(m_state, str); ::lean::dostring(m_state, str);
} }
@ -162,7 +162,7 @@ bool script_state::import_explicit(char const * str) {
return m_ptr->import_explicit(str); return m_ptr->import_explicit(str);
} }
mutex & script_state::get_mutex() { recursive_mutex & script_state::get_mutex() {
return m_ptr->m_mutex; return m_ptr->m_mutex;
} }

View file

@ -20,7 +20,7 @@ public:
private: private:
std::shared_ptr<imp> m_ptr; std::shared_ptr<imp> m_ptr;
friend script_state to_script_state(lua_State * L); friend script_state to_script_state(lua_State * L);
mutex & get_mutex(); recursive_mutex & get_mutex();
lua_State * get_state(); lua_State * get_state();
friend class data_channel; friend class data_channel;
public: public:
@ -60,7 +60,7 @@ public:
*/ */
template<typename F> template<typename F>
typename std::result_of<F(lua_State * L)>::type apply(F && f) { typename std::result_of<F(lua_State * L)>::type apply(F && f) {
lock_guard<mutex> lock(get_mutex()); lock_guard<recursive_mutex> lock(get_mutex());
return f(get_state()); return f(get_state());
} }
@ -74,13 +74,13 @@ public:
*/ */
template<typename F> template<typename F>
void exec_unprotected(F && f) { void exec_unprotected(F && f) {
unlock_guard unlock(get_mutex()); unlock_guard<recursive_mutex> unlock(get_mutex());
f(); f();
} }
template<typename F> template<typename F>
void exec_protected(F && f) { void exec_protected(F && f) {
lock_guard<mutex> lock(get_mutex()); lock_guard<recursive_mutex> lock(get_mutex());
f(); f();
} }
}; };

View file

@ -18,6 +18,7 @@ namespace lean {
inline void set_thread_stack_size(size_t ) {} inline void set_thread_stack_size(size_t ) {}
using std::thread; using std::thread;
using std::mutex; using std::mutex;
using std::recursive_mutex;
using std::atomic; using std::atomic;
using std::atomic_bool; using std::atomic_bool;
using std::atomic_ushort; using std::atomic_ushort;
@ -40,7 +41,7 @@ namespace lean {
void set_thread_stack_size(size_t ); void set_thread_stack_size(size_t );
boost::thread::attributes const & get_thread_attributes(); boost::thread::attributes const & get_thread_attributes();
using boost::thread; using boost::thread;
using boost::mutex; using boost::recursive_mutex;
using boost::atomic; using boost::atomic;
using boost::memory_order_relaxed; using boost::memory_order_relaxed;
using boost::condition_variable; using boost::condition_variable;
@ -118,6 +119,11 @@ public:
void lock() {} void lock() {}
void unlock() {} void unlock() {}
}; };
class recursive_mutex {
public:
void lock() {}
void unlock() {}
};
class condition_variable { class condition_variable {
public: public:
template<typename Lock> void wait(Lock const &) {} template<typename Lock> void wait(Lock const &) {}

View file

@ -29,10 +29,11 @@ namespace lean {
\warning The calling thread must own the lock to m_mutex \warning The calling thread must own the lock to m_mutex
*/ */
template<typename Mutex>
class unlock_guard { class unlock_guard {
mutex & m_mutex; Mutex & m_mutex;
public: public:
explicit unlock_guard(mutex & m):m_mutex(m) { m_mutex.unlock(); } explicit unlock_guard(Mutex & m):m_mutex(m) { m_mutex.unlock(); }
unlock_guard(unlock_guard const &) = delete; unlock_guard(unlock_guard const &) = delete;
unlock_guard(unlock_guard &&) = delete; unlock_guard(unlock_guard &&) = delete;
unlock_guard & operator=(unlock_guard const &) = delete; unlock_guard & operator=(unlock_guard const &) = delete;