# New Ticket Created by  Mark Glines 
# Please include the string:  [perl #53536]
# in the subject line of all future correspondence about this issue. 
# <URL: http://rt.perl.org/rt3/Ticket/Display.html?id=53536 >


The "sleep" op calls Parrot_cx_schedule_sleep(), which falls back to
Parrot_sleep() on non-threaded architectures.  Here's a patch to
implement a Parrot_usleep() and call that instead, for those
platforms.

POSIX platforms have usleep(), with microsecond precision.  Win32
has Sleep(), with millisecond precision.  Are there any platforms which
*don't* have one of those?  Such a platform can probably either use
select(), or some other platform-specific call.

I am having trouble finding a platform to test this on... everything
available to me seems to have threads, and I can't find a Configure.pl
option to disable them, so I can't test this effectively.

Anyway, it turns out I didn't need to shave this yak, but it might be
useful for someone else.  I'm posting the patch for review, hopefully
someone smarter than me will speak up. :)

Mark
Index: src/scheduler.c
===================================================================
--- src/scheduler.c     (revision 27229)
+++ src/scheduler.c     (working copy)
@@ -655,7 +655,13 @@
     MUTEX_DESTROY(lock);
 #else
     /* A more primitive, platform-specific, non-threaded form of sleep. */
-    Parrot_sleep((UINTVAL) ceil(time));
+    if(time > 1000) {
+        /* prevent integer overflow when converting to microseconds */
+        int seconds = floor(time);
+        Parrot_sleep(seconds);
+        time -= seconds;
+    }
+    Parrot_usleep((UINTVAL) time*1000000);
 #endif
     return next;
 }
Index: config/gen/platform/win32/time.c
===================================================================
--- config/gen/platform/win32/time.c    (revision 27229)
+++ config/gen/platform/win32/time.c    (working copy)
@@ -91,6 +91,22 @@
 
 /*
 
+=item C<void Parrot_usleep(unsigned int microseconds)>
+
+RT#48260: Not yet documented!!!
+
+=cut
+
+*/
+
+void
+Parrot_usleep(unsigned int microseconds)
+{
+    Sleep(microseconds / 1000);
+}
+
+/*
+
 =item C<struct tm * Parrot_gmtime_r(const time_t *t, struct tm *tm)>
 
 RT#48260: Not yet documented!!!
Index: config/gen/platform/ansi/time.c
===================================================================
--- config/gen/platform/ansi/time.c     (revision 27229)
+++ config/gen/platform/ansi/time.c     (working copy)
@@ -79,6 +79,23 @@
 
 /*
 
+=item C<void Parrot_usleep(unsigned int microseconds)>
+
+RT#48260: Not yet documented!!!
+
+=cut
+
+*/
+
+void
+Parrot_usleep(unsigned int microseconds)
+{
+    Parrot_warn(NULL, PARROT_WARNINGS_PLATFORM_FLAG, "Parrot_usleep not 
implemented");
+    return;
+}
+
+/*
+
 =back
 
 =cut
Index: config/gen/platform/platform_interface.h
===================================================================
--- config/gen/platform/platform_interface.h    (revision 27229)
+++ config/gen/platform/platform_interface.h    (working copy)
@@ -72,6 +72,7 @@
 */
 
 void Parrot_sleep(unsigned int seconds);
+void Parrot_usleep(unsigned int microseconds);
 INTVAL Parrot_intval_time(void);
 FLOATVAL Parrot_floatval_time(void);
 struct tm * Parrot_gmtime_r(const time_t *, struct tm *);
Index: config/gen/platform/generic/time.c
===================================================================
--- config/gen/platform/generic/time.c  (revision 27229)
+++ config/gen/platform/generic/time.c  (working copy)
@@ -80,6 +80,23 @@
 
 /*
 
+=item C<void
+Parrot_usleep(unsigned int microseconds)>
+
+RT#48260: Not yet documented!!!
+
+=cut
+
+*/
+
+void
+Parrot_usleep(unsigned int microseconds)
+{
+    usleep(microseconds);
+}
+
+/*
+
 =item C<struct tm *
 Parrot_gmtime_r(const time_t *t, struct tm *tm)>
 
Index: config/gen/platform/solaris/time.c
===================================================================
--- config/gen/platform/solaris/time.c  (revision 27229)
+++ config/gen/platform/solaris/time.c  (working copy)
@@ -80,6 +80,23 @@
 
 /*
 
+=item C<void
+Parrot_usleep(unsigned int microseconds)>
+
+RT#48260: Not yet documented!!!
+
+=cut
+
+*/
+
+void
+Parrot_usleep(unsigned int microseconds)
+{
+    usleep(microseconds);
+}
+
+/*
+
 =item C<struct tm *
 Parrot_gmtime_r(const time_t *t, struct tm *tm)>
 

Reply via email to