File: Warp.xs

package info (click to toggle)
libtime-warp-perl 0.52-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 96 kB
  • ctags: 1
  • sloc: perl: 11; makefile: 2
file content (138 lines) | stat: -rw-r--r-- 2,769 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#ifdef __cplusplus
extern "C" {
#endif

#define MIN_PERL_DEFINE 1

#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#ifdef __cplusplus
}
#endif

/* Is time() portable everywhere?  Hope so!  XXX */

static NV fallback_NVtime()
{ return time(0); }

static void fallback_U2time(U32 *ret)
{
  ret[0]=time(0);
  ret[1]=0;
}

/*-----------------*/

static int    Installed=0;
static NV (*realNVtime)();
static void   (*realU2time)(U32 *);

static double Lost;    /** time relative to now */
static double Zero;    /** apply Scale from when? */
static double Scale;   /** speed of time (.5 == half speed) */

static void reset_warp()
{
    Lost=0;
    Zero=(*realNVtime)();
    Scale=1;
}

/*-----------------*/

static NV warped_NVtime()
{
    double now = (*realNVtime)() - Lost;
    double delta = now - Zero;
    delta *= Scale;
    return Zero + delta;
}

static void warped_U2time(U32 *ret)
{
    /* performance doesn't matter enough for a native
       non-float implementation */
    double now = warped_NVtime();
    U32 unow = now;
    ret[0] = unow;
    ret[1] = (now - unow) * 1000000;
}

MODULE = Time::Warp            PACKAGE = Time::Warp

PROTOTYPES: ENABLE

void
install_time_api()
	CODE:
{
    SV **svp;
    if (Installed) {
	warn("Time::Warp::install_time_api() called more than once");
	return;
    }
    Installed=1;
    svp = hv_fetch(PL_modglobal, "Time::NVtime", 12, 0);
    if (!svp) {
	warn("Time::Warp: Time::HiRes is not loaded --\n\tat best 1s time accuracy is available");
	hv_store(PL_modglobal, "Time::NVtime", 12,
		 newSViv((IV) fallback_NVtime), 0);
	hv_store(PL_modglobal, "Time::U2time", 12,
		 newSViv((IV) fallback_U2time), 0);
    }
    svp = hv_fetch(PL_modglobal, "Time::NVtime", 12, 0);
    if (!SvIOK(*svp)) croak("Time::NVtime isn't a function pointer");
    realNVtime = (NV(*)()) SvIV(*svp);
    svp = hv_fetch(PL_modglobal, "Time::U2time", 12, 0);
    if (!SvIOK(*svp)) croak("Time::U2time isn't a function pointer");
    realU2time = (void(*)(U32*)) SvIV(*svp);
    hv_store(PL_modglobal, "Time::NVtime", 12,
	     newSViv((IV) warped_NVtime), 0);
    hv_store(PL_modglobal, "Time::U2time", 12,
	     newSViv((IV) warped_U2time), 0);

    reset_warp();
}

void
reset()
	CODE:
	reset_warp();

void
to(when)
     double when
     CODE:
{
    Lost += (warped_NVtime() - when) / Scale;
}

void
scale(...)
     PPCODE:
{
    if (items == 0) {
	XPUSHs(sv_2mortal(newSVnv(Scale)));
    } else {
	Zero = warped_NVtime();
	Lost = 0;
	Scale = SvNV(ST(0));
	if (Scale < 0) {
	    warn("Sorry, Time::Warp cannot go backwards");
	    Scale = 1;
	}
	else if (Scale < .001) {
	    warn("Sorry, Time::Warp cannot stop time");
	    Scale = .001;
	}
    }
}

void
time()
     PPCODE:
{
    XPUSHs(sv_2mortal(newSVnv(warped_NVtime())));
}