From 360cc3188db5f5b65c7fcddba0b2e38fd3483d15 Mon Sep 17 00:00:00 2001 From: Enrico Granata Date: Wed, 27 Mar 2013 22:38:11 +0000 Subject: [PATCH] Implementing the notion of externally-acquirable ScriptInterpreter lock With this notion, if parties outside the ScriptInterpreter itself need to acquire a lock on script APIs, they can do so by a pattern like this: { auto lock = interpeter->AcquireInterpreterLock(); // do whatever you need to do... } // lock will automatically be released here This might be useful for classes that use the Python convenience objects (e.g. PythonDictionary) to ensure they keep the underlying interpreter in a safe and controlled condition while they call through the C API functions Of course, the ScriptInterpreter still manages its internal locking correctly when necessary :-) llvm-svn: 178189 --- .../lldb/Interpreter/ScriptInterpreter.h | 19 +++++++++++++++++++ .../Interpreter/ScriptInterpreterPython.h | 5 ++++- lldb/include/lldb/lldb-forward.h | 1 + lldb/source/Interpreter/ScriptInterpreter.cpp | 6 ++++++ .../Interpreter/ScriptInterpreterPython.cpp | 10 ++++++++++ 5 files changed, 40 insertions(+), 1 deletion(-) diff --git a/lldb/include/lldb/Interpreter/ScriptInterpreter.h b/lldb/include/lldb/Interpreter/ScriptInterpreter.h index cc66a08f0cfb..417c79fde323 100644 --- a/lldb/include/lldb/Interpreter/ScriptInterpreter.h +++ b/lldb/include/lldb/Interpreter/ScriptInterpreter.h @@ -61,6 +61,22 @@ public: protected: void* m_object; }; + +class ScriptInterpreterLocker +{ +public: + + ScriptInterpreterLocker () + { + } + + virtual ~ScriptInterpreterLocker () + { + } +private: + DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterLocker); +}; + class ScriptInterpreter { @@ -409,6 +425,9 @@ public: return lldb::ScriptInterpreterObjectSP(new ScriptInterpreterObject(object)); } + virtual std::auto_ptr + AcquireInterpreterLock (); + const char * GetScriptInterpreterPtyName (); diff --git a/lldb/include/lldb/Interpreter/ScriptInterpreterPython.h b/lldb/include/lldb/Interpreter/ScriptInterpreterPython.h index c6a66fdb4324..31e958362512 100644 --- a/lldb/include/lldb/Interpreter/ScriptInterpreterPython.h +++ b/lldb/include/lldb/Interpreter/ScriptInterpreterPython.h @@ -181,6 +181,9 @@ public: virtual lldb::ScriptInterpreterObjectSP MakeScriptObject (void* object); + virtual std::auto_ptr + AcquireInterpreterLock (); + void CollectDataForBreakpointCommandCallback (BreakpointOptions *bp_options, CommandReturnObject &result); @@ -271,7 +274,7 @@ private: DISALLOW_COPY_AND_ASSIGN (ScriptInterpreterPythonObject); }; - class Locker + class Locker : public ScriptInterpreterLocker { public: diff --git a/lldb/include/lldb/lldb-forward.h b/lldb/include/lldb/lldb-forward.h index 31148327cf6e..2227fdb4c213 100644 --- a/lldb/include/lldb/lldb-forward.h +++ b/lldb/include/lldb/lldb-forward.h @@ -178,6 +178,7 @@ class RegisterValue; class RegularExpression; class Scalar; class ScriptInterpreter; +class ScriptInterpreterLocker; class ScriptInterpreterObject; #ifndef LLDB_DISABLE_PYTHON class ScriptInterpreterPython; diff --git a/lldb/source/Interpreter/ScriptInterpreter.cpp b/lldb/source/Interpreter/ScriptInterpreter.cpp index 6bc6cd0d2ad8..0e85902cff2c 100644 --- a/lldb/source/Interpreter/ScriptInterpreter.cpp +++ b/lldb/source/Interpreter/ScriptInterpreter.cpp @@ -81,6 +81,12 @@ ScriptInterpreter::LanguageToString (lldb::ScriptLanguage language) return return_value; } +std::auto_ptr +ScriptInterpreter::AcquireInterpreterLock () +{ + return std::auto_ptr(new ScriptInterpreterLocker()); +} + void ScriptInterpreter::InitializeInterpreter (SWIGInitCallback python_swig_init_callback) { diff --git a/lldb/source/Interpreter/ScriptInterpreterPython.cpp b/lldb/source/Interpreter/ScriptInterpreterPython.cpp index 5a97107ac101..68e984c1e6b1 100644 --- a/lldb/source/Interpreter/ScriptInterpreterPython.cpp +++ b/lldb/source/Interpreter/ScriptInterpreterPython.cpp @@ -135,6 +135,7 @@ ScriptInterpreterPython::Locker::Locker (ScriptInterpreterPython *py_interpreter uint16_t on_entry, uint16_t on_leave, FILE* wait_msg_handle) : + ScriptInterpreterLocker (), m_teardown_session( (on_leave & TearDownSession) == TearDownSession ), m_python_interpreter(py_interpreter), m_tmp_fh(wait_msg_handle) @@ -2806,6 +2807,15 @@ ScriptInterpreterPython::GetDocumentationForItem(const char* item, std::string& } } +std::auto_ptr +ScriptInterpreterPython::AcquireInterpreterLock () +{ + std::auto_ptr py_lock(new Locker(this, + Locker::AcquireLock | Locker::InitSession, + Locker::FreeLock | Locker::TearDownSession)); + return py_lock; +} + void ScriptInterpreterPython::InitializeInterpreter (SWIGInitCallback python_swig_init_callback) {