[PATCH] libgst: Fix small amount of sleeps on OSX

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

[PATCH] libgst: Fix small amount of sleeps on OSX

Holger Freyther
create_timer is not available on OSX which means the older
setitimer is being used. For small times we could either
cancel the timer or sleep a very long time.

* If nsTime < now we would sleep a very long time.
* If deltaMilli == 0 we could cancel the timer and
never wake up.

If the delta is smaller than what can be expressed in
milliseconds sleep the shortest time possible as it is
too late to cancel everything.

This could be easily reproduced using the following code
which would be unlikely to run to the end.

 (Delay forMilliseconds: 1) wait
 (Delay forMilliseconds: 1) wait
 (Delay forMilliseconds: 1) wait
 (Delay forMilliseconds: 1) wait
 (Delay forMilliseconds: 1) wait
 (Delay forMilliseconds: 1) wait
 (Delay forMilliseconds: 1) wait
 (Delay forMilliseconds: 1) wait
---
 libgst/ChangeLog            |  5 +++++
 libgst/sysdep/posix/timer.c | 18 ++++++++++++++----
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/libgst/ChangeLog b/libgst/ChangeLog
index 9c648c9..6724338 100644
--- a/libgst/ChangeLog
+++ b/libgst/ChangeLog
@@ -1,3 +1,8 @@
+2015-04-17  Holger Hans Peter Freyther  <[hidden email]>
+
+ * sysdep/posix/timer.c: Fix handling small sleeping
+ intervals.
+
 2015-02-03  Holger Hans Peter Freyther  <[hidden email]>
 
  * files.c: Include SmallInt.st before Float.st.
diff --git a/libgst/sysdep/posix/timer.c b/libgst/sysdep/posix/timer.c
index c47ae91..6be4376 100644
--- a/libgst/sysdep/posix/timer.c
+++ b/libgst/sysdep/posix/timer.c
@@ -117,12 +117,22 @@ _gst_sigalrm_at (int64_t nsTime)
   else
 #endif
     {
-      int64_t deltaMilli = (nsTime - _gst_get_ns_time()) / 1000000;
+      uint64_t now = _gst_get_ns_time();
+      int64_t deltaNano = nsTime - now;
       struct itimerval value;
-
       value.it_interval.tv_sec = value.it_interval.tv_usec = 0;
-      value.it_value.tv_sec = deltaMilli / 1000;
-      value.it_value.tv_usec = (deltaMilli % 1000) * 1000;
+
+      if (deltaNano <= 1000000)
+        {
+          value.it_value.tv_sec = 0;
+          value.it_value.tv_usec = 1;
+        }
+      else
+        {
+          int64_t deltaMilli = deltaNano / 1000000;
+          value.it_value.tv_sec = deltaMilli / 1000;
+          value.it_value.tv_usec = (deltaMilli % 1000) * 1000;
+        }
       setitimer (ITIMER_REAL, &value, (struct itimerval *) 0);
     }
 }
--
2.3.5


_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] libgst: Fix small amount of sleeps on OSX

Paolo Bonzini-2


On 17/04/2015 23:38, Holger Hans Peter Freyther wrote:

> +      if (deltaNano <= 1000000)
> +        {
> +          value.it_value.tv_sec = 0;
> +          value.it_value.tv_usec = 1;
> +        }
> +      else
> +        {
> +          int64_t deltaMilli = deltaNano / 1000000;
> +          value.it_value.tv_sec = deltaMilli / 1000;
> +          value.it_value.tv_usec = (deltaMilli % 1000) * 1000;
> +        }
>        setitimer (ITIMER_REAL, &value, (struct itimerval *) 0);

Why not:

          int64_t deltaMicro = deltaNano < 1000 ? 1 : deltaNano / 1000;
          value.it_value.tv_sec = deltaMicro / 1000000;
          value.it_value.tv_usec = deltaMicro % 1000000;

?

Paolo

_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk
Reply | Threaded
Open this post in threaded view
|

Re: [PATCH] libgst: Fix small amount of sleeps on OSX

Holger Freyther

> On 20 Apr 2015, at 04:35, Paolo Bonzini <[hidden email]> wrote:
>
>          int64_t deltaMicro = deltaNano < 1000 ? 1 : deltaNano / 1000;

use <= 1000 so we avoid the “1000 / 1000” multiplication that gcc 4.9.2
doesn’t appear to optimize away.

but yes, nice simplificiation


_______________________________________________
help-smalltalk mailing list
[hidden email]
https://lists.gnu.org/mailman/listinfo/help-smalltalk