From patchwork Tue Oct 1 20:12:27 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 34778 Received: (qmail 42307 invoked by alias); 1 Oct 2019 20:12:45 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 42239 invoked by uid 89); 1 Oct 2019 20:12:44 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-21.3 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy=Submit, 94063, Wake, wake X-HELO: gateway23.websitewelcome.com Received: from gateway23.websitewelcome.com (HELO gateway23.websitewelcome.com) (192.185.49.218) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Tue, 01 Oct 2019 20:12:40 +0000 Received: from cm10.websitewelcome.com (cm10.websitewelcome.com [100.42.49.4]) by gateway23.websitewelcome.com (Postfix) with ESMTP id 7393B456B for ; Tue, 1 Oct 2019 15:12:38 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id FOViiJFLgHunhFOVii1HZp; Tue, 01 Oct 2019 15:12:38 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=4YxJ4Hswz1PzKJGpcY6gRgMMU9iQFkQp0zOYJmXHK5E=; b=xc3TtFtvztM6RcljbMB5GJHXpQ AflQtEWmH6tm4jwblZcLf6XPgsCvv1ucAPqlXxswu4dUwIAIIMSMfasqhUGNJO/L/G2cT3pN7deKI Dfe7Dhn6mtNYkEiGQ3sKJtTwf; Received: from 75-166-72-156.hlrn.qwest.net ([75.166.72.156]:47310 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.92) (envelope-from ) id 1iFOVh-0023p5-Ia; Tue, 01 Oct 2019 14:12:37 -0600 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v4 11/11] Use run_on_main_thread in gdb.post_event Date: Tue, 1 Oct 2019 14:12:27 -0600 Message-Id: <20191001201227.8519-12-tom@tromey.com> In-Reply-To: <20191001201227.8519-1-tom@tromey.com> References: <20191001201227.8519-1-tom@tromey.com> This changes gdb.post_event to use the new run_on_main_thread function. This is somewhat tricky because the Python GIL must be held while manipulating reference counts. gdb/ChangeLog 2019-10-01 Tom Tromey * python/python.c (class gdbpy_gil): New. (struct gdbpy_event): Add constructor, destructor, operator(). (gdbpy_post_event): Use run_on_main_thread. (gdbpy_initialize_events): Remove. (do_start_initialization): Update. --- gdb/ChangeLog | 8 +++ gdb/python/python.c | 131 +++++++++++++++++++++----------------------- 2 files changed, 70 insertions(+), 69 deletions(-) diff --git a/gdb/python/python.c b/gdb/python/python.c index ddf0e72d26f..1e054578900 100644 --- a/gdb/python/python.c +++ b/gdb/python/python.c @@ -28,7 +28,6 @@ #include "value.h" #include "language.h" #include "event-loop.h" -#include "serial.h" #include "readline/tilde.h" #include "python.h" #include "extension-priv.h" @@ -940,63 +939,81 @@ gdbpy_source_script (const struct extension_language_defn *extlang, /* Posting and handling events. */ +/* A helper class to save and restore the GIL, but without touching + the other globals that are handled by gdbpy_enter. */ + +class gdbpy_gil +{ +public: + + gdbpy_gil () + : m_state (PyGILState_Ensure ()) + { + } + + ~gdbpy_gil () + { + PyGILState_Release (m_state); + } + + DISABLE_COPY_AND_ASSIGN (gdbpy_gil); + +private: + + PyGILState_STATE m_state; +}; + /* A single event. */ struct gdbpy_event { - /* The Python event. This is just a callable object. */ - PyObject *event; - /* The next event. */ - struct gdbpy_event *next; -}; + gdbpy_event (gdbpy_ref<> &&func) + : m_func (func.release ()) + { + } -/* All pending events. */ -static struct gdbpy_event *gdbpy_event_list; -/* The final link of the event list. */ -static struct gdbpy_event **gdbpy_event_list_end; + gdbpy_event (gdbpy_event &&other) + : m_func (other.m_func) + { + other.m_func = nullptr; + } -/* So that we can wake up the main thread even when it is blocked in - poll(). */ -static struct serial_event *gdbpy_serial_event; + gdbpy_event (const gdbpy_event &other) + : m_func (other.m_func) + { + gdbpy_gil gil; + Py_XINCREF (m_func); + } -/* The file handler callback. This reads from the internal pipe, and - then processes the Python event queue. This will always be run in - the main gdb thread. */ + ~gdbpy_event () + { + gdbpy_gil gil; + Py_XDECREF (m_func); + } -static void -gdbpy_run_events (int error, gdb_client_data client_data) -{ - gdbpy_enter enter_py (get_current_arch (), current_language); + gdbpy_event &operator= (const gdbpy_event &other) = delete; - /* Clear the event fd. Do this before flushing the events list, so - that any new event post afterwards is sure to re-awake the event - loop. */ - serial_event_clear (gdbpy_serial_event); + void operator() () + { + gdbpy_enter enter_py (get_current_arch (), current_language); - while (gdbpy_event_list) - { - /* Dispatching the event might push a new element onto the event - loop, so we update here "atomically enough". */ - struct gdbpy_event *item = gdbpy_event_list; - gdbpy_event_list = gdbpy_event_list->next; - if (gdbpy_event_list == NULL) - gdbpy_event_list_end = &gdbpy_event_list; - - gdbpy_ref<> call_result (PyObject_CallObject (item->event, NULL)); - if (call_result == NULL) - gdbpy_print_stack (); + gdbpy_ref<> call_result (PyObject_CallObject (m_func, NULL)); + if (call_result == NULL) + gdbpy_print_stack (); + } - Py_DECREF (item->event); - xfree (item); - } -} +private: + + /* The Python event. This is just a callable object. Note that + this is not a gdbpy_ref<>, because we have to take particular + care to only destroy the reference when holding the GIL. */ + PyObject *m_func; +}; /* Submit an event to the gdb thread. */ static PyObject * gdbpy_post_event (PyObject *self, PyObject *args) { - struct gdbpy_event *event; PyObject *func; - int wakeup; if (!PyArg_ParseTuple (args, "O", &func)) return NULL; @@ -1010,36 +1027,13 @@ gdbpy_post_event (PyObject *self, PyObject *args) Py_INCREF (func); - /* From here until the end of the function, we have the GIL, so we - can operate on our global data structures without worrying. */ - wakeup = gdbpy_event_list == NULL; - - event = XNEW (struct gdbpy_event); - event->event = func; - event->next = NULL; - *gdbpy_event_list_end = event; - gdbpy_event_list_end = &event->next; - - /* Wake up gdb when needed. */ - if (wakeup) - serial_event_set (gdbpy_serial_event); + gdbpy_ref<> func_ref = gdbpy_ref<>::new_reference (func); + gdbpy_event event (std::move (func_ref)); + run_on_main_thread (event); Py_RETURN_NONE; } -/* Initialize the Python event handler. */ -static int -gdbpy_initialize_events (void) -{ - gdbpy_event_list_end = &gdbpy_event_list; - - gdbpy_serial_event = make_serial_event (); - add_file_handler (serial_event_fd (gdbpy_serial_event), - gdbpy_run_events, NULL); - - return 0; -} - /* This is the extension_language_ops.before_prompt "method". */ @@ -1704,7 +1698,6 @@ do_start_initialization () || gdbpy_initialize_linetable () < 0 || gdbpy_initialize_thread () < 0 || gdbpy_initialize_inferior () < 0 - || gdbpy_initialize_events () < 0 || gdbpy_initialize_eventregistry () < 0 || gdbpy_initialize_py_events () < 0 || gdbpy_initialize_event () < 0