Hi Søren,

I'm began a little patch to give such a wake-up callback (attached).
Is this something you all would accept?

It adds a new public function v8::Debug::Poll() which is to be called
from the main thread occasionally. It also adds an optional third
argument to v8::Debug::EnableAgent() which is a callback. The callback
(called "WantPollHandler") is executed from the Agent thread. The user
can use that callback to wake up the main thread to enter V8 (by
calling Poll()).

The patch currently untested, but I can probably put a unit test
together using the Semaphore's Signal() and Wait()

ry

--~--~---------~--~----~------------~-------~--~----~
v8-users mailing list
[email protected]
http://groups.google.com/group/v8-users
-~----------~----~----~----~------~----~------~--~---

Index: include/v8-debug.h
===================================================================
--- include/v8-debug.h	(revision 3035)
+++ include/v8-debug.h	(working copy)
@@ -187,6 +187,13 @@
    * Debug host dispatch callback function.
    */
   typedef void (*HostDispatchHandler)();
+  
+  /**
+   * A callback made by the DebuggerAgent thread when it has received a
+   * message which might need processing by V8. The user should, through
+   * some means, wake up the main thread and then call Debug::Poll().
+   */
+  typedef void (*WantPollHandler)();
 
   // Set a C debug event listener.
   static bool SetDebugEventListener(EventCallback that,
@@ -241,8 +248,17 @@
   * supplied TCP/IP port for remote debugger connection.
   * \param name the name of the embedding application
   * \param port the TCP/IP port to listen on
+  * \param handler a callback which is executed asynchornously from the main
+  * thread 
   */
-  static bool EnableAgent(const char* name, int port);
+  static bool EnableAgent(const char* name, int port,
+                          WantPollHandler handler = NULL);
+
+  /**
+   * Give control back to V8. Call this only from the main thread, usually
+   * soon after the WantPollHandler has been called.
+   */
+  static void Poll();
 };
 
 
Index: src/api.cc
===================================================================
--- src/api.cc	(revision 3035)
+++ src/api.cc	(working copy)
@@ -3685,9 +3685,17 @@
 }
 
 
-bool Debug::EnableAgent(const char* name, int port) {
-  return i::Debugger::StartAgent(name, port);
+bool Debug::EnableAgent(const char* name, int port,
+                        v8::Debug::WantPollHandler handler) {
+  return i::Debugger::StartAgent(name, port, handler);
 }
+
+
+void Debug::Poll() {
+  EnsureInitialized("v8::Debug::Poll");
+  ENTER_V8;
+  return i::Debugger::Poll();
+}
 #endif  // ENABLE_DEBUGGER_SUPPORT
 
 namespace internal {
Index: src/debug.h
===================================================================
--- src/debug.h	(revision 3035)
+++ src/debug.h	(working copy)
@@ -641,11 +641,15 @@
                              bool* pending_exception);
 
   // Start the debugger agent listening on the provided port.
-  static bool StartAgent(const char* name, int port);
+  static bool StartAgent(const char* name, int port,
+                         v8::Debug::WantPollHandler handler);
 
   // Stop the debugger agent.
   static void StopAgent();
 
+  // Public function to enter the debugger.
+  static void Poll();
+
   // Blocks until the agent has started listening for connections
   static void WaitForAgent();
 
@@ -684,6 +688,7 @@
   static bool never_unload_debugger_;  // Can we unload the debugger?
   static v8::Debug::MessageHandler2 message_handler_;
   static bool debugger_unload_pending_;  // Was message handler cleared?
+  static v8::Debug::WantPollHandler want_poll_handler_;
   static v8::Debug::HostDispatchHandler host_dispatch_handler_;
   static int host_dispatch_micros_;
 
Index: src/debug.cc
===================================================================
--- src/debug.cc	(revision 3035)
+++ src/debug.cc	(working copy)
@@ -1757,6 +1757,7 @@
 bool Debugger::never_unload_debugger_ = false;
 v8::Debug::MessageHandler2 Debugger::message_handler_ = NULL;
 bool Debugger::debugger_unload_pending_ = false;
+v8::Debug::WantPollHandler Debugger::want_poll_handler_ = NULL;
 v8::Debug::HostDispatchHandler Debugger::host_dispatch_handler_ = NULL;
 int Debugger::host_dispatch_micros_ = 100 * 1000;
 DebuggerAgent* Debugger::agent_ = NULL;
@@ -2415,6 +2416,10 @@
   command_queue_.Put(message);
   command_received_->Signal();
 
+  if (Debugger::want_poll_handler_) {
+    Debugger::want_poll_handler_();
+  }
+
   // Set the debug command break flag to have the command processed.
   if (!Debug::InDebugger()) {
     StackGuard::DebugCommand();
@@ -2461,8 +2466,10 @@
 }
 
 
-bool Debugger::StartAgent(const char* name, int port) {
+bool Debugger::StartAgent(const char* name, int port, 
+                          v8::Debug::WantPollHandler handler) {
   if (Socket::Setup()) {
+    Debugger::want_poll_handler_ = handler;
     agent_ = new DebuggerAgent(name, port);
     agent_->Start();
     return true;
@@ -2482,11 +2489,17 @@
 }
 
 
+void Debugger::Poll() {
+  EnterDebugger debugger;
+}
+
+
 void Debugger::WaitForAgent() {
   if (agent_ != NULL)
     agent_->WaitUntilListening();
 }
 
+
 MessageImpl MessageImpl::NewEvent(DebugEvent event,
                                   bool running,
                                   Handle<JSObject> exec_state,

Reply via email to