~ubuntu-branches/ubuntu/breezy/php5/breezy-security

« back to all changes in this revision

Viewing changes to win32/time.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-10-09 03:14:32 UTC
  • Revision ID: james.westby@ubuntu.com-20051009031432-kspik3lobxstafv9
Tags: upstream-5.0.5
ImportĀ upstreamĀ versionĀ 5.0.5

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
 
 
2
/*****************************************************************************
 
3
 *                                                                           *
 
4
 * DH_TIME.C                                                                 *
 
5
 *                                                                           *
 
6
 * Freely redistributable and modifiable.  Use at your own risk.             *
 
7
 *                                                                           *
 
8
 * Copyright 1994 The Downhill Project                                       *
 
9
 * 
 
10
 * Modified by Shane Caraveo for use with PHP
 
11
 *
 
12
 *****************************************************************************/
 
13
 
 
14
/* $Id: time.c,v 1.9.2.1 2004/07/30 14:24:59 wez Exp $ */
 
15
 
 
16
 /**
 
17
  *
 
18
  * 04-Feb-2001
 
19
  *   - Added patch by "Vanhanen, Reijo" <Reijo.Vanhanen@helsoft.fi>
 
20
  *     Improves accuracy of msec
 
21
  */
 
22
 
 
23
/* Include stuff ************************************************************ */
 
24
 
 
25
/* this allows the use of the WaitableTimer functions.
 
26
 * For win98 and later */
 
27
#define _WIN32_WINNT 0x400
 
28
 
 
29
#include "time.h"
 
30
#include "unistd.h"
 
31
#include "signal.h"
 
32
#include <windows.h>
 
33
#include <winbase.h>
 
34
#include <mmsystem.h>
 
35
#include <errno.h>
 
36
#include "php_win32_globals.h"
 
37
 
 
38
int getfilesystemtime(struct timeval *time_Info) 
 
39
{
 
40
FILETIME ft;
 
41
__int64 ff;
 
42
 
 
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;
 
48
    return 0;
 
49
}
 
50
 
 
51
 
 
52
 
 
53
PHPAPI int gettimeofday(struct timeval *time_Info, struct timezone *timezone_Info)
 
54
{
 
55
        __int64 timer;
 
56
        LARGE_INTEGER li;
 
57
        BOOL b;
 
58
        double dt;
 
59
        TSRMLS_FETCH();
 
60
 
 
61
        /* Get the time, if they want it */
 
62
        if (time_Info != NULL) {
 
63
                if (PW32G(starttime).tv_sec == 0) {
 
64
            b = QueryPerformanceFrequency(&li);
 
65
            if (!b) {
 
66
                PW32G(starttime).tv_sec = -1;
 
67
            }
 
68
            else {
 
69
                PW32G(freq) = li.QuadPart;
 
70
                b = QueryPerformanceCounter(&li);
 
71
                if (!b) {
 
72
                    PW32G(starttime).tv_sec = -1;
 
73
                }
 
74
                else {
 
75
                    getfilesystemtime(&PW32G(starttime));
 
76
                    timer = li.QuadPart;
 
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;
 
82
                    }
 
83
                    PW32G(starttime).tv_sec -= (int)dt;
 
84
                }
 
85
            }
 
86
        }
 
87
        if (PW32G(starttime).tv_sec > 0) {
 
88
            b = QueryPerformanceCounter(&li);
 
89
            if (!b) {
 
90
                PW32G(starttime).tv_sec = -1;
 
91
            }
 
92
            else {
 
93
                timer = li.QuadPart;
 
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;
 
102
                    }
 
103
                    PW32G(starttime).tv_sec -= (int)dt;
 
104
                }
 
105
                else {
 
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;
 
112
                        ++time_Info->tv_sec;
 
113
                    }
 
114
                }
 
115
            }
 
116
        }
 
117
        if (PW32G(starttime).tv_sec < 0) {
 
118
            getfilesystemtime(time_Info);
 
119
        }
 
120
 
 
121
        }
 
122
        /* Get the timezone, if they want it */
 
123
        if (timezone_Info != NULL) {
 
124
                _tzset();
 
125
                timezone_Info->tz_minuteswest = _timezone;
 
126
                timezone_Info->tz_dsttime = _daylight;
 
127
        }
 
128
        /* And return */
 
129
        return 0;
 
130
}
 
131
 
 
132
void usleep(unsigned int useconds)
 
133
{
 
134
        HANDLE timer;
 
135
        LARGE_INTEGER due;
 
136
 
 
137
        due.QuadPart = -(10 * (__int64)useconds);
 
138
 
 
139
        timer = CreateWaitableTimer(NULL, TRUE, NULL);
 
140
        SetWaitableTimer(timer, &due, 0, NULL, NULL, 0);
 
141
        WaitForSingleObject(timer, INFINITE);
 
142
        CloseHandle(timer);
 
143
}
 
144
 
 
145
#if 0 /* looks pretty ropey in here */
 
146
#ifdef HAVE_SETITIMER
 
147
 
 
148
 
 
149
#ifndef THREAD_SAFE
 
150
unsigned int proftimer, virttimer, realtimer;
 
151
extern LPMSG phpmsg;
 
152
#endif
 
153
 
 
154
struct timer_msg {
 
155
        int signal;
 
156
        unsigned int threadid;
 
157
};
 
158
 
 
159
 
 
160
LPTIMECALLBACK setitimer_timeout(UINT uTimerID, UINT info, DWORD dwUser, DWORD dw1, DWORD dw2)
 
161
{
 
162
        struct timer_msg *msg = (struct timer_msg *) info;
 
163
 
 
164
        if (msg) {
 
165
                raise((int) msg->signal);
 
166
                PostThreadMessage(msg->threadid,
 
167
                                                  WM_NOTIFY, msg->signal, 0);
 
168
                free(msg);
 
169
        }
 
170
        return 0;
 
171
}
 
172
 
 
173
PHPAPI int setitimer(int which, const struct itimerval *value, struct itimerval *ovalue)
 
174
{
 
175
        int timeout = value->it_value.tv_sec * 1000 + value->it_value.tv_usec;
 
176
        int repeat = TIME_ONESHOT;
 
177
 
 
178
        /*make sure the message queue is initialized */
 
179
        PeekMessage(phpmsg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
 
180
        if (timeout > 0) {
 
181
                struct timer_msg *msg = malloc(sizeof(struct timer_msg));
 
182
                msg->threadid = GetCurrentThreadId();
 
183
                if (!ovalue) {
 
184
                        repeat = TIME_PERIODIC;
 
185
                }
 
186
                switch (which) {
 
187
                        case ITIMER_REAL:
 
188
                                msg->signal = SIGALRM;
 
189
                                realtimer = timeSetEvent(timeout, 100, (LPTIMECALLBACK) setitimer_timeout, (UINT) msg, repeat);
 
190
                                break;
 
191
                        case ITIMER_VIRT:
 
192
                                msg->signal = SIGVTALRM;
 
193
                                virttimer = timeSetEvent(timeout, 100, (LPTIMECALLBACK) setitimer_timeout, (UINT) msg, repeat);
 
194
                                break;
 
195
                        case ITIMER_PROF:
 
196
                                msg->signal = SIGPROF;
 
197
                                proftimer = timeSetEvent(timeout, 100, (LPTIMECALLBACK) setitimer_timeout, (UINT) msg, repeat);
 
198
                                break;
 
199
                        default:
 
200
                                errno = EINVAL;
 
201
                                return -1;
 
202
                                break;
 
203
                }
 
204
        } else {
 
205
                switch (which) {
 
206
                        case ITIMER_REAL:
 
207
                                timeKillEvent(realtimer);
 
208
                                break;
 
209
                        case ITIMER_VIRT:
 
210
                                timeKillEvent(virttimer);
 
211
                                break;
 
212
                        case ITIMER_PROF:
 
213
                                timeKillEvent(proftimer);
 
214
                                break;
 
215
                        default:
 
216
                                errno = EINVAL;
 
217
                                return -1;
 
218
                                break;
 
219
                }
 
220
        }
 
221
 
 
222
 
 
223
        return 0;
 
224
}
 
225
 
 
226
#endif
 
227
#endif
 
228