Skip to content

Commit 293bd93

Browse files
committed
support windows
1 parent 99ffbfb commit 293bd93

27 files changed

+130
-2196
lines changed

beast.c

+21-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,15 @@
2424

2525
#include <sys/types.h>
2626
#include <sys/stat.h>
27-
#include <unistd.h>
2827
#include <fcntl.h>
2928
#include <signal.h>
3029
#include <time.h>
31-
#include <pwd.h>
30+
31+
#ifdef PHP_WIN32
32+
#else
33+
#include <pwd.h>
34+
#include <unistd.h>
35+
#endif
3236

3337
typedef struct yy_buffer_state *YY_BUFFER_STATE;
3438

@@ -131,7 +135,9 @@ extern struct file_handler pipe_handler;
131135
static struct file_handler *default_file_handler = NULL;
132136
static struct file_handler *file_handlers[] = {
133137
&tmpfile_handler,
138+
#ifndef PHP_WIN32
134139
&pipe_handler,
140+
#endif
135141
NULL
136142
};
137143

@@ -875,6 +881,9 @@ PHP_INI_END()
875881

876882
void segmentfault_deadlock_fix(int sig)
877883
{
884+
#ifdef PHP_WIN32 // windows not support backtrace
885+
beast_write_log(beast_log_error, "Segmentation fault and fix deadlock");
886+
#else
878887
void *array[10] = {0};
879888
size_t size;
880889
char **info = NULL;
@@ -891,7 +900,7 @@ void segmentfault_deadlock_fix(int sig)
891900
}
892901
free(info);
893902
}
894-
903+
#endif
895904
beast_mm_unlock(); /* Maybe lock mm so free here */
896905
beast_cache_unlock(); /* Maybe lock cache so free here */
897906

@@ -1034,6 +1043,7 @@ PHP_MINIT_FUNCTION(beast)
10341043
return FAILURE;
10351044
}
10361045

1046+
#ifndef PHP_WIN32
10371047
if (getuid() == 0 && beast_log_user) { /* Change log file owner user */
10381048
struct passwd *pwd;
10391049

@@ -1050,11 +1060,17 @@ PHP_MINIT_FUNCTION(beast)
10501060
return FAILURE;
10511061
}
10521062
}
1063+
#endif
10531064

10541065
old_compile_file = zend_compile_file;
10551066
zend_compile_file = cgi_compile_file;
1056-
1057-
beast_ncpu = sysconf(_SC_NPROCESSORS_ONLN); /* Get CPU nums */
1067+
#ifdef PHP_WIN32
1068+
SYSTEM_INFO info;
1069+
GetSystemInfo(&info);
1070+
beast_ncpu = info.dwNumberOfProcessors;
1071+
#else
1072+
beast_ncpu = sysconf(_SC_NPROCESSORS_ONLN); /* Get CPU nums */
1073+
#endif
10581074
if (beast_ncpu <= 0) {
10591075
beast_ncpu = 1;
10601076
}

beast_log.c

+8
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
#include <string.h>
33
#include <stdio.h>
44
#include <stdarg.h>
5+
#ifdef PHP_WIN32
6+
#else
57
#include <unistd.h>
8+
#endif
69
#include "main/php_reentrancy.h"
710
#include "beast_log.h"
811

@@ -24,6 +27,9 @@ int beast_log_init(char *log_file)
2427

2528
int beast_log_chown(uid_t uid, gid_t gid)
2629
{
30+
#ifdef PHP_WIN32
31+
return 1;
32+
#else
2733
int fd;
2834

2935
if (!beast_log_fp) {
@@ -33,11 +39,13 @@ int beast_log_chown(uid_t uid, gid_t gid)
3339
fd = fileno(beast_log_fp);
3440

3541
return fchown(fd, uid, gid);
42+
#endif
3643
}
3744

3845

3946
void beast_write_log(beast_log_level level, const char *fmt, ...)
4047
{
48+
4149
struct tm local_tm, *result_tm;
4250
time_t the_time;
4351
char buf[64];

beast_log.h

+4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
#ifndef BEAST_LOG_H
22
#define BEAST_LOG_H
33

4+
#ifdef PHP_WIN32
5+
#include "win95nt.h"
6+
#else
47
#include <unistd.h>
8+
#endif
59

610
typedef enum {
711
beast_log_debug, /* 0 */

beast_mm.c

+44
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
#include <limits.h>
1010
#include <fcntl.h>
1111
#include <sys/types.h>
12+
#ifdef PHP_WIN32
13+
#include <Windows.h>
14+
#else
1215
#include <sys/mman.h>
16+
#endif
1317

1418
#include "spinlock.h"
1519
#include "beast_log.h"
@@ -216,12 +220,28 @@ int beast_mm_init(int block_size)
216220
}
217221

218222
/* init memory manager lock */
223+
#ifdef PHP_WIN32
224+
mm_lock = NULL;
225+
HANDLE hLockMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,
226+
NULL, PAGE_READWRITE, 0, sizeof(int), NULL);
227+
if (hLockMapFile) {
228+
mm_lock = MapViewOfFile(
229+
hLockMapFile,
230+
FILE_MAP_ALL_ACCESS,
231+
0,
232+
0,
233+
sizeof(int)
234+
);
235+
CloseHandle(hLockMapFile);
236+
}
237+
#else
219238
mm_lock = (int *)mmap(NULL,
220239
sizeof(int),
221240
PROT_READ|PROT_WRITE,
222241
MAP_SHARED|MAP_ANON,
223242
-1,
224243
0);
244+
#endif
225245
if (!mm_lock) {
226246
beast_write_log(beast_log_error,
227247
"Unable alloc share memory for memory manager lock");
@@ -236,16 +256,35 @@ int beast_mm_init(int block_size)
236256
beast_mm_block_size = block_size;
237257
}
238258

259+
#ifdef PHP_WIN32
260+
HANDLE hBlockMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,
261+
NULL, PAGE_READWRITE, 0, beast_mm_block_size, NULL);
262+
if (hBlockMapFile) {
263+
shmaddr = beast_mm_block = MapViewOfFile(
264+
hBlockMapFile,
265+
FILE_MAP_ALL_ACCESS,
266+
0,
267+
0,
268+
beast_mm_block_size
269+
);
270+
CloseHandle(hBlockMapFile);
271+
}
272+
#else
239273
shmaddr = beast_mm_block = (void *)mmap(NULL,
240274
beast_mm_block_size,
241275
PROT_READ|PROT_WRITE,
242276
MAP_SHARED|MAP_ANON,
243277
-1,
244278
0);
279+
#endif
245280
if (!beast_mm_block) {
246281
beast_write_log(beast_log_error,
247282
"Unable alloc share memory for beast");
283+
#ifdef PHP_WIN32
284+
UnmapViewOfFile(mm_lock);
285+
#else
248286
munmap((void *)mm_lock, sizeof(int));
287+
#endif
249288
return -1;
250289
}
251290

@@ -388,10 +427,15 @@ int beast_mm_realspace()
388427
void beast_mm_destroy()
389428
{
390429
if (beast_mm_initialized) {
430+
#ifdef PHP_WIN32
431+
UnmapViewOfFile(beast_mm_block);
432+
UnmapViewOfFile(mm_lock);
433+
#else
391434
/* Free cache memory */
392435
munmap((void *)beast_mm_block, beast_mm_block_size);
393436
/* Free memory lock */
394437
munmap((void *)mm_lock, sizeof(int));
438+
#endif
395439
beast_mm_initialized = 0;
396440
}
397441
}

cache.c

+44-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818

1919
#include <stdlib.h>
2020
#include <sys/types.h>
21+
#ifndef PHP_WIN32
2122
#include <sys/mman.h>
23+
#endif
2224

2325
#include "beast_mm.h"
2426
#include "spinlock.h"
@@ -77,12 +79,28 @@ int beast_cache_init(int size)
7779
}
7880

7981
/* init cache lock */
82+
#ifdef PHP_WIN32
83+
cache_lock = NULL;
84+
HANDLE hLockMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,
85+
NULL, PAGE_READWRITE, 0, sizeof(int), NULL);
86+
if (hLockMapFile) {
87+
cache_lock = MapViewOfFile(
88+
hLockMapFile,
89+
FILE_MAP_ALL_ACCESS,
90+
0,
91+
0,
92+
sizeof(int)
93+
);
94+
CloseHandle(hLockMapFile);
95+
}
96+
#else
8097
cache_lock = (int *)mmap(NULL,
8198
sizeof(int),
8299
PROT_READ|PROT_WRITE,
83100
MAP_SHARED|MAP_ANON,
84101
-1,
85102
0);
103+
#endif
86104
if (!cache_lock) {
87105
beast_write_log(beast_log_error,
88106
"Unable alloc share memory for cache lock");
@@ -95,16 +113,36 @@ int beast_cache_init(int size)
95113
/* init cache buckets's memory */
96114
bucket_size = sizeof(cache_item_t *) * BUCKETS_DEFAULT_SIZE;
97115

116+
#ifdef PHP_WIN32
117+
HANDLE hBucketsMapFile = CreateFileMapping(INVALID_HANDLE_VALUE,
118+
NULL, PAGE_READWRITE, 0, bucket_size, NULL);
119+
if (hBucketsMapFile) {
120+
beast_cache_buckets = (cache_item_t **)MapViewOfFile(
121+
hBucketsMapFile,
122+
FILE_MAP_ALL_ACCESS,
123+
0,
124+
0,
125+
bucket_size
126+
);
127+
CloseHandle(hBucketsMapFile);
128+
}
129+
#else
98130
beast_cache_buckets = (cache_item_t **)mmap(NULL,
99131
bucket_size,
100132
PROT_READ|PROT_WRITE,
101133
MAP_SHARED|MAP_ANON,
102134
-1,
103135
0);
136+
#endif
137+
104138
if (!beast_cache_buckets) {
105139
beast_write_log(beast_log_error,
106140
"Unable alloc share memory for cache buckets");
107-
munmap((void *)cache_lock, sizeof(int));
141+
#ifdef PHP_WIN32
142+
UnmapViewOfFile(cache_lock);
143+
#else
144+
munmap((void *)cache_lock, sizeof(int));
145+
#endif
108146
beast_mm_destroy();
109147
return -1;
110148
}
@@ -228,11 +266,15 @@ int beast_cache_destroy()
228266
beast_mm_destroy(); /* destroy memory manager */
229267

230268
/* free cache buckets's mmap memory */
269+
#ifdef PHP_WIN32
270+
UnmapViewOfFile((void *)cache_lock);
271+
UnmapViewOfFile((void *)beast_cache_buckets);
272+
#else
231273
munmap((void *)beast_cache_buckets,
232274
sizeof(cache_item_t *) * BUCKETS_DEFAULT_SIZE);
233275

234276
munmap((void *)cache_lock, sizeof(int));
235-
277+
#endif
236278
beast_cache_initialization = 0;
237279

238280
return 0;

spinlock.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -17,17 +17,23 @@
1717
*/
1818

1919
#include <stdlib.h>
20-
#include <pthread.h>
2120
#include "spinlock.h"
22-
21+
#ifdef PHP_WIN32
22+
#include <Windows.h>
23+
#define __sync_bool_compare_and_swap(lock, o, n) (o == InterlockedExchange(lock, n))
24+
#define __asm__(c) __asm { pause }
25+
#define sched_yield() __asm { rep nop }
26+
#else
27+
#include <pthread.h>
28+
#endif
29+
#include "beast_log.h"
2330

2431
extern int beast_ncpu;
2532

2633

2734
void beast_spinlock(beast_atomic_t *lock, int pid)
2835
{
2936
int i, n;
30-
3137
for ( ;; ) {
3238

3339
if (*lock == 0 &&

win32/.cvsignore

-3
This file was deleted.

win32/CREDITS

-1
This file was deleted.

win32/EXPERIMENTAL

Whitespace-only changes.

0 commit comments

Comments
 (0)