Re: Sleep on Windows systems

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Re: Sleep on Windows systems

Paul Kienzle-2
We don't need to be that sophisticated since we are not event
driven.  Seems to me their code is wrong, too.  As well as
sleeping for the entire time, are they not also "sleeping"
for the time required to process the event queue?

What I need is a test of whether I am to use the Windows API
or if there is some underlying posix layer I can rely upon.

How about (untested):

        #if defined(__WIN32__) && !defined(__POSIX__)

- Paul

On Wed, Oct 02, 2002 at 09:37:03AM -0500, John W. Eaton wrote:

> Here's what R does, in a Windows-specific source file:
>
>   SEXP do_syssleep(SEXP call, SEXP op, SEXP args, SEXP rho)
>   {    DWORD mtime;
>       int ntime;
>       double time;
>
>       checkArity(op, args);
>       time = asReal(CAR(args));
>       if (ISNAN(time) || time < 0)
>  errorcall(call, "invalid time value");
>       ntime = 1000*(time) + 0.5;
>       while (ntime > 0) {
>  mtime = min(500, ntime);
>  ntime -= mtime;
>  Sleep(mtime);
>  R_ProcessEvents();
>       }
>       return R_NilValue;
>   }
>
> So maybe we should just do something similar in sysdep.cc inside an
> #ifdef for WIN32 (or whatever is the appropriate macro).
>
> jwe


Reply | Threaded
Open this post in threaded view
|

Re: Sleep on Windows systems

John W. Eaton-6
On  2-Oct-2002, Paul Kienzle <[hidden email]> wrote:

| We don't need to be that sophisticated since we are not event
| driven.  Seems to me their code is wrong, too.  As well as
| sleeping for the entire time, are they not also "sleeping"
| for the time required to process the event queue?

Yes, I suppose they assume it is only a small fraction of the total
time, but that may not always be valid.

Anyway, my point was just that they don't test for Sleep, they just
assume it is present on Windows systems.  So that seems easier and not
as confusing as trying to make autoconf test for a mixed-case name.

How about the following change?

jwe


2002-10-02  John W. Eaton  <[hidden email]>

        * cutils.c (octave_sleep): Handle Windows, which may not have
        sleep, but should have Sleep.


Index: cutils.c
===================================================================
RCS file: /usr/local/cvsroot/octave/src/cutils.c,v
retrieving revision 1.8
diff -u -r1.8 cutils.c
--- cutils.c 26 Sep 2002 22:43:25 -0000 1.8
+++ cutils.c 3 Oct 2002 03:12:31 -0000
@@ -74,7 +74,11 @@
 void
 octave_sleep (unsigned int seconds)
 {
+#if defined (__WIN32__) && ! defined (_POSIX_VERSION)
+  Sleep (1000 * seconds);
+#else
   sleep (seconds);
+#endif
 }
 
 void


Reply | Threaded
Open this post in threaded view
|

Re: Sleep on Windows systems

Paul Kienzle-6
On Wed, Oct 02, 2002 at 10:19:39PM -0500, John W. Eaton wrote:

> On  2-Oct-2002, Paul Kienzle <[hidden email]> wrote:
>
> | We don't need to be that sophisticated since we are not event
> | driven.  Seems to me their code is wrong, too.  As well as
> | sleeping for the entire time, are they not also "sleeping"
> | for the time required to process the event queue?
>
> Yes, I suppose they assume it is only a small fraction of the total
> time, but that may not always be valid.
>
> Anyway, my point was just that they don't test for Sleep, they just
> assume it is present on Windows systems.  So that seems easier and not
> as confusing as trying to make autoconf test for a mixed-case name.

That was my conclusion as well.  Rather than testing against
        defined (__WIN32__) && ! defined(_POSIX_VERSION)
every time, should we define USE_WIN32API in config.h?

>
> How about the following change?
>

Here is an additional patch to define usleep.  It seems to work correctly
in that usleep(1000000) is about the same as sleep(1) and usleep(500000)
is half that.

Aside: pause(0.5) doesn't pause.  This is an incompatibility.
Aside: Why do we need both sleep and usleep?

Paul Kienzle <[hidden email]>
        * cutils.c: Win32 version of octave_usleep.

Index: cutils.c
===================================================================
RCS file: /cvs/octave/src/cutils.c,v
retrieving revision 1.9
diff -c -p -r1.9 cutils.c
*** cutils.c 2002/10/03 03:23:15 1.9
--- cutils.c 2002/10/03 12:44:32
*************** Software Foundation, 59 Temple Place - S
*** 24,29 ****
--- 24,40 ----
  #include <config.h>
  #endif
 
+ #include <stdarg.h>
+ #include <stdio.h>
+ #include <stdlib.h>
+ #include <string.h>
+
+ #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
+
+ #include <windows.h>
+
+ #else /* ! defined (__WIN32__) || defined (_POSIX_VERSION) */
+
  #ifdef HAVE_UNISTD_H
  #ifdef HAVE_SYS_TYPES_H
  #include <sys/types.h>
*************** Software Foundation, 59 Temple Place - S
*** 39,49 ****
  #include <sys/poll.h>
  #endif
 
- #include <stdarg.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
-
  static void
  do_octave_usleep (unsigned int useconds)
  {
--- 50,55 ----
*************** do_octave_usleep (unsigned int useconds)
*** 71,76 ****
--- 77,84 ----
  #endif
  }
 
+ #endif /* ! defined (__WIN32__) || defined (_POSIX_VERSION) */
+
  void
  octave_sleep (unsigned int seconds)
  {
*************** octave_sleep (unsigned int seconds)
*** 84,89 ****
--- 92,108 ----
  void
  octave_usleep (unsigned int useconds)
  {
+ #if defined (__WIN32__) && ! defined (_POSIX_VERSION)
+   /* On Windows, round to the nearest millisecond, with a
+    * minimum of 1 millisecond if usleep was called with a
+    * a non-zero value. */
+   if (useconds > 500)
+     Sleep ((useconds+500)/1000);
+   else if (useconds > 0)
+     Sleep (1);
+   else
+     Sleep (0);
+ #else /* ! defined (__WIN32__) || defined (_POSIX_VERSION) */
    unsigned int sec = useconds / 1000000;
    unsigned int usec = useconds % 1000000;
 
*************** octave_usleep (unsigned int useconds)
*** 91,96 ****
--- 110,116 ----
      sleep (sec);
 
    do_octave_usleep (usec);
+ #endif /* ! defined (__WIN32__) || defined (_POSIX_VERSION) */
  }
 
  int