2
/*****************************************************************************
6
* Freely redistributable and modifiable. Use at your own risk. *
8
* Copyright 1994 The Downhill Project *
10
* Modified by Shane Caraveo for use with PHP
12
*****************************************************************************/
14
/* $Id: time.c,v 1.9.2.1 2004/07/30 14:24:59 wez Exp $ */
19
* - Added patch by "Vanhanen, Reijo" <Reijo.Vanhanen@helsoft.fi>
20
* Improves accuracy of msec
23
/* Include stuff ************************************************************ */
25
/* this allows the use of the WaitableTimer functions.
26
* For win98 and later */
27
#define _WIN32_WINNT 0x400
36
#include "php_win32_globals.h"
38
int getfilesystemtime(struct timeval *time_Info)
43
GetSystemTimeAsFileTime(&ft); /* 100 ns blocks since 01-Jan-1641 */
44
/* resolution seems to be 0.01 sec */
45
ff = *(__int64*)(&ft);
46
time_Info->tv_sec = (int)(ff/(__int64)10000000-(__int64)11644473600);
47
time_Info->tv_usec = (int)(ff % 10000000)/10;
53
PHPAPI int gettimeofday(struct timeval *time_Info, struct timezone *timezone_Info)
61
/* Get the time, if they want it */
62
if (time_Info != NULL) {
63
if (PW32G(starttime).tv_sec == 0) {
64
b = QueryPerformanceFrequency(&li);
66
PW32G(starttime).tv_sec = -1;
69
PW32G(freq) = li.QuadPart;
70
b = QueryPerformanceCounter(&li);
72
PW32G(starttime).tv_sec = -1;
75
getfilesystemtime(&PW32G(starttime));
77
dt = (double)timer/PW32G(freq);
78
PW32G(starttime).tv_usec -= (int)((dt-(int)dt)*1000000);
79
if (PW32G(starttime).tv_usec < 0) {
80
PW32G(starttime).tv_usec += 1000000;
81
--PW32G(starttime).tv_sec;
83
PW32G(starttime).tv_sec -= (int)dt;
87
if (PW32G(starttime).tv_sec > 0) {
88
b = QueryPerformanceCounter(&li);
90
PW32G(starttime).tv_sec = -1;
94
if (timer < PW32G(lasttime)) {
95
getfilesystemtime(time_Info);
96
dt = (double)timer/PW32G(freq);
97
PW32G(starttime) = *time_Info;
98
PW32G(starttime).tv_usec -= (int)((dt-(int)dt)*1000000);
99
if (PW32G(starttime).tv_usec < 0) {
100
PW32G(starttime).tv_usec += 1000000;
101
--PW32G(starttime).tv_sec;
103
PW32G(starttime).tv_sec -= (int)dt;
106
PW32G(lasttime) = timer;
107
dt = (double)timer/PW32G(freq);
108
time_Info->tv_sec = PW32G(starttime).tv_sec + (int)dt;
109
time_Info->tv_usec = PW32G(starttime).tv_usec + (int)((dt-(int)dt)*1000000);
110
if (time_Info->tv_usec > 1000000) {
111
time_Info->tv_usec -= 1000000;
117
if (PW32G(starttime).tv_sec < 0) {
118
getfilesystemtime(time_Info);
122
/* Get the timezone, if they want it */
123
if (timezone_Info != NULL) {
125
timezone_Info->tz_minuteswest = _timezone;
126
timezone_Info->tz_dsttime = _daylight;
132
void usleep(unsigned int useconds)
137
due.QuadPart = -(10 * (__int64)useconds);
139
timer = CreateWaitableTimer(NULL, TRUE, NULL);
140
SetWaitableTimer(timer, &due, 0, NULL, NULL, 0);
141
WaitForSingleObject(timer, INFINITE);
145
#if 0 /* looks pretty ropey in here */
146
#ifdef HAVE_SETITIMER
150
unsigned int proftimer, virttimer, realtimer;
156
unsigned int threadid;
160
LPTIMECALLBACK setitimer_timeout(UINT uTimerID, UINT info, DWORD dwUser, DWORD dw1, DWORD dw2)
162
struct timer_msg *msg = (struct timer_msg *) info;
165
raise((int) msg->signal);
166
PostThreadMessage(msg->threadid,
167
WM_NOTIFY, msg->signal, 0);
173
PHPAPI int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
175
int timeout = value->it_value.tv_sec * 1000 + value->it_value.tv_usec;
176
int repeat = TIME_ONESHOT;
178
/*make sure the message queue is initialized */
179
PeekMessage(phpmsg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
181
struct timer_msg *msg = malloc(sizeof(struct timer_msg));
182
msg->threadid = GetCurrentThreadId();
184
repeat = TIME_PERIODIC;
188
msg->signal = SIGALRM;
189
realtimer = timeSetEvent(timeout, 100, (LPTIMECALLBACK) setitimer_timeout, (UINT) msg, repeat);
192
msg->signal = SIGVTALRM;
193
virttimer = timeSetEvent(timeout, 100, (LPTIMECALLBACK) setitimer_timeout, (UINT) msg, repeat);
196
msg->signal = SIGPROF;
197
proftimer = timeSetEvent(timeout, 100, (LPTIMECALLBACK) setitimer_timeout, (UINT) msg, repeat);
207
timeKillEvent(realtimer);
210
timeKillEvent(virttimer);
213
timeKillEvent(proftimer);