Skip to content
Newer
Older
100644 246 lines (215 sloc) 6.88 KB
aceaabc @zsuraski PHP 4.0
zsuraski authored
1 /*****************************************************************************
2 * *
3 * DH_TIME.C *
4 * *
5 * Freely redistributable and modifiable. Use at your own risk. *
6 * *
7 * Copyright 1994 The Downhill Project *
8 *
9 * Modified by Shane Caraveo for use with PHP
10 *
11 *****************************************************************************/
12
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
13 /* $Id$ */
14
15 /**
16 *
17 * 04-Feb-2001
18 * - Added patch by "Vanhanen, Reijo" <Reijo.Vanhanen@helsoft.fi>
19 * Improves accuracy of msec
20 */
aceaabc @zsuraski PHP 4.0
zsuraski authored
21
22 /* Include stuff ************************************************************ */
6f7c0ed @zsuraski Remove tls.[ch]
zsuraski authored
23
49494be @KalleZ MFH:
KalleZ authored
24 #include <config.w32.h>
25
aceaabc @zsuraski PHP 4.0
zsuraski authored
26 #include "time.h"
27 #include "unistd.h"
28 #include "signal.h"
7a7713e @wez Fix build for new usleep implementation.
wez authored
29 #include <windows.h>
aceaabc @zsuraski PHP 4.0
zsuraski authored
30 #include <winbase.h>
31 #include <mmsystem.h>
32 #include <errno.h>
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
33 #include "php_win32_globals.h"
aceaabc @zsuraski PHP 4.0
zsuraski authored
34
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
35 int getfilesystemtime(struct timeval *time_Info)
36 {
6ecc9f1 @laruence Remove unused codes
laruence authored
37 FILETIME ft;
38 __int64 ff;
39 ULARGE_INTEGER convFromft;
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
40
41 GetSystemTimeAsFileTime(&ft); /* 100 ns blocks since 01-Jan-1641 */
6ecc9f1 @laruence Remove unused codes
laruence authored
42 /* resolution seems to be 0.01 sec */
d9810af @slayercat based on microsoft's description,the direct convert from FILETIME str…
slayercat authored
43 /*
6ecc9f1 @laruence Remove unused codes
laruence authored
44 * Do not cast a pointer to a FILETIME structure to either a
45 * ULARGE_INTEGER* or __int64* value because it can cause alignment faults on 64-bit Windows.
46 * via http://technet.microsoft.com/en-us/library/ms724284(v=vs.85).aspx
d9810af @slayercat based on microsoft's description,the direct convert from FILETIME str…
slayercat authored
47 */
48 convFromft.HighPart = ft.dwHighDateTime;
49 convFromft.LowPart = ft.dwLowDateTime;
50 ff = convFromft.QuadPart;
6ecc9f1 @laruence Remove unused codes
laruence authored
51
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
52 time_Info->tv_sec = (int)(ff/(__int64)10000000-(__int64)11644473600);
53 time_Info->tv_usec = (int)(ff % 10000000)/10;
54 return 0;
55 }
56
57
58
2183565 @edinkad Allow dynamically compiled extensions to use gettimeofday() on win32.
edinkad authored
59 PHPAPI int gettimeofday(struct timeval *time_Info, struct timezone *timezone_Info)
aceaabc @zsuraski PHP 4.0
zsuraski authored
60 {
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
61 __int64 timer;
62 LARGE_INTEGER li;
63 BOOL b;
64 double dt;
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
65 TSRMLS_FETCH();
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
66
aceaabc @zsuraski PHP 4.0
zsuraski authored
67 /* Get the time, if they want it */
68 if (time_Info != NULL) {
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
69 if (PW32G(starttime).tv_sec == 0) {
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
70 b = QueryPerformanceFrequency(&li);
71 if (!b) {
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
72 PW32G(starttime).tv_sec = -1;
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
73 }
74 else {
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
75 PW32G(freq) = li.QuadPart;
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
76 b = QueryPerformanceCounter(&li);
77 if (!b) {
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
78 PW32G(starttime).tv_sec = -1;
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
79 }
80 else {
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
81 getfilesystemtime(&PW32G(starttime));
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
82 timer = li.QuadPart;
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
83 dt = (double)timer/PW32G(freq);
84 PW32G(starttime).tv_usec -= (int)((dt-(int)dt)*1000000);
85 if (PW32G(starttime).tv_usec < 0) {
86 PW32G(starttime).tv_usec += 1000000;
87 --PW32G(starttime).tv_sec;
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
88 }
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
89 PW32G(starttime).tv_sec -= (int)dt;
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
90 }
91 }
92 }
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
93 if (PW32G(starttime).tv_sec > 0) {
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
94 b = QueryPerformanceCounter(&li);
95 if (!b) {
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
96 PW32G(starttime).tv_sec = -1;
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
97 }
98 else {
99 timer = li.QuadPart;
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
100 if (timer < PW32G(lasttime)) {
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
101 getfilesystemtime(time_Info);
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
102 dt = (double)timer/PW32G(freq);
103 PW32G(starttime) = *time_Info;
104 PW32G(starttime).tv_usec -= (int)((dt-(int)dt)*1000000);
105 if (PW32G(starttime).tv_usec < 0) {
106 PW32G(starttime).tv_usec += 1000000;
107 --PW32G(starttime).tv_sec;
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
108 }
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
109 PW32G(starttime).tv_sec -= (int)dt;
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
110 }
111 else {
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
112 PW32G(lasttime) = timer;
113 dt = (double)timer/PW32G(freq);
114 time_Info->tv_sec = PW32G(starttime).tv_sec + (int)dt;
115 time_Info->tv_usec = PW32G(starttime).tv_usec + (int)((dt-(int)dt)*1000000);
3332c5b @cataphract - Fixed bug #53297 (gettimeofday implementation in php/win32/time.c c…
cataphract authored
116 if (time_Info->tv_usec >= 1000000) {
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
117 time_Info->tv_usec -= 1000000;
118 ++time_Info->tv_sec;
119 }
120 }
121 }
122 }
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
123 if (PW32G(starttime).tv_sec < 0) {
a97e0e1 Fix for time.c under win32. Patch By: "Vanhanen, Reijo" <Reijo.Vanhan…
James Moore authored
124 getfilesystemtime(time_Info);
125 }
126
aceaabc @zsuraski PHP 4.0
zsuraski authored
127 }
128 /* Get the timezone, if they want it */
129 if (timezone_Info != NULL) {
130 _tzset();
131 timezone_Info->tz_minuteswest = _timezone;
132 timezone_Info->tz_dsttime = _daylight;
133 }
134 /* And return */
135 return 0;
136 }
137
274dad3 @pierrejoye - MFH:
pierrejoye authored
138 PHPAPI int usleep(unsigned int useconds)
aceaabc @zsuraski PHP 4.0
zsuraski authored
139 {
dd60413 @wez implement usleep for win32
wez authored
140 HANDLE timer;
141 LARGE_INTEGER due;
142
848684f @wez make usleep actually work properly
wez authored
143 due.QuadPart = -(10 * (__int64)useconds);
aceaabc @zsuraski PHP 4.0
zsuraski authored
144
848684f @wez make usleep actually work properly
wez authored
145 timer = CreateWaitableTimer(NULL, TRUE, NULL);
dd60413 @wez implement usleep for win32
wez authored
146 SetWaitableTimer(timer, &due, 0, NULL, NULL, 0);
147 WaitForSingleObject(timer, INFINITE);
148 CloseHandle(timer);
274dad3 @pierrejoye - MFH:
pierrejoye authored
149 return 0;
150 }
151
152 PHPAPI int nanosleep( const struct timespec * rqtp, struct timespec * rmtp )
153 {
154 if (rqtp->tv_nsec > 999999999) {
155 /* The time interval specified 1,000,000 or more microseconds. */
156 errno = EINVAL;
157 return -1;
158 }
159 return usleep( rqtp->tv_sec * 1000000 + rqtp->tv_nsec / 1000 );
dd60413 @wez implement usleep for win32
wez authored
160 }
aceaabc @zsuraski PHP 4.0
zsuraski authored
161
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
162 #if 0 /* looks pretty ropey in here */
117a2fb * fixed some #if/#ifdef issues
Stig Bakken authored
163 #ifdef HAVE_SETITIMER
aceaabc @zsuraski PHP 4.0
zsuraski authored
164
165
166 #ifndef THREAD_SAFE
167 unsigned int proftimer, virttimer, realtimer;
168 extern LPMSG phpmsg;
169 #endif
170
171 struct timer_msg {
172 int signal;
173 unsigned int threadid;
174 };
175
176
177 LPTIMECALLBACK setitimer_timeout(UINT uTimerID, UINT info, DWORD dwUser, DWORD dw1, DWORD dw2)
178 {
179 struct timer_msg *msg = (struct timer_msg *) info;
180
181 if (msg) {
182 raise((int) msg->signal);
183 PostThreadMessage(msg->threadid,
184 WM_NOTIFY, msg->signal, 0);
185 free(msg);
186 }
187 return 0;
188 }
189
2183565 @edinkad Allow dynamically compiled extensions to use gettimeofday() on win32.
edinkad authored
190 PHPAPI int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
aceaabc @zsuraski PHP 4.0
zsuraski authored
191 {
192 int timeout = value->it_value.tv_sec * 1000 + value->it_value.tv_usec;
193 int repeat = TIME_ONESHOT;
194
195 /*make sure the message queue is initialized */
0818d96 @zsuraski A lot of cleanups... Removed old thread-safe code and other redundant…
zsuraski authored
196 PeekMessage(phpmsg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
aceaabc @zsuraski PHP 4.0
zsuraski authored
197 if (timeout > 0) {
198 struct timer_msg *msg = malloc(sizeof(struct timer_msg));
199 msg->threadid = GetCurrentThreadId();
200 if (!ovalue) {
201 repeat = TIME_PERIODIC;
202 }
203 switch (which) {
204 case ITIMER_REAL:
205 msg->signal = SIGALRM;
0818d96 @zsuraski A lot of cleanups... Removed old thread-safe code and other redundant…
zsuraski authored
206 realtimer = timeSetEvent(timeout, 100, (LPTIMECALLBACK) setitimer_timeout, (UINT) msg, repeat);
aceaabc @zsuraski PHP 4.0
zsuraski authored
207 break;
208 case ITIMER_VIRT:
209 msg->signal = SIGVTALRM;
0818d96 @zsuraski A lot of cleanups... Removed old thread-safe code and other redundant…
zsuraski authored
210 virttimer = timeSetEvent(timeout, 100, (LPTIMECALLBACK) setitimer_timeout, (UINT) msg, repeat);
aceaabc @zsuraski PHP 4.0
zsuraski authored
211 break;
212 case ITIMER_PROF:
213 msg->signal = SIGPROF;
0818d96 @zsuraski A lot of cleanups... Removed old thread-safe code and other redundant…
zsuraski authored
214 proftimer = timeSetEvent(timeout, 100, (LPTIMECALLBACK) setitimer_timeout, (UINT) msg, repeat);
aceaabc @zsuraski PHP 4.0
zsuraski authored
215 break;
216 default:
217 errno = EINVAL;
218 return -1;
219 break;
220 }
221 } else {
222 switch (which) {
223 case ITIMER_REAL:
0818d96 @zsuraski A lot of cleanups... Removed old thread-safe code and other redundant…
zsuraski authored
224 timeKillEvent(realtimer);
aceaabc @zsuraski PHP 4.0
zsuraski authored
225 break;
226 case ITIMER_VIRT:
0818d96 @zsuraski A lot of cleanups... Removed old thread-safe code and other redundant…
zsuraski authored
227 timeKillEvent(virttimer);
aceaabc @zsuraski PHP 4.0
zsuraski authored
228 break;
229 case ITIMER_PROF:
0818d96 @zsuraski A lot of cleanups... Removed old thread-safe code and other redundant…
zsuraski authored
230 timeKillEvent(proftimer);
aceaabc @zsuraski PHP 4.0
zsuraski authored
231 break;
232 default:
233 errno = EINVAL;
234 return -1;
235 break;
236 }
237 }
238
239
240 return 0;
241 }
242
243 #endif
cde7423 @wez Misc. win32 thread safety fixes.
wez authored
244 #endif
245
Something went wrong with that request. Please try again.