From c31e8b8c1742c8e23639141a35f6e5bdcccab113 Mon Sep 17 00:00:00 2001 From: rerobika Date: Wed, 28 Jun 2017 11:26:45 +0200 Subject: [PATCH] Add timeout to jerry_debugger_receive (#1885) Fixed when debug server uses a CPU core on 100%. JerryScript-DCO-1.0-Signed-off-by: Robert Fancsik frobert@inf.u-szeged.hu --- jerry-core/CMakeLists.txt | 10 ++++++++++ jerry-core/debugger/debugger.c | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/jerry-core/CMakeLists.txt b/jerry-core/CMakeLists.txt index 6912c4f12..1670e80ff 100644 --- a/jerry-core/CMakeLists.txt +++ b/jerry-core/CMakeLists.txt @@ -181,6 +181,16 @@ if(FEATURE_DEBUGGER) message(FATAL_ERROR "This configuration is not supported. Please build against your system libc to enable the JerryScript debugger.") endif() + # Sleep function availability check + INCLUDE (CheckIncludeFiles) + CHECK_INCLUDE_FILES (time.h HAVE_TIME_H) + CHECK_INCLUDE_FILES (unistd.h HAVE_UNISTD_H) + if(HAVE_TIME_H) + set(DEFINES_JERRY ${DEFINES_JERRY} HAVE_TIME_H) + elseif(HAVE_UNISTD_H) + set(DEFINES_JERRY ${DEFINES_JERRY} HAVE_UNISTD_H) + endif() + set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_DEBUGGER) set(DEFINES_JERRY ${DEFINES_JERRY} JERRY_DEBUGGER_PORT=${FEATURE_DEBUGGER_PORT}) endif() diff --git a/jerry-core/debugger/debugger.c b/jerry-core/debugger/debugger.c index 4a202a10b..068bc02f8 100644 --- a/jerry-core/debugger/debugger.c +++ b/jerry-core/debugger/debugger.c @@ -13,6 +13,8 @@ * limitations under the License. */ +#define _XOPEN_SOURCE 500 /* Required macro for sleep functions */ + #include "byte-code.h" #include "debugger.h" #include "ecma-builtin-helpers.h" @@ -25,6 +27,12 @@ #ifdef JERRY_DEBUGGER +#ifdef HAVE_TIME_H +#include +#elif defined (HAVE_UNISTD_H) +#include +#endif /* HAVE_TIME_H */ + /** * Type cast the debugger send buffer into a specific type. */ @@ -37,6 +45,11 @@ #define JERRY_DEBUGGER_RECEIVE_BUFFER_AS(type, name_p) \ type *name_p = ((type *) recv_buffer_p) +/** + * Sleep time in milliseconds between each jerry_debugger_receive call + */ +#define JERRY_DEBUGGER_TIMEOUT 100 + /** * Free all unreferenced byte code structures which * were not acknowledged by the debugger client. @@ -201,6 +214,24 @@ jerry_debugger_send_eval (const lit_utf8_byte_t *eval_string_p, /**< evaluated s return success; } /* jerry_debugger_send_eval */ +/** + * Suspend execution for a given time. + * Note: If the platform does not have nanosleep or usleep, this function does not sleep at all. + */ +static void +jerry_debugger_sleep (unsigned milliseconds) /**< suspending time */ +{ +#ifdef HAVE_TIME_H + nanosleep (&(const struct timespec) + { + milliseconds / 1000, (milliseconds % 1000) * 1000000L /* Seconds, nanoseconds */ + } + , NULL); +#elif defined (HAVE_UNISTD_H) + usleep ((useconds_t) milliseconds * 1000); +#endif /* HAVE_TIME_H */ +} /* jerry_debugger_sleep */ + /** * Check received packet size. */ @@ -514,6 +545,7 @@ jerry_debugger_breakpoint_hit (uint8_t message_type) /**< message type */ while (!jerry_debugger_receive ()) { + jerry_debugger_sleep (JERRY_DEBUGGER_TIMEOUT); } JERRY_CONTEXT (debugger_flags) = (uint8_t) (JERRY_CONTEXT (debugger_flags) & ~JERRY_DEBUGGER_BREAKPOINT_MODE);