Skip to content

Commit cde7423

Browse files
committed
Misc. win32 thread safety fixes.
1 parent c7f22e5 commit cde7423

File tree

8 files changed

+192
-60
lines changed

8 files changed

+192
-60
lines changed

ext/standard/basic_functions.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
#include "ext/standard/dns.h"
3636
#include "ext/standard/php_uuencode.h"
3737

38+
#ifdef PHP_WIN32
39+
#include "win32/php_win32_globals.h"
40+
#endif
41+
3842
typedef struct yy_buffer_state *YY_BUFFER_STATE;
3943

4044
#include "zend.h"
@@ -1005,8 +1009,14 @@ PHP_MINIT_FUNCTION(basic)
10051009
{
10061010
#ifdef ZTS
10071011
ts_allocate_id(&basic_globals_id, sizeof(php_basic_globals), (ts_allocate_ctor) basic_globals_ctor, (ts_allocate_dtor) basic_globals_dtor);
1012+
#ifdef PHP_WIN32
1013+
ts_allocate_id(&php_win32_core_globals_id, sizeof(php_win32_core_globals), (ts_allocate_ctor)php_win32_core_globals_ctor, NULL);
1014+
#endif
10081015
#else
10091016
basic_globals_ctor(&basic_globals TSRMLS_CC);
1017+
#ifdef PHP_WIN32
1018+
php_win32_core_globals_ctor(&php_win32_core_globals TSRMLS_CC);
1019+
#endif
10101020
#endif
10111021

10121022
REGISTER_LONG_CONSTANT("CONNECTION_ABORTED", PHP_CONNECTION_ABORTED, CONST_CS | CONST_PERSISTENT);
@@ -1105,6 +1115,9 @@ PHP_MSHUTDOWN_FUNCTION(basic)
11051115
{
11061116
#ifdef ZTS
11071117
ts_free_id(basic_globals_id);
1118+
#ifdef PHP_WIN32
1119+
ts_free_id(php_win32_core_globals_id);
1120+
#endif
11081121
#else
11091122
basic_globals_dtor(&basic_globals TSRMLS_CC);
11101123
#endif
@@ -1214,6 +1227,7 @@ PHP_RSHUTDOWN_FUNCTION(basic)
12141227
PHP_RSHUTDOWN(assert)(SHUTDOWN_FUNC_ARGS_PASSTHRU);
12151228
PHP_RSHUTDOWN(url_scanner_ex)(SHUTDOWN_FUNC_ARGS_PASSTHRU);
12161229
PHP_RSHUTDOWN(streams)(SHUTDOWN_FUNC_ARGS_PASSTHRU);
1230+
PHP_RSHUTDOWN(win32_core_globals)(SHUTDOWN_FUNC_ARGS_PASSTHRU);
12171231

12181232
if (BG(user_tick_functions)) {
12191233
zend_llist_destroy(BG(user_tick_functions));

ext/standard/syslog.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,9 @@ PHP_RSHUTDOWN_FUNCTION(syslog)
119119
if (BG(syslog_device)) {
120120
efree(BG(syslog_device));
121121
}
122+
#ifdef PHP_WIN32
123+
closelog();
124+
#endif
122125
return SUCCESS;
123126
}
124127

win32/build/config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ ADD_SOURCES("main/streams", "streams.c cast.c memory.c filter.c plain_wrapper.c
194194
userspace.c transports.c xp_socket.c mmap.c");
195195

196196
ADD_SOURCES("win32", "crypt_win32.c flock.c glob.c md5crypt.c pwd.c readdir.c \
197-
registry.c select.c sendmail.c time.c wfile.c winutil.c wsyslog.c");
197+
registry.c select.c sendmail.c time.c wfile.c winutil.c wsyslog.c globals.c");
198198

199199
ADD_SOURCES("regex", "regcomp.c regerror.c regexec.c regfree.c");
200200

win32/globals.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 5 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2004 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.0 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_0.txt. |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Author: Wez Furlong <[email protected]> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
/* $Id$ */
20+
21+
#include "php.h"
22+
#include "php_win32_globals.h"
23+
24+
#ifdef ZTS
25+
PHPAPI int php_win32_core_globals_id;
26+
#else
27+
php_win32_core_globals php_win32_core_globals;
28+
#endif
29+
30+
void php_win32_core_globals_ctor(void *vg TSRMLS_DC)
31+
{
32+
php_win32_core_globals *wg = (php_win32_core_globals*)vg;
33+
memset(wg, 0, sizeof(*wg));
34+
}
35+
36+
PHP_RSHUTDOWN_FUNCTION(win32_core_globals)
37+
{
38+
php_win32_core_globals *wg =
39+
#ifdef ZTS
40+
ts_resource(php_win32_core_globals_id)
41+
#else
42+
&php_win32_core_globals
43+
#endif
44+
;
45+
46+
STR_FREE(wg->login_name);
47+
48+
memset(wg, 0, sizeof(*wg));
49+
}
50+

win32/php_win32_globals.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| PHP Version 5 |
4+
+----------------------------------------------------------------------+
5+
| Copyright (c) 1997-2004 The PHP Group |
6+
+----------------------------------------------------------------------+
7+
| This source file is subject to version 3.0 of the PHP license, |
8+
| that is bundled with this package in the file LICENSE, and is |
9+
| available through the world-wide-web at the following url: |
10+
| http://www.php.net/license/3_0.txt. |
11+
| If you did not receive a copy of the PHP license and are unable to |
12+
| obtain it through the world-wide-web, please send a note to |
13+
| [email protected] so we can mail you a copy immediately. |
14+
+----------------------------------------------------------------------+
15+
| Author: Wez Furlong <[email protected]> |
16+
+----------------------------------------------------------------------+
17+
*/
18+
19+
/* $Id$ */
20+
21+
#ifndef PHP_WIN32_GLOBALS_H
22+
#define PHP_WIN32_GLOBALS_H
23+
24+
/* misc globals for thread-safety under win32 */
25+
26+
#include "pwd.h"
27+
28+
typedef struct _php_win32_core_globals php_win32_core_globals;
29+
30+
#ifdef ZTS
31+
# define PW32G(v) TSRMG(php_win32_core_globals_id, php_win32_core_globals*, v)
32+
extern PHPAPI int php_win32_core_globals_id;
33+
#else
34+
# define PW32G(v) (php_win32_core_globals.v)
35+
extern PHPAPI struct _php_win32_core_globals php_win32_core_globals;
36+
#endif
37+
38+
struct _php_win32_core_globals {
39+
/* syslog */
40+
char *log_header;
41+
HANDLE log_source;
42+
43+
/* getpwuid */
44+
struct passwd pwd;
45+
46+
/* getlogin */
47+
char *login_name;
48+
49+
/* time */
50+
struct timeval starttime;
51+
__int64 lasttime, freq;
52+
};
53+
54+
void php_win32_core_globals_ctor(void *vg TSRMLS_DC);
55+
PHP_RSHUTDOWN_FUNCTION(win32_core_globals);
56+
57+
#endif
58+

win32/pwd.c

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,7 @@
2626
#include <lmapibuf.h>
2727
#include "pwd.h"
2828
#include "grp.h"
29-
30-
#ifndef THREAD_SAFE
31-
static struct passwd pwd;
32-
#endif
29+
#include "php_win32_globals.h"
3330

3431
static char *home_dir = ".";
3532
static char *login_shell = "not command.com!";
@@ -44,21 +41,26 @@ getpwnam(char *name)
4441
char *
4542
getlogin()
4643
{
47-
static char name[256];
44+
char name[256];
4845
DWORD max_len = 256;
46+
TSRMLS_FETCH();
4947

48+
STR_FREE(PW32G(login_name));
5049
GetUserName(name, &max_len);
51-
return name;
50+
name[max_len] = '\0';
51+
PW32G(login_name) = strdup(name);
52+
return PW32G(login_name);
5253
}
5354

5455
struct passwd *
5556
getpwuid(int user_id)
5657
{
57-
pwd.pw_name = getlogin();
58-
pwd.pw_dir = home_dir;
59-
pwd.pw_shell = login_shell;
60-
pwd.pw_uid = 0;
58+
TSRMLS_FETCH();
59+
PW32G(pwd).pw_name = getlogin();
60+
PW32G(pwd).pw_dir = home_dir;
61+
PW32G(pwd).pw_shell = login_shell;
62+
PW32G(pwd).pw_uid = 0;
6163

62-
return &pwd;
64+
return &PW32G(pwd);
6365
}
6466

win32/time.c

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include <winbase.h>
3434
#include <mmsystem.h>
3535
#include <errno.h>
36+
#include "php_win32_globals.h"
3637

3738
int getfilesystemtime(struct timeval *time_Info)
3839
{
@@ -51,72 +52,69 @@ __int64 ff;
5152

5253
PHPAPI int gettimeofday(struct timeval *time_Info, struct timezone *timezone_Info)
5354
{
54-
55-
static struct timeval starttime = {0, 0};
56-
static __int64 lasttime = 0;
57-
static __int64 freq = 0;
5855
__int64 timer;
5956
LARGE_INTEGER li;
6057
BOOL b;
6158
double dt;
59+
TSRMLS_FETCH();
6260

6361
/* Get the time, if they want it */
6462
if (time_Info != NULL) {
65-
if (starttime.tv_sec == 0) {
63+
if (PW32G(starttime).tv_sec == 0) {
6664
b = QueryPerformanceFrequency(&li);
6765
if (!b) {
68-
starttime.tv_sec = -1;
66+
PW32G(starttime).tv_sec = -1;
6967
}
7068
else {
71-
freq = li.QuadPart;
69+
PW32G(freq) = li.QuadPart;
7270
b = QueryPerformanceCounter(&li);
7371
if (!b) {
74-
starttime.tv_sec = -1;
72+
PW32G(starttime).tv_sec = -1;
7573
}
7674
else {
77-
getfilesystemtime(&starttime);
75+
getfilesystemtime(&PW32G(starttime));
7876
timer = li.QuadPart;
79-
dt = (double)timer/freq;
80-
starttime.tv_usec -= (int)((dt-(int)dt)*1000000);
81-
if (starttime.tv_usec < 0) {
82-
starttime.tv_usec += 1000000;
83-
--starttime.tv_sec;
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;
8482
}
85-
starttime.tv_sec -= (int)dt;
83+
PW32G(starttime).tv_sec -= (int)dt;
8684
}
8785
}
8886
}
89-
if (starttime.tv_sec > 0) {
87+
if (PW32G(starttime).tv_sec > 0) {
9088
b = QueryPerformanceCounter(&li);
9189
if (!b) {
92-
starttime.tv_sec = -1;
90+
PW32G(starttime).tv_sec = -1;
9391
}
9492
else {
9593
timer = li.QuadPart;
96-
if (timer < lasttime) {
94+
if (timer < PW32G(lasttime)) {
9795
getfilesystemtime(time_Info);
98-
dt = (double)timer/freq;
99-
starttime = *time_Info;
100-
starttime.tv_usec -= (int)((dt-(int)dt)*1000000);
101-
if (starttime.tv_usec < 0) {
102-
starttime.tv_usec += 1000000;
103-
--starttime.tv_sec;
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;
104102
}
105-
starttime.tv_sec -= (int)dt;
103+
PW32G(starttime).tv_sec -= (int)dt;
106104
}
107105
else {
108-
lasttime = timer;
109-
dt = (double)timer/freq;
110-
time_Info->tv_sec = starttime.tv_sec + (int)dt;
111-
time_Info->tv_usec = starttime.tv_usec + (int)((dt-(int)dt)*1000000);
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);
112110
if (time_Info->tv_usec > 1000000) {
113111
time_Info->tv_usec -= 1000000;
114112
++time_Info->tv_sec;
115113
}
116114
}
117115
}
118116
}
119-
if (starttime.tv_sec < 0) {
117+
if (PW32G(starttime).tv_sec < 0) {
120118
getfilesystemtime(time_Info);
121119
}
122120

@@ -144,6 +142,7 @@ void usleep(unsigned int useconds)
144142
CloseHandle(timer);
145143
}
146144

145+
#if 0 /* looks pretty ropey in here */
147146
#ifdef HAVE_SETITIMER
148147

149148

@@ -225,3 +224,5 @@ PHPAPI int setitimer(int which, const struct itimerval *value, struct itimerval
225224
}
226225

227226
#endif
227+
#endif
228+

0 commit comments

Comments
 (0)