1999-04-08 05:05:13 +08:00
|
|
|
/*****************************************************************************
|
|
|
|
* *
|
|
|
|
* DH_TIME.C *
|
|
|
|
* *
|
|
|
|
* Freely redistributable and modifiable. Use at your own risk. *
|
|
|
|
* *
|
|
|
|
* Copyright 1994 The Downhill Project *
|
|
|
|
*
|
|
|
|
* Modified by Shane Caraveo for use with PHP
|
|
|
|
*
|
|
|
|
*****************************************************************************/
|
|
|
|
|
2001-02-04 23:52:32 +08:00
|
|
|
/* $Id$ */
|
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
/* Include stuff ************************************************************ */
|
1999-04-24 04:06:01 +08:00
|
|
|
|
2008-08-15 07:29:25 +08:00
|
|
|
#include <config.w32.h>
|
|
|
|
|
1999-04-08 05:05:13 +08:00
|
|
|
#include "time.h"
|
|
|
|
#include "unistd.h"
|
|
|
|
#include "signal.h"
|
2003-11-30 06:59:33 +08:00
|
|
|
#include <windows.h>
|
1999-04-08 05:05:13 +08:00
|
|
|
#include <winbase.h>
|
|
|
|
#include <mmsystem.h>
|
|
|
|
#include <errno.h>
|
2004-07-29 10:59:44 +08:00
|
|
|
#include "php_win32_globals.h"
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2013-03-16 02:04:40 +08:00
|
|
|
typedef VOID (WINAPI *MyGetSystemTimeAsFileTime)(LPFILETIME lpSystemTimeAsFileTime);
|
|
|
|
|
|
|
|
static MyGetSystemTimeAsFileTime get_time_func(void)
|
2001-02-04 23:52:32 +08:00
|
|
|
{
|
2013-03-16 02:04:40 +08:00
|
|
|
MyGetSystemTimeAsFileTime timefunc = NULL;
|
|
|
|
HMODULE hMod = LoadLibrary("kernel32.dll");
|
|
|
|
|
|
|
|
if (hMod) {
|
|
|
|
/* Max possible resolution <1us, win8/server2012 */
|
|
|
|
timefunc = (MyGetSystemTimeAsFileTime)GetProcAddress(hMod, "GetSystemTimePreciseAsFileTime");
|
|
|
|
|
|
|
|
if(!timefunc) {
|
|
|
|
/* 100ns blocks since 01-Jan-1641 */
|
|
|
|
timefunc = (MyGetSystemTimeAsFileTime)GetProcAddress(hMod, "GetSystemTimeAsFileTime");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return timefunc;
|
2001-02-04 23:52:32 +08:00
|
|
|
}
|
|
|
|
|
2013-03-16 02:04:40 +08:00
|
|
|
int getfilesystemtime(struct timeval *tv)
|
|
|
|
{
|
|
|
|
FILETIME ft;
|
|
|
|
unsigned __int64 ff = 0;
|
|
|
|
MyGetSystemTimeAsFileTime timefunc;
|
|
|
|
|
|
|
|
timefunc = get_time_func();
|
|
|
|
if (timefunc) {
|
|
|
|
timefunc(&ft);
|
|
|
|
} else {
|
|
|
|
GetSystemTimeAsFileTime(&ft);
|
|
|
|
}
|
|
|
|
|
|
|
|
ff |= ft.dwHighDateTime;
|
|
|
|
ff <<= 32;
|
|
|
|
ff |= ft.dwLowDateTime;
|
|
|
|
ff /= 10; /* convert to microseconds */
|
|
|
|
ff -= 11644473600000000Ui64; /* convert to unix epoch */
|
|
|
|
|
|
|
|
tv->tv_sec = (long)(ff / 1000000UL);
|
|
|
|
tv->tv_usec = (long)(ff % 1000000UL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2001-02-04 23:52:32 +08:00
|
|
|
|
2002-06-24 07:22:33 +08:00
|
|
|
PHPAPI int gettimeofday(struct timeval *time_Info, struct timezone *timezone_Info)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
|
|
|
/* Get the time, if they want it */
|
|
|
|
if (time_Info != NULL) {
|
2013-03-16 02:04:40 +08:00
|
|
|
getfilesystemtime(time_Info);
|
1999-04-08 05:05:13 +08:00
|
|
|
}
|
|
|
|
/* Get the timezone, if they want it */
|
|
|
|
if (timezone_Info != NULL) {
|
|
|
|
_tzset();
|
|
|
|
timezone_Info->tz_minuteswest = _timezone;
|
|
|
|
timezone_Info->tz_dsttime = _daylight;
|
|
|
|
}
|
|
|
|
/* And return */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-01-19 10:35:22 +08:00
|
|
|
PHPAPI int usleep(unsigned int useconds)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
2003-11-30 06:48:42 +08:00
|
|
|
HANDLE timer;
|
|
|
|
LARGE_INTEGER due;
|
|
|
|
|
2004-01-13 10:07:04 +08:00
|
|
|
due.QuadPart = -(10 * (__int64)useconds);
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2004-01-13 10:07:04 +08:00
|
|
|
timer = CreateWaitableTimer(NULL, TRUE, NULL);
|
2003-11-30 06:48:42 +08:00
|
|
|
SetWaitableTimer(timer, &due, 0, NULL, NULL, 0);
|
|
|
|
WaitForSingleObject(timer, INFINITE);
|
|
|
|
CloseHandle(timer);
|
2009-01-19 10:35:22 +08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
PHPAPI int nanosleep( const struct timespec * rqtp, struct timespec * rmtp )
|
|
|
|
{
|
|
|
|
if (rqtp->tv_nsec > 999999999) {
|
|
|
|
/* The time interval specified 1,000,000 or more microseconds. */
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return usleep( rqtp->tv_sec * 1000000 + rqtp->tv_nsec / 1000 );
|
2003-11-30 06:48:42 +08:00
|
|
|
}
|
1999-04-08 05:05:13 +08:00
|
|
|
|
2004-07-29 10:59:44 +08:00
|
|
|
#if 0 /* looks pretty ropey in here */
|
* fixed some #if/#ifdef issues
* hand-patched in php3 changes from 3.0.6 to HEAD in these files:
fopen-wrappers.[ch] ext/standard/file.[ch] ext/standard/fsock.[ch]
ext/standard/php3_string.h ext/standard/string.c
* added some new file/socket macros for more readable code:
FP_FGETS(buf,len,sock,fp,issock)
FP_FREAD(buf,len,sock,fp,issock)
FP_FEOF(sock,fp,issock)
FP_FGETC(sock,fp,issock)
1999-06-17 01:06:53 +08:00
|
|
|
#ifdef HAVE_SETITIMER
|
1999-04-08 05:05:13 +08:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef THREAD_SAFE
|
|
|
|
unsigned int proftimer, virttimer, realtimer;
|
|
|
|
extern LPMSG phpmsg;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct timer_msg {
|
|
|
|
int signal;
|
|
|
|
unsigned int threadid;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
LPTIMECALLBACK setitimer_timeout(UINT uTimerID, UINT info, DWORD dwUser, DWORD dw1, DWORD dw2)
|
|
|
|
{
|
|
|
|
struct timer_msg *msg = (struct timer_msg *) info;
|
|
|
|
|
|
|
|
if (msg) {
|
|
|
|
raise((int) msg->signal);
|
|
|
|
PostThreadMessage(msg->threadid,
|
|
|
|
WM_NOTIFY, msg->signal, 0);
|
|
|
|
free(msg);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2002-06-24 07:22:33 +08:00
|
|
|
PHPAPI int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
|
1999-04-08 05:05:13 +08:00
|
|
|
{
|
|
|
|
int timeout = value->it_value.tv_sec * 1000 + value->it_value.tv_usec;
|
|
|
|
int repeat = TIME_ONESHOT;
|
|
|
|
|
|
|
|
/*make sure the message queue is initialized */
|
1999-04-24 08:12:00 +08:00
|
|
|
PeekMessage(phpmsg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
|
1999-04-08 05:05:13 +08:00
|
|
|
if (timeout > 0) {
|
|
|
|
struct timer_msg *msg = malloc(sizeof(struct timer_msg));
|
|
|
|
msg->threadid = GetCurrentThreadId();
|
|
|
|
if (!ovalue) {
|
|
|
|
repeat = TIME_PERIODIC;
|
|
|
|
}
|
|
|
|
switch (which) {
|
|
|
|
case ITIMER_REAL:
|
|
|
|
msg->signal = SIGALRM;
|
1999-04-24 08:12:00 +08:00
|
|
|
realtimer = timeSetEvent(timeout, 100, (LPTIMECALLBACK) setitimer_timeout, (UINT) msg, repeat);
|
1999-04-08 05:05:13 +08:00
|
|
|
break;
|
|
|
|
case ITIMER_VIRT:
|
|
|
|
msg->signal = SIGVTALRM;
|
1999-04-24 08:12:00 +08:00
|
|
|
virttimer = timeSetEvent(timeout, 100, (LPTIMECALLBACK) setitimer_timeout, (UINT) msg, repeat);
|
1999-04-08 05:05:13 +08:00
|
|
|
break;
|
|
|
|
case ITIMER_PROF:
|
|
|
|
msg->signal = SIGPROF;
|
1999-04-24 08:12:00 +08:00
|
|
|
proftimer = timeSetEvent(timeout, 100, (LPTIMECALLBACK) setitimer_timeout, (UINT) msg, repeat);
|
1999-04-08 05:05:13 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (which) {
|
|
|
|
case ITIMER_REAL:
|
1999-04-24 08:12:00 +08:00
|
|
|
timeKillEvent(realtimer);
|
1999-04-08 05:05:13 +08:00
|
|
|
break;
|
|
|
|
case ITIMER_VIRT:
|
1999-04-24 08:12:00 +08:00
|
|
|
timeKillEvent(virttimer);
|
1999-04-08 05:05:13 +08:00
|
|
|
break;
|
|
|
|
case ITIMER_PROF:
|
1999-04-24 08:12:00 +08:00
|
|
|
timeKillEvent(proftimer);
|
1999-04-08 05:05:13 +08:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
errno = EINVAL;
|
|
|
|
return -1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
2004-07-29 10:59:44 +08:00
|
|
|
#endif
|
|
|
|
|