From dcfeb97c9a6d751c7f1548a59a0fc38d51fc7dcc Mon Sep 17 00:00:00 2001
From: ~byakuren
Date: Tue, 23 Sep 2025 23:37:38 +0100
Subject: Fix 6!:3 (delay) consuming 100% CPU on FreeBSD

The delay function was busy-waiting instead of properly sleeping due to
two issues in the futex-based sleep implementation:

1. The futex wait was checking for a non-existent value (waitval|1 instead
   of waitval), causing immediate returns and busy-waiting
2. The loop was exiting on timeout instead of continuing for the full duration

This fix ensures the delay function:
- Properly yields CPU time (now uses <1% CPU instead of 100%)
- Runs for the full specified duration
- Remains interruptible every second as designed
---
 jsrc/mt.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/jsrc/mt.c b/jsrc/mt.c
index a4b745a5..ec1ed29b 100644
--- a/jsrc/mt.c
+++ b/jsrc/mt.c
@@ -244,10 +244,10 @@ C jtjsleep(J jt,UI ns){
   UI4 waitval=lda(&ftx);
   if(unlikely(BETWEENC(lda(&JT(jt,systemlock)),1,2))){jtsystemlockaccept(jt,LOCKALL);goto retime;} //systemlock can take a long time
   if(unlikely(lda(&JT(jt,adbreak)[0])>1)){r=EVBREAK;break;}   // ignore ATTN, but exit on BREAK
-  I i=jfutex_waitn(&ftx,waitval|1,MIN(ns,1000000000));  // the 1 is PYXWAIT defined in ct.c - we wait for the state to move off WAIT state
+  I i=jfutex_waitn(&ftx,waitval,MIN(ns,1000000000));  // wait for the state to change from current value
    // ATTN doesn't wake us, so we have to sleep at most 1s at a time
   if(unlikely(i>0)){r=EVFACE;break;}
-  if(i==-1){r=0;break;} //timed out
+  if(i==-1){goto retime;} //timed out - recalculate remaining time and continue
 retime:
   if(-1ull==(ns=jtmdif(tgt))){r=0;break;}}  // recalculate time-to-target
  CLRFUTEXWT;
-- 
2.51.2

