This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Time-HiRes: refactor also nanosleep init
authorJarkko Hietaniemi <jhi@iki.fi>
Fri, 4 Mar 2016 02:20:55 +0000 (21:20 -0500)
committerJarkko Hietaniemi <jhi@iki.fi>
Fri, 4 Mar 2016 22:46:47 +0000 (17:46 -0500)
dist/Time-HiRes/HiRes.xs

index c6ac977..02909d7 100644 (file)
@@ -899,6 +899,16 @@ static int clock_nanosleep(int clock_id, int flags,
 #if (defined(TIME_HIRES_NANOSLEEP)) || \
     (defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME))
 
 #if (defined(TIME_HIRES_NANOSLEEP)) || \
     (defined(TIME_HIRES_CLOCK_NANOSLEEP) && defined(TIMER_ABSTIME))
 
+static void
+nanosleep_init(NV nsec,
+                    struct timespec *sleepfor,
+                    struct timespec *unslept) {
+  sleepfor->tv_sec = (Time_t)(nsec / NV_1E9);
+  sleepfor->tv_nsec = (long)(nsec - ((NV)sleepfor->tv_sec) * NV_1E9);
+  unslept->tv_sec = 0;
+  unslept->tv_nsec = 0;
+}
+
 static NV
 nsec_without_unslept(struct timespec *sleepfor,
                      const struct timespec *unslept) {
 static NV
 nsec_without_unslept(struct timespec *sleepfor,
                      const struct timespec *unslept) {
@@ -998,10 +1008,7 @@ nanosleep(nsec)
        CODE:
        if (nsec < 0.0)
            croak("Time::HiRes::nanosleep(%"NVgf"): negative time not invented yet", nsec);
        CODE:
        if (nsec < 0.0)
            croak("Time::HiRes::nanosleep(%"NVgf"): negative time not invented yet", nsec);
-       sleepfor.tv_sec = (Time_t)(nsec / NV_1E9);
-       sleepfor.tv_nsec = (long)(nsec - ((NV)sleepfor.tv_sec) * NV_1E9);
-       unslept.tv_sec = 0;
-       unslept.tv_nsec = 0;
+        nanosleep_init(nsec, &sleepfor, &unslept);
        if (nanosleep(&sleepfor, &unslept) == 0) {
            RETVAL = nsec;
        } else {
        if (nanosleep(&sleepfor, &unslept) == 0) {
            RETVAL = nsec;
        } else {
@@ -1395,10 +1402,7 @@ clock_nanosleep(clock_id, nsec, flags = 0)
     CODE:
        if (nsec < 0.0)
            croak("Time::HiRes::clock_nanosleep(..., %"NVgf"): negative time not invented yet", nsec);
     CODE:
        if (nsec < 0.0)
            croak("Time::HiRes::clock_nanosleep(..., %"NVgf"): negative time not invented yet", nsec);
-       sleepfor.tv_sec = (Time_t)(nsec / NV_1E9);
-       sleepfor.tv_nsec = (long)(nsec - ((NV)sleepfor.tv_sec) * NV_1E9);
-       unslept.tv_sec = 0;
-       unslept.tv_nsec = 0;
+        nanosleep_init(nsec, &sleepfor, &unslept);
        if (clock_nanosleep(clock_id, flags, &sleepfor, &unslept) == 0) {
            RETVAL = nsec;
        } else {
        if (clock_nanosleep(clock_id, flags, &sleepfor, &unslept) == 0) {
            RETVAL = nsec;
        } else {