Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
* [PATCH] Add a timeout parameter to gdb_do_one_event
@ 2022-03-17 13:08 Patrick Monnerat via Gdb-patches
  2022-04-15 16:21 ` Tom Tromey
                   ` (2 more replies)
  0 siblings, 3 replies; 19+ messages in thread
From: Patrick Monnerat via Gdb-patches @ 2022-03-17 13:08 UTC (permalink / raw)
  To: gdb-patches

Since commit b2d8657, having a per-interpreter event/command loop is not
possible anymore.

As Insight uses a GUI that has its own event loop, gdb and GUI event
loops have then to be "merged" (i.e.: work together). But this is
problematic as gdb_do_one_event is not aware of this alternate event
loop and thus may wait forever.

The solution is to implement a wait timeout to gdb_do_one_event. This
cannot be done externally as gdb timers are event sources themselves.

The new parameter defaults to "no timeout": as it is used by Insight
only, there is no need to update calls from the gdb source tree.
---
 gdbsupport/event-loop.cc | 45 +++++++++++++++++++++++++++++++---------
 gdbsupport/event-loop.h  |  2 +-
 2 files changed, 36 insertions(+), 11 deletions(-)

diff --git a/gdbsupport/event-loop.cc b/gdbsupport/event-loop.cc
index 385b45b2de1..14168cae5a6 100644
--- a/gdbsupport/event-loop.cc
+++ b/gdbsupport/event-loop.cc
@@ -33,6 +33,8 @@
 #include <sys/types.h>
 #include "gdbsupport/gdb_sys_time.h"
 #include "gdbsupport/gdb_select.h"
+#include "gdbsupport/gdb_optional.h"
+#include "gdbsupport/scope-exit.h"
 
 /* See event-loop.h.  */
 
@@ -177,12 +179,17 @@ static int update_wait_timeout (void);
 static int poll_timers (void);
 \f
 /* Process one high level event.  If nothing is ready at this time,
-   wait for something to happen (via gdb_wait_for_event), then process
-   it.  Returns >0 if something was done otherwise returns <0 (this
-   can happen if there are no event sources to wait for).  */
+   wait at most MSTIMEOUT milliseconds for something to happen (via
+   gdb_wait_for_event), then process it.  Returns >0 if something was
+   done, <0 if there are no event sources to wait for, =0 if timeout occurred.
+   A timeout of 0 allows to serve an already pending event, but does not
+   wait if none found.
+   Setting the timeout to a negative value disables it.
+   The timeout is never used by gdb itself, it is however needed to
+   integrate gdb event handling within Insight's GUI event loop. */
 
 int
-gdb_do_one_event (void)
+gdb_do_one_event (int mstimeout)
 {
   static int event_source_head = 0;
   const int number_of_sources = 3;
@@ -229,17 +236,35 @@ gdb_do_one_event (void)
 	return 1;
     }
 
+  if (!mstimeout)
+    return 0;	/* Null timeout: do not wait for an event. */
+
   /* Block waiting for a new event.  If gdb_wait_for_event returns -1,
      we should get out because this means that there are no event
      sources left.  This will make the event loop stop, and the
-     application exit.  */
+     application exit.
+     If a timeout has been given, a new timer is set accordingly
+     to abort event wait.  It is deleted upon gdb_wait_for_event
+     termination and thus should never be triggered.
+     When the timeout is reached, events are not monitored again:
+     they already have been checked in the loop above. */
 
-  if (gdb_wait_for_event (1) < 0)
-    return -1;
+  gdb::optional<int> timer_id;
 
-  /* If gdb_wait_for_event has returned 1, it means that one event has
-     been handled.  We break out of the loop.  */
-  return 1;
+  SCOPE_EXIT 
+    {
+      if (timer_id.has_value ())
+	delete_timer (*timer_id);
+    };
+
+  if (mstimeout > 0)
+    timer_id = create_timer (mstimeout,
+			     [] (gdb_client_data arg)
+			     {
+			       ((gdb::optional<int> *) arg)->reset ();
+			     },
+			     &timer_id);
+  return gdb_wait_for_event (1);
 }
 
 /* See event-loop.h  */
diff --git a/gdbsupport/event-loop.h b/gdbsupport/event-loop.h
index 9ed592877ab..c82493e9bdf 100644
--- a/gdbsupport/event-loop.h
+++ b/gdbsupport/event-loop.h
@@ -76,7 +76,7 @@ typedef void (timer_handler_func) (gdb_client_data);
 
 /* Exported functions from event-loop.c */
 
-extern int gdb_do_one_event (void);
+extern int gdb_do_one_event (int mstimeout = -1);
 extern void delete_file_handler (int fd);
 
 /* Add a file handler/descriptor to the list of descriptors we are
-- 
2.35.1



^ permalink raw reply	[flat|nested] 19+ messages in thread
* [PATCH] Add a timeout parameter to gdb_do_one_event
@ 2022-03-14 14:49 Patrick Monnerat via Gdb-patches
  2022-03-14 16:17 ` Pedro Alves
  0 siblings, 1 reply; 19+ messages in thread
From: Patrick Monnerat via Gdb-patches @ 2022-03-14 14:49 UTC (permalink / raw)
  To: gdb-patches

Since commit b2d8657, having a per-interpreter event/command loop is not
possible anymore.

As Insight uses a GUI that has its own event loop, gdb and GUI event
loops have then to be "merged" (i.e.: work together). But this is
problematic as gdb_do_one_event is not aware of this alternate event
loop and thus may wait forever.

The solution is to implement a wait timeout to gdb_do_one_event. This
cannot be done externally as gdb timers are event sources themselves.

The new parameter defaults to "no timeout": as it is used by Insight
only, there is no need to update calls from the gdb source tree.
---
 gdbsupport/event-loop.cc | 55 +++++++++++++++++++++++++++++++---------
 gdbsupport/event-loop.h  |  2 +-
 2 files changed, 44 insertions(+), 13 deletions(-)

diff --git a/gdbsupport/event-loop.cc b/gdbsupport/event-loop.cc
index 385b45b2de1..1129f4cc84e 100644
--- a/gdbsupport/event-loop.cc
+++ b/gdbsupport/event-loop.cc
@@ -33,6 +33,7 @@
 #include <sys/types.h>
 #include "gdbsupport/gdb_sys_time.h"
 #include "gdbsupport/gdb_select.h"
+#include "gdbsupport/gdb_optional.h"
 
 /* See event-loop.h.  */
 
@@ -177,16 +178,22 @@ static int update_wait_timeout (void);
 static int poll_timers (void);
 \f
 /* Process one high level event.  If nothing is ready at this time,
-   wait for something to happen (via gdb_wait_for_event), then process
-   it.  Returns >0 if something was done otherwise returns <0 (this
-   can happen if there are no event sources to wait for).  */
+   wait at most MSTIMEOUT milliseconds for something to happen (via
+   gdb_wait_for_event), then process it.  Returns >0 if something was
+   done, <0 if there are no event sources to wait for, =0 if timeout occurred.
+   A timeout of 0 allows to serve an already pending event, but does not
+   wait if none found.
+   Setting the timeout to a negative value disables it.
+   The timeout is never used by gdb itself, it is however needed to
+   integrate gdb event handling within Insight's GUI event loop. */
 
 int
-gdb_do_one_event (void)
+gdb_do_one_event (int mstimeout)
 {
   static int event_source_head = 0;
   const int number_of_sources = 3;
   int current = 0;
+  int res = 0;
 
   /* First let's see if there are any asynchronous signal handlers
      that are ready.  These would be the result of invoking any of the
@@ -198,8 +205,6 @@ gdb_do_one_event (void)
      round-robin fashion.  */
   for (current = 0; current < number_of_sources; current++)
     {
-      int res;
-
       switch (event_source_head)
 	{
 	case 0:
@@ -232,14 +237,40 @@ gdb_do_one_event (void)
   /* Block waiting for a new event.  If gdb_wait_for_event returns -1,
      we should get out because this means that there are no event
      sources left.  This will make the event loop stop, and the
-     application exit.  */
+     application exit.
+     If a timeout has been given, a new timer is set accordingly
+     to abort event wait.  It is deleted upon gdb_wait_for_event
+     termination and thus should never be triggered.
+     When the timeout is reached, events are not monitored again:
+     they already have been checked in the loop above. */
+
+  res = 0;
+  if (mstimeout != 0)
+    {
+      gdb::optional<int> timer_id;
 
-  if (gdb_wait_for_event (1) < 0)
-    return -1;
+      try
+	{
+	  if (mstimeout > 0)
+	    timer_id = create_timer (mstimeout,
+				     [] (gdb_client_data arg)
+				     {
+				       ((gdb::optional<int> *) arg)->reset ();
+				     },
+				     &timer_id);
+	  res = gdb_wait_for_event (1);
+	  if (timer_id.has_value ())
+	    delete_timer (*timer_id);
+	}
+      catch (...)
+	{
+	  if (timer_id.has_value ())
+	    delete_timer (*timer_id);
+	  throw;
+	}
+    }
 
-  /* If gdb_wait_for_event has returned 1, it means that one event has
-     been handled.  We break out of the loop.  */
-  return 1;
+  return res;
 }
 
 /* See event-loop.h  */
diff --git a/gdbsupport/event-loop.h b/gdbsupport/event-loop.h
index 9ed592877ab..c82493e9bdf 100644
--- a/gdbsupport/event-loop.h
+++ b/gdbsupport/event-loop.h
@@ -76,7 +76,7 @@ typedef void (timer_handler_func) (gdb_client_data);
 
 /* Exported functions from event-loop.c */
 
-extern int gdb_do_one_event (void);
+extern int gdb_do_one_event (int mstimeout = -1);
 extern void delete_file_handler (int fd);
 
 /* Add a file handler/descriptor to the list of descriptors we are
-- 
2.35.1



^ permalink raw reply	[flat|nested] 19+ messages in thread
* [PATCH] Add a timeout parameter to gdb_do_one_event
@ 2021-08-26 18:30 Patrick Monnerat via Gdb-patches
  0 siblings, 0 replies; 19+ messages in thread
From: Patrick Monnerat via Gdb-patches @ 2021-08-26 18:30 UTC (permalink / raw)
  To: gdb-patches

Since commit b2d8657, having a per-interpreter event/command loop is not
possible anymore.

As Insight uses a GUI that has its own event loop, gdb and GUI event
loops have then to be "merged" (i.e.: work together). But this is
problematic as gdb_do_one_event is not aware of this alternate event
loop and thus may wait forever.

The solution is to implement a wait timeout to gdb_do_one_event. This
cannot be done externally as gdb timers are event sources themselves.

The new parameter defaults to "no timeout": as it is used by Insight
only, there is no need to update calls from the gdb source tree.
---
 gdbsupport/event-loop.cc | 80 ++++++++++++++++++++++++++++++++++------
 gdbsupport/event-loop.h  |  2 +-
 2 files changed, 69 insertions(+), 13 deletions(-)

diff --git a/gdbsupport/event-loop.cc b/gdbsupport/event-loop.cc
index 98d1ada52cd..7c99d8ccbb6 100644
--- a/gdbsupport/event-loop.cc
+++ b/gdbsupport/event-loop.cc
@@ -33,6 +33,7 @@
 #include <sys/types.h>
 #include "gdbsupport/gdb_sys_time.h"
 #include "gdbsupport/gdb_select.h"
+#include "gdbsupport/gdb_optional.h"
 
 /* See event-loop.h.  */
 
@@ -176,17 +177,64 @@ static int gdb_wait_for_event (int);
 static int update_wait_timeout (void);
 static int poll_timers (void);
 \f
+/* RAII class for the gdb_do_one_event timeout timer. */
+
+class scoped_event_wait_timeout
+{
+public:
+  scoped_event_wait_timeout ()
+    : m_timer_id ()
+  {}
+
+  ~scoped_event_wait_timeout ()
+  {
+    this->destroy ();
+  }
+
+  void reset ()
+  {
+    m_timer_id.reset ();
+  }
+
+  void destroy ()
+  {
+    if (m_timer_id.has_value ())
+      delete_timer (*m_timer_id);
+    this->reset ();
+  }
+
+  void start_timer (int timeout)
+  {
+    this->destroy ();
+    m_timer_id = create_timer (timeout,
+			       [] (gdb_client_data arg)
+			       {
+				 ((scoped_event_wait_timeout *) arg)->reset ();
+			       },
+			       this);
+  }
+
+private:
+  gdb::optional<int> m_timer_id;
+};
+
 /* Process one high level event.  If nothing is ready at this time,
-   wait for something to happen (via gdb_wait_for_event), then process
-   it.  Returns >0 if something was done otherwise returns <0 (this
-   can happen if there are no event sources to wait for).  */
+   wait at most MSTIMEOUT milliseconds for something to happen (via
+   gdb_wait_for_event), then process it.  Returns >0 if something was
+   done, <0 if there are no event sources to wait for, =0 if timeout occurred.
+   A timeout of 0 allows to serve an already pending event, but does not
+   wait if none found.
+   Setting the timeout to a negative value disables it.
+   The timeout is never used by gdb itself, it is however needed to
+   integrate gdb event handling within Insight's GUI event loop. */
 
 int
-gdb_do_one_event (void)
+gdb_do_one_event (int mstimeout)
 {
   static int event_source_head = 0;
   const int number_of_sources = 3;
   int current = 0;
+  int res = 0;
 
   /* First let's see if there are any asynchronous signal handlers
      that are ready.  These would be the result of invoking any of the
@@ -198,8 +246,6 @@ gdb_do_one_event (void)
      round-robin fashion.  */
   for (current = 0; current < number_of_sources; current++)
     {
-      int res;
-
       switch (event_source_head)
 	{
 	case 0:
@@ -232,14 +278,24 @@ gdb_do_one_event (void)
   /* Block waiting for a new event.  If gdb_wait_for_event returns -1,
      we should get out because this means that there are no event
      sources left.  This will make the event loop stop, and the
-     application exit.  */
+     application exit.
+     If a timeout has been given, a new timer is set accordingly
+     to abort event wait.  It is deleted upon gdb_wait_for_event
+     termination and thus should never be triggered.
+     When the timeout is reached, events are not monitored again:
+     they already have been checked in the loop above. */
+
+  res = 0;
+  if (mstimeout != 0)
+    {
+      scoped_event_wait_timeout timer_id;
 
-  if (gdb_wait_for_event (1) < 0)
-    return -1;
+      if (mstimeout > 0)
+	timer_id.start_timer (mstimeout);
+      res = gdb_wait_for_event (1);
+    }
 
-  /* If gdb_wait_for_event has returned 1, it means that one event has
-     been handled.  We break out of the loop.  */
-  return 1;
+  return res;
 }
 
 /* See event-loop.h  */
diff --git a/gdbsupport/event-loop.h b/gdbsupport/event-loop.h
index dc4e4d59f03..cf62f654c1c 100644
--- a/gdbsupport/event-loop.h
+++ b/gdbsupport/event-loop.h
@@ -76,7 +76,7 @@ typedef void (timer_handler_func) (gdb_client_data);
 
 /* Exported functions from event-loop.c */
 
-extern int gdb_do_one_event (void);
+extern int gdb_do_one_event (int mstimeout = -1);
 extern void delete_file_handler (int fd);
 
 /* Add a file handler/descriptor to the list of descriptors we are
-- 
2.31.1


^ permalink raw reply	[flat|nested] 19+ messages in thread
* [PATCH] Add a timeout parameter to gdb_do_one_event
@ 2021-08-23 18:23 Patrick Monnerat via Gdb-patches
  2021-08-26  3:24 ` Simon Marchi via Gdb-patches
  0 siblings, 1 reply; 19+ messages in thread
From: Patrick Monnerat via Gdb-patches @ 2021-08-23 18:23 UTC (permalink / raw)
  To: gdb-patches

Since commit b2d8657, having a per-interpreter event/command loop is not
possible anymore.

As Insight uses a GUI that has its own event loop, gdb and GUI event
loops have then to be "merged" (i.e.: work together). But this is
problematic as gdb_do_one_event is not aware of this alternate event
loop and thus may wait forever.

The solution is to implement a wait timeout to gdb_do_one_event. This
cannot be done externally as timers are event sources themselves.

The new parameter defaults to "no timeout": as it is used by Insight
only, there is no need to update calls from the gdb source tree.
---
 gdbsupport/event-loop.cc | 45 ++++++++++++++++++++++++++++------------
 gdbsupport/event-loop.h  |  2 +-
 2 files changed, 33 insertions(+), 14 deletions(-)

diff --git a/gdbsupport/event-loop.cc b/gdbsupport/event-loop.cc
index 98d1ada52cd..72c64dcdb72 100644
--- a/gdbsupport/event-loop.cc
+++ b/gdbsupport/event-loop.cc
@@ -177,16 +177,21 @@ static int update_wait_timeout (void);
 static int poll_timers (void);
 \f
 /* Process one high level event.  If nothing is ready at this time,
-   wait for something to happen (via gdb_wait_for_event), then process
-   it.  Returns >0 if something was done otherwise returns <0 (this
-   can happen if there are no event sources to wait for).  */
+   wait at most mstimeout milliseconds for something to happen (via
+   gdb_wait_for_event), then process it.  Returns >0 if something was
+   done, <0 if there are no event sources to wait for, =0 if timeout occurred.
+   Setting the timeout to a negative value disables it.
+   The timeout is never used by gdb itself, it is however needed to
+   integrate gdb event handling within some external (i.e.: GUI) event
+   loop. */
 
 int
-gdb_do_one_event (void)
+gdb_do_one_event (int mstimeout)
 {
   static int event_source_head = 0;
   const int number_of_sources = 3;
   int current = 0;
+  int res = 0;
 
   /* First let's see if there are any asynchronous signal handlers
      that are ready.  These would be the result of invoking any of the
@@ -198,8 +203,6 @@ gdb_do_one_event (void)
      round-robin fashion.  */
   for (current = 0; current < number_of_sources; current++)
     {
-      int res;
-
       switch (event_source_head)
 	{
 	case 0:
@@ -232,14 +235,30 @@ gdb_do_one_event (void)
   /* Block waiting for a new event.  If gdb_wait_for_event returns -1,
      we should get out because this means that there are no event
      sources left.  This will make the event loop stop, and the
-     application exit.  */
-
-  if (gdb_wait_for_event (1) < 0)
-    return -1;
+     application exit.
+     If a timeout has been given, a new timer is set accordingly
+     to abort event wait.  It is deleted upon gdb_wait_for_event
+     termination and thus should never be triggered.
+     When the timeout is reached, events are not monitored again:
+     they already have been checked in the loop above. */
+
+  if (mstimeout != 0)
+    {
+      int timerid = 0;
+
+      if (mstimeout > 0)
+	timerid = create_timer (mstimeout,
+				[] (gdb_client_data timeridp)
+				  {
+				    *((int *) timeridp) = 0;
+				  },
+				&timerid);
+      res = gdb_wait_for_event (1);
+      if (timerid)
+	delete_timer (timerid);
+    }
 
-  /* If gdb_wait_for_event has returned 1, it means that one event has
-     been handled.  We break out of the loop.  */
-  return 1;
+  return res;
 }
 
 /* See event-loop.h  */
diff --git a/gdbsupport/event-loop.h b/gdbsupport/event-loop.h
index dc4e4d59f03..cf62f654c1c 100644
--- a/gdbsupport/event-loop.h
+++ b/gdbsupport/event-loop.h
@@ -76,7 +76,7 @@ typedef void (timer_handler_func) (gdb_client_data);
 
 /* Exported functions from event-loop.c */
 
-extern int gdb_do_one_event (void);
+extern int gdb_do_one_event (int mstimeout = -1);
 extern void delete_file_handler (int fd);
 
 /* Add a file handler/descriptor to the list of descriptors we are
-- 
2.31.1


^ permalink raw reply	[flat|nested] 19+ messages in thread

end of thread, other threads:[~2022-08-23 18:38 UTC | newest]

Thread overview: 19+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-17 13:08 [PATCH] Add a timeout parameter to gdb_do_one_event Patrick Monnerat via Gdb-patches
2022-04-15 16:21 ` Tom Tromey
2022-04-16  0:38   ` Patrick Monnerat via Gdb-patches
2022-07-22 13:41 ` Simon Marchi via Gdb-patches
2022-07-22 22:45   ` Patrick Monnerat via Gdb-patches
2022-07-25  1:07     ` Simon Marchi via Gdb-patches
2022-08-18 11:16 ` Andrew Burgess via Gdb-patches
2022-08-19 11:29   ` Patrick Monnerat via Gdb-patches
2022-08-23 18:38     ` Tom Tromey
  -- strict thread matches above, loose matches on Subject: below --
2022-03-14 14:49 Patrick Monnerat via Gdb-patches
2022-03-14 16:17 ` Pedro Alves
2021-08-26 18:30 Patrick Monnerat via Gdb-patches
2021-08-23 18:23 Patrick Monnerat via Gdb-patches
2021-08-26  3:24 ` Simon Marchi via Gdb-patches
2021-08-26 11:36   ` Patrick Monnerat via Gdb-patches
2021-08-26 13:47     ` Simon Marchi via Gdb-patches
2021-08-26 15:14       ` Patrick Monnerat via Gdb-patches
2021-08-27 18:08     ` Tom Tromey
2021-08-28  0:07       ` Patrick Monnerat via Gdb-patches

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox