poke-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] pvm: Refactor sleep to push erorrs and to check ranges


From: Arsen Arsenović
Subject: [PATCH] pvm: Refactor sleep to push erorrs and to check ranges
Date: Mon, 23 Jan 2023 14:49:36 +0100

2023-01-23  Arsen Arsenović  <arsen@aarsen.me>

        * libpoke/pvm.jitter (sleep): Push an error code rather than
        raising an exception.  Do a range check on the passed seconds and
        nanoseconds value, to reconcile differences between platforms.
        * libpoke/pkl-rt.pk (sleep): Translate error codes pushed by the
        sleep instruction into exceptions.
---
Hi,

This patch fixes the portability issue Bruno reported by always including a
range check for the sleep call.  It also removes branching from the sleep
instruction.

OK for master?

Thanks in advance.

 ChangeLog          |  8 ++++++++
 libpoke/pkl-rt.pk  | 19 ++++++++++++++-----
 libpoke/pvm.jitter | 29 +++++++++++++++--------------
 3 files changed, 37 insertions(+), 19 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 51739797..9945e1cf 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,11 @@
+2023-01-23  Arsen Arsenović  <arsen@aarsen.me>
+
+       * libpoke/pvm.jitter (sleep): Push an error code rather than
+       raising an exception.  Do a range check on the passed seconds and
+       nanoseconds value, to reconcile differences between platforms.
+       * libpoke/pkl-rt.pk (sleep): Translate error codes pushed by the
+       sleep instruction into exceptions.
+
 2023-01-23  Arsen Arsenović  <arsen@aarsen.me>
 
        * doc/poke.texi (Exceptions): Fix typo in the E_no_return
diff --git a/libpoke/pkl-rt.pk b/libpoke/pkl-rt.pk
index acde58df..7cb81ec7 100644
--- a/libpoke/pkl-rt.pk
+++ b/libpoke/pkl-rt.pk
@@ -181,11 +181,6 @@ immutable fun get_time = int<64>[2]:
   return asm int<64>[2]: ("time");
 }
 
-immutable fun sleep = (int<64> sec, int<64> nsec = 0) void:
-{
-  asm ("sleep; drop; drop" :: sec, nsec);
-}
-
 immutable fun strace = void:
 {
   asm ("strace 0");
@@ -545,6 +540,20 @@ immutable fun _pkl_assert = (uint<64> cond, string msg, 
string filename,
     };
   }
 
+/* Sleep instruction wrapper.  */
+
+immutable fun sleep = (int<64> sec, int<64> nsec = 0) void:
+{
+  var err = 0;
+  asm ("sleep; nip2" : err : sec, nsec);
+  if (err == -1)
+    raise E_inval;
+  if (err == -2)
+    raise E_generic;
+  assert (err == 0,
+          "Unhandled error in sleep()!  Please email <poke-devel@gnu.org>.");
+}
+
 /* The following function implements the fast path of the aoref macro
    in pkl-gen.pks.  Given an array whose elements have all the same
    size, which is known at compile-time, and an offset, return the
diff --git a/libpoke/pvm.jitter b/libpoke/pvm.jitter
index 8f7f33aa..6070b59b 100644
--- a/libpoke/pvm.jitter
+++ b/libpoke/pvm.jitter
@@ -6843,32 +6843,33 @@ end
 # Sleep for a given number of seconds and nanoseconds.
 #
 # If the provided number of nanoseconds are not in the range 0 to
-# 999999999 or the number of provided seconds is negative, raise
-# PVM_E_INVAL.
+# 999999999 or the number of provided seconds is negative, pushes
+# -1.
 #
-# If there is any other error performing the operation then raise
-# PVM_E_GENERIC.
+# If there is any other error performing the operation then push
+# -2.
 #
-# Stack: ( LONG LONG -- LONG LONG )
-# Exceptions: PVM_E_INVAL, PVM_E_GENERIC
+# EINTR is treated as no error currently.
+#
+# Stack: ( LONG LONG -- LONG LONG INT )
 
 instruction sleep ()
-  branching # because of PVM_RAISE_DIRECT
   code
     struct timespec ts;
 
     ts.tv_sec = PVM_VAL_LONG (JITTER_UNDER_TOP_STACK ());
     ts.tv_nsec = PVM_VAL_LONG (JITTER_TOP_STACK ());
 
-    if (pvm_nanosleep (&ts, NULL) == -1)
+    if (ts.tv_sec < 0 || ts.tv_nsec < 0 || ts.tv_nsec > 999999999)
+      JITTER_PUSH_STACK (PVM_MAKE_INT (-1, 32));
+    else if (pvm_nanosleep (&ts, NULL) == -1 && errno != EINTR)
     {
-      if (errno == EINTR)
-        /* This is most likely Ctrl-C.  Do nothing */;
-      else if (errno == EINVAL)
-        PVM_RAISE_DFL (PVM_E_INVAL);
+      if (errno == EINVAL)
+        JITTER_PUSH_STACK (PVM_MAKE_INT (-1, 32));
       else
-        PVM_RAISE_DFL (PVM_E_GENERIC);
-    }
+        JITTER_PUSH_STACK (PVM_MAKE_INT (-2, 32));
+    } else
+      JITTER_PUSH_STACK (PVM_MAKE_INT (0, 32));
   end
 end
 
-- 
2.39.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]