From b2452bfc139a3e5e4c7b4714e68f0839ca196858 Mon Sep 17 00:00:00 2001
From: deuce <>
Date: Mon, 11 Aug 2003 23:42:42 +0000
Subject: [PATCH] Change od_sleep() behaviour to select STDIN then call
 od_kernel() if there is data waiting.

od_kernel is now ran once every second rather than every 250 ms (One second
granularity is fine for all other kernel tasks afaik)
---
 src/odoors/ODInQue.c |  4 ++++
 src/odoors/ODKrnl.c  |  6 +++---
 src/odoors/ODPlat.c  | 14 +++++++++-----
 3 files changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/odoors/ODInQue.c b/src/odoors/ODInQue.c
index a2c0a7d40c..46c49484bc 100644
--- a/src/odoors/ODInQue.c
+++ b/src/odoors/ODInQue.c
@@ -304,6 +304,10 @@ tODResult ODInQueueGetNextEvent(tODInQueueHandle hInQueue,
    tODInputEvent *pEvent, tODMilliSec Timeout)
 {
    tInputQueueInfo *pInputQueueInfo = ODHANDLE2PTR(hInQueue, tInputQueueInfo);
+#ifdef ODPLAT_NIX
+   struct timeval tv;
+   fd_set in;
+#endif
 
    ASSERT(pInputQueueInfo != NULL);
    ASSERT(pEvent != NULL);
diff --git a/src/odoors/ODKrnl.c b/src/odoors/ODKrnl.c
index 90853657d3..aa070f08f3 100644
--- a/src/odoors/ODKrnl.c
+++ b/src/odoors/ODKrnl.c
@@ -185,13 +185,13 @@ tODResult ODKrnlInitialize(void)
    sigaddset(&block,SIGHUP);
    sigprocmask(SIG_BLOCK,&block,NULL);
 
-   /* Run kernel on SIGALRM */
+   /* Run kernel on SIGALRM (Every 1 second) */
    act.sa_handler=sig_run_kernel;
    act.sa_flags=0;
    sigemptyset(&(act.sa_mask));
    sigaction(SIGALRM,&act,NULL);
-   itv.it_interval.tv_sec=0;
-   itv.it_interval.tv_usec=250000;
+   itv.it_interval.tv_sec=1;
+   itv.it_interval.tv_usec=0;
    itv.it_value.tv_sec=0;
    itv.it_value.tv_usec=250000;
    setitimer(ITIMER_REAL,&itv,NULL);
diff --git a/src/odoors/ODPlat.c b/src/odoors/ODPlat.c
index 12c924f3c3..b60a1a297a 100644
--- a/src/odoors/ODPlat.c
+++ b/src/odoors/ODPlat.c
@@ -779,6 +779,7 @@ ODAPIDEF void ODCALL od_sleep(tODMilliSec Milliseconds)
 {
 #ifdef ODPLAT_NIX
    struct timeval tv;
+   fd_set in;
 #endif
    /* Log function entry if running in trace mode. */
    TRACE(TRACE_API, "od_sleep()");
@@ -810,13 +811,16 @@ ODAPIDEF void ODCALL od_sleep(tODMilliSec Milliseconds)
 #endif /* ODPLAT_WIN32 */
 
 #ifdef ODPLAT_NIX
-   /* Prevent 100% CPU usage! */
-   if(Milliseconds==0)
-      Milliseconds=1;
-	  
+   FD_ZERO(&in);
    tv.tv_sec=Milliseconds/1000;
    tv.tv_usec=(Milliseconds%1000)*1000;
-   select(0,NULL,NULL,NULL,&tv);
+   if(Milliseconds==0)  {
+      tv.tv_usec=1000;
+      FD_SET(1,&in);
+   }
+
+   if(select(2,Milliseconds?NULL:&in,NULL,NULL,&tv)>0)
+      od_kernel();
 #endif
 
    OD_API_EXIT();
-- 
GitLab