This is an automated email from Gerrit.

"Lucien Buchmann <lucien.buchm...@dufour.aero>" just uploaded a new patch set 
to Gerrit, which you can find at https://review.openocd.org/c/openocd/+/8930

-- gerrit

commit f53ef91274c8d702db49382634534e15bf1b9f0e
Author: Lucien Dufour <lucien.buchm...@dufour.aero>
Date:   Wed May 21 19:38:26 2025 +0200

    target: Fix keep_alive() calls in target_wait_state()
    
    Previously, after 500ms, the keep_alive() function was
    called continuously until the timeout was reached.
    
    Change-Id: I99b6bb60c0a36ab976619fbadfcb1d5e674b5ad2
    Signed-off-by: Lucien Dufour <lucien.buchm...@dufour.aero>

diff --git a/src/target/target.c b/src/target/target.c
index 6653c381c0..be1134ffed 100644
--- a/src/target/target.c
+++ b/src/target/target.c
@@ -3212,46 +3212,45 @@ COMMAND_HANDLER(handle_wait_halt_command)
        return target_wait_state(target, TARGET_HALTED, ms);
 }
 
-/* wait for target state to change. The trick here is to have a low
- * latency for short waits and not to suck up all the CPU time
- * on longer waits.
- *
- * After 500ms, keep_alive() is invoked
+/* Wait for the target's state to match the desired state.
+ * The trick here is to have a low latency for short waits and
+ * not to suck up all the CPU time on longer waits.
+ * Every 500ms, a keep_alive() is invoked to avoid GDB timeouts.
  */
-int target_wait_state(struct target *target, enum target_state state, unsigned 
int ms)
+int target_wait_state(struct target *target, enum target_state state, unsigned 
int timeout_ms)
 {
        int retval;
-       int64_t then = 0, cur;
        bool once = true;
+       int64_t since = timeval_ms();
+       int64_t last_alive = timeval_ms();
+       int64_t now = timeval_ms();
 
-       for (;;) {
+       while ((now - since) < (int64_t)timeout_ms) {
                retval = target_poll(target);
                if (retval != ERROR_OK)
                        return retval;
                if (target->state == state)
-                       break;
-               cur = timeval_ms();
+                       return ERROR_OK;
+
                if (once) {
                        once = false;
-                       then = timeval_ms();
                        LOG_DEBUG("waiting for target %s...",
                                nvp_value2name(nvp_target_state, state)->name);
                }
 
-               if (cur - then > 500) {
+               now = timeval_ms();
+               if (now - last_alive > 500) {
                        keep_alive();
+                       last_alive = timeval_ms();
                        if (openocd_is_shutdown_pending())
                                return ERROR_SERVER_INTERRUPTED;
                }
-
-               if ((cur-then) > ms) {
-                       LOG_ERROR("timed out while waiting for target %s",
-                               nvp_value2name(nvp_target_state, state)->name);
-                       return ERROR_FAIL;
-               }
        }
 
-       return ERROR_OK;
+       LOG_ERROR("timed out (%d ms) while waiting for target %s",
+               timeout_ms,
+               nvp_value2name(nvp_target_state, state)->name);
+       return ERROR_FAIL;
 }
 
 COMMAND_HANDLER(handle_halt_command)

-- 

Reply via email to