* python/python-internal.h (PyGILState_Ensure): New define.
authorTom Tromey <tromey@redhat.com>
Fri, 21 Nov 2008 14:59:56 +0000 (14:59 +0000)
committerTom Tromey <tromey@redhat.com>
Fri, 21 Nov 2008 14:59:56 +0000 (14:59 +0000)
(PyGILState_Release): Likewise.
(PyEval_InitThreads): Likewise.
(PyThreadState_Swap): Likewise.
(PyEval_InitThreads): Likewise.
* python/python.c (_initialize_python): Initialize threads.
Release GIL.
(eval_python_from_control_command): Acquire GIL.
(python_command): Likewise.
* python/python-internal.h (make_cleanup_py_restore_gil):
Declare.
* python/python-utils.c (py_gil_restore): New function.
(make_cleanup_py_restore_gil): Likewise.

gdb/ChangeLog
gdb/python/python-internal.h
gdb/python/python-utils.c
gdb/python/python.c

index 92a50e25f65ab4e9da21b542868ebf903385f57f..87cb7eff1aa1a0a054cc1f184584be22f0418151 100644 (file)
@@ -1,3 +1,19 @@
+2008-11-21  Tom Tromey  <tromey@redhat.com>
+
+       * python/python-internal.h (PyGILState_Ensure): New define.
+       (PyGILState_Release): Likewise.
+       (PyEval_InitThreads): Likewise.
+       (PyThreadState_Swap): Likewise.
+       (PyEval_InitThreads): Likewise.
+       * python/python.c (_initialize_python): Initialize threads.
+       Release GIL.
+       (eval_python_from_control_command): Acquire GIL.
+       (python_command): Likewise.
+       * python/python-internal.h (make_cleanup_py_restore_gil):
+       Declare.
+       * python/python-utils.c (py_gil_restore): New function.
+       (make_cleanup_py_restore_gil): Likewise.
+
 2008-11-20  Doug Evans  <dje@google.com>
 
        * frame.c (frame_debug_got_null_frame): Remove file arg.
index 72f7a5f5c66f169485ac393f467e8c8743594a77..dbc0a539f3efe2ca493ff39a067091c0a3b43bf2 100644 (file)
@@ -43,6 +43,17 @@ typedef Py_intptr_t Py_ssize_t;
 #error "Unable to find usable Python.h"
 #endif
 
+/* If Python.h does not define WITH_THREAD, then the various
+   GIL-related functions will not be defined.  However,
+   PyGILState_STATE will be.  */
+#ifndef WITH_THREAD
+#define PyGILState_Ensure() ((PyGILState_STATE) 0)
+#define PyGILState_Release(ARG) (ARG)
+#define PyEval_InitThreads() 0
+#define PyThreadState_Swap(ARG) (ARG)
+#define PyEval_InitThreads() 0
+#endif
+
 struct value;
 
 extern PyObject *gdb_module;
@@ -57,6 +68,7 @@ struct value *convert_value_from_python (PyObject *obj);
 void gdbpy_initialize_values (void);
 
 struct cleanup *make_cleanup_py_decref (PyObject *py);
+struct cleanup *make_cleanup_py_restore_gil (PyGILState_STATE *state);
 
 /* Use this after a TRY_EXCEPT to throw the appropriate Python
    exception.  */
index 912845f248bd55ab6254bf6190cc70a1d7d9698c..8db81ecb85970c4b4d43ef0cf852c0422e732d34 100644 (file)
@@ -46,6 +46,23 @@ make_cleanup_py_decref (PyObject *py)
   return make_cleanup (py_decref, (void *) py);
 }
 
+/* A cleanup function to restore the thread state.  */
+
+static void
+py_gil_restore (void *p)
+{
+  PyGILState_STATE *state = p;
+  PyGILState_Release (*state);
+}
+
+/* Return a new cleanup which will restore the Python GIL state.  */
+
+struct cleanup *
+make_cleanup_py_restore_gil (PyGILState_STATE *state)
+{
+  return make_cleanup (py_gil_restore, state);
+}
+
 /* Converts a Python 8-bit string to a unicode string object.  Assumes the
    8-bit string is in the host charset.  If an error occurs during conversion,
    returns NULL with a python exception set.
index 77d8774ed6f94a63620da17e9893019a82247cd6..50277d45d85cc1933f26109b4141043f661ea60f 100644 (file)
@@ -103,10 +103,15 @@ void
 eval_python_from_control_command (struct command_line *cmd)
 {
   char *script;
+  struct cleanup *cleanup;
+  PyGILState_STATE state;
 
   if (cmd->body_count != 1)
     error (_("Invalid \"python\" block structure."));
 
+  state = PyGILState_Ensure ();
+  cleanup = make_cleanup_py_restore_gil (&state);
+
   script = compute_python_string (cmd->body_list[0]);
   PyRun_SimpleString (script);
   xfree (script);
@@ -115,6 +120,8 @@ eval_python_from_control_command (struct command_line *cmd)
       gdbpy_print_stack ();
       error (_("error while executing Python code"));
     }
+
+  do_cleanups (cleanup);
 }
 
 /* Implementation of the gdb "python" command.  */
@@ -122,6 +129,12 @@ eval_python_from_control_command (struct command_line *cmd)
 static void
 python_command (char *arg, int from_tty)
 {
+  struct cleanup *cleanup;
+  PyGILState_STATE state;
+
+  state = PyGILState_Ensure ();
+  cleanup = make_cleanup_py_restore_gil (&state);
+
   while (arg && *arg && isspace (*arg))
     ++arg;
   if (arg && *arg)
@@ -136,10 +149,11 @@ python_command (char *arg, int from_tty)
   else
     {
       struct command_line *l = get_command_line (python_control, "");
-      struct cleanup *cleanups = make_cleanup_free_command_lines (&l);
+      make_cleanup_free_command_lines (&l);
       execute_control_command_untraced (l);
-      do_cleanups (cleanups);
     }
+
+  do_cleanups (cleanup);
 }
 
 \f
@@ -392,6 +406,7 @@ Enables or disables printing of Python stack traces."),
 
 #ifdef HAVE_PYTHON
   Py_Initialize ();
+  PyEval_InitThreads ();
 
   gdb_module = Py_InitModule ("gdb", GdbMethods);
 
@@ -429,5 +444,10 @@ class GdbOutputFile:\n\
 sys.stderr = GdbOutputFile()\n\
 sys.stdout = GdbOutputFile()\n\
 ");
+
+  /* Release the GIL while gdb runs.  */
+  PyThreadState_Swap (NULL);
+  PyEval_ReleaseLock ();
+
 #endif /* HAVE_PYTHON */
 }