Skip to content

Commit 939072d

Browse files
committed
fix: Restore opcache locking primitives for wasix build
1 parent 808a211 commit 939072d

3 files changed

Lines changed: 67 additions & 15 deletions

File tree

ext/opcache/ZendAccelerator.c

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#include "zend_accelerator_util_funcs.h"
4747
#include "zend_accelerator_hash.h"
4848
#include "zend_file_cache.h"
49+
#include "zend_atomic.h"
4950
#include "ext/pcre/php_pcre.h"
5051
#include "ext/standard/md5.h"
5152
#include "ext/hash/php_hash.h"
@@ -143,6 +144,12 @@ static void preload_restart(void);
143144
# define INCREMENT(v) InterlockedIncrement64(&ZCSG(v))
144145
# define DECREMENT(v) InterlockedDecrement64(&ZCSG(v))
145146
# define LOCKVAL(v) (ZCSG(v))
147+
#elif defined(__wasi__)
148+
static uint32_t accel_wasi_mem_usage = 0;
149+
static uint32_t accel_wasi_restart_in = 0;
150+
# define INCREMENT(v) __atomic_add_fetch(&accel_wasi_##v, 1, __ATOMIC_SEQ_CST)
151+
# define DECREMENT(v) __atomic_sub_fetch(&accel_wasi_##v, 1, __ATOMIC_SEQ_CST)
152+
# define LOCKVAL(v) __atomic_load_n(&accel_wasi_##v, __ATOMIC_SEQ_CST)
146153
#endif
147154

148155
#define ZCG_KEY_LEN (MAXPATHLEN * 8)
@@ -266,7 +273,7 @@ static ZEND_INI_MH(accel_include_path_on_modify)
266273

267274
static inline void accel_restart_enter(void)
268275
{
269-
#ifdef ZEND_WIN32
276+
#if defined(ZEND_WIN32) || defined(__wasi__)
270277
INCREMENT(restart_in);
271278
#elif !defined(__wasi__)
272279
struct flock restart_in_progress;
@@ -285,7 +292,7 @@ static inline void accel_restart_enter(void)
285292

286293
static inline void accel_restart_leave(void)
287294
{
288-
#ifdef ZEND_WIN32
295+
#if defined(ZEND_WIN32) || defined(__wasi__)
289296
ZCSG(restart_in_progress) = false;
290297
DECREMENT(restart_in);
291298
#elif !defined(__wasi__)
@@ -306,7 +313,7 @@ static inline void accel_restart_leave(void)
306313
static inline int accel_restart_is_active(void)
307314
{
308315
if (ZCSG(restart_in_progress)) {
309-
#ifdef ZEND_WIN32
316+
#if defined(ZEND_WIN32) || defined(__wasi__)
310317
return LOCKVAL(restart_in) != 0;
311318
#elif !defined(__wasi__)
312319
struct flock restart_check;
@@ -334,7 +341,7 @@ static inline int accel_restart_is_active(void)
334341
/* Creates a read lock for SHM access */
335342
static inline zend_result accel_activate_add(void)
336343
{
337-
#ifdef ZEND_WIN32
344+
#if defined(ZEND_WIN32) || defined(__wasi__)
338345
SHM_UNPROTECT();
339346
INCREMENT(mem_usage);
340347
SHM_PROTECT();
@@ -357,7 +364,7 @@ static inline zend_result accel_activate_add(void)
357364
/* Releases a lock for SHM access */
358365
static inline void accel_deactivate_sub(void)
359366
{
360-
#ifdef ZEND_WIN32
367+
#if defined(ZEND_WIN32) || defined(__wasi__)
361368
if (ZCG(counted)) {
362369
SHM_UNPROTECT();
363370
DECREMENT(mem_usage);
@@ -380,7 +387,7 @@ static inline void accel_deactivate_sub(void)
380387

381388
static inline void accel_unlock_all(void)
382389
{
383-
#ifdef ZEND_WIN32
390+
#if defined(ZEND_WIN32) || defined(__wasi__)
384391
accel_deactivate_sub();
385392
#elif !defined(__wasi__)
386393
if (lock_file == -1) {
@@ -892,7 +899,7 @@ static inline void kill_all_lockers(struct flock *mem_usage_check)
892899

893900
static inline bool accel_is_inactive(void)
894901
{
895-
#ifdef ZEND_WIN32
902+
#if defined(ZEND_WIN32) || defined(__wasi__)
896903
/* on Windows, we don't need kill_all_lockers() because SAPIs
897904
that work on Windows don't manage child processes (and we
898905
can't do anything about hanging threads anyway); therefore
@@ -2922,6 +2929,10 @@ static zend_result zend_accel_init_shm(void)
29222929
ZCSG(start_time) = zend_accel_get_time();
29232930
ZCSG(last_restart_time) = 0;
29242931
ZCSG(restart_in_progress) = false;
2932+
#ifdef __wasi__
2933+
__atomic_store_n(&accel_wasi_mem_usage, 0, __ATOMIC_SEQ_CST);
2934+
__atomic_store_n(&accel_wasi_restart_in, 0, __ATOMIC_SEQ_CST);
2935+
#endif
29252936

29262937
for (i = 0; i < -HT_MIN_MASK; i++) {
29272938
ZCSG(uninitialized_bucket)[i] = HT_INVALID_IDX;

ext/opcache/zend_file_cache.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,15 @@ static int zend_file_cache_flock(int fd, int op)
9696
#elif defined(HAVE_FLOCK)
9797
# define zend_file_cache_flock flock
9898
#else
99-
# define LOCK_SH 0
100-
# define LOCK_EX 1
101-
# define LOCK_UN 2
99+
# ifndef LOCK_SH
100+
# define LOCK_SH 0
101+
# endif
102+
# ifndef LOCK_EX
103+
# define LOCK_EX 1
104+
# endif
105+
# ifndef LOCK_UN
106+
# define LOCK_UN 2
107+
# endif
102108
static int zend_file_cache_flock(int fd, int type)
103109
{
104110
return 0;

ext/opcache/zend_shared_alloc.c

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include <errno.h>
3030
#include "ZendAccelerator.h"
3131
#include "zend_shared_alloc.h"
32+
#include "zend_atomic.h"
3233
#ifdef HAVE_UNISTD_H
3334
# include <unistd.h>
3435
#endif
@@ -63,6 +64,14 @@ int lock_file = -1;
6364
static char lockfile_name[MAXPATHLEN];
6465
#endif
6566

67+
#ifdef __wasi__
68+
# ifdef ZTS
69+
static MUTEX_T zend_shared_alloc_wasi_mutex;
70+
# else
71+
static zend_atomic_bool zend_shared_alloc_wasi_lock;
72+
# endif
73+
#endif
74+
6675
static const zend_shared_memory_handler_entry handler_table[] = {
6776
#ifdef USE_MMAP
6877
{ "mmap", &zend_alloc_mmap_handlers },
@@ -181,12 +190,18 @@ int zend_shared_alloc_startup(size_t requested_size, size_t reserved_size)
181190
smm_shared_globals = &tmp_shared_globals;
182191
ZSMMG(shared_free) = requested_size - reserved_size; /* goes to tmp_shared_globals.shared_free */
183192

184-
#ifndef __wasi__
185193
#ifndef ZEND_WIN32
194+
#ifdef __wasi__
195+
#ifdef ZTS
196+
zend_shared_alloc_wasi_mutex = tsrm_mutex_alloc();
197+
#else
198+
ZEND_ATOMIC_BOOL_INIT(&zend_shared_alloc_wasi_lock, false);
199+
#endif
200+
#else
186201
zend_shared_alloc_create_lock(ZCG(accel_directives).lockfile_path);
202+
#endif
187203
#else
188204
zend_shared_alloc_create_lock();
189-
#endif
190205
#endif
191206

192207
if (ZCG(accel_directives).memory_model && ZCG(accel_directives).memory_model[0]) {
@@ -325,10 +340,17 @@ void zend_shared_alloc_shutdown(void)
325340
ZSMMG(shared_segments) = NULL;
326341
g_shared_alloc_handler = NULL;
327342
#ifndef ZEND_WIN32
328-
close(lock_file);
343+
if (lock_file >= 0) {
344+
close(lock_file);
345+
lock_file = -1;
346+
}
329347

330348
# ifdef ZTS
349+
# ifdef __wasi__
350+
tsrm_mutex_free(zend_shared_alloc_wasi_mutex);
351+
# else
331352
tsrm_mutex_free(zts_lock);
353+
# endif
332354
# endif
333355
#endif
334356
}
@@ -481,7 +503,14 @@ void zend_shared_alloc_lock(void)
481503

482504
#ifdef ZEND_WIN32
483505
zend_shared_alloc_lock_win32();
484-
#elif !defined(__wasi__)
506+
#elif defined(__wasi__)
507+
#ifdef ZTS
508+
tsrm_mutex_lock(zend_shared_alloc_wasi_mutex);
509+
#else
510+
while (zend_atomic_bool_exchange_ex(&zend_shared_alloc_wasi_lock, true)) {
511+
}
512+
#endif
513+
#else
485514
struct flock mem_write_lock;
486515

487516
mem_write_lock.l_type = F_WRLCK;
@@ -531,7 +560,13 @@ void zend_shared_alloc_unlock(void)
531560

532561
#ifdef ZEND_WIN32
533562
zend_shared_alloc_unlock_win32();
534-
#elif !defined(__wasi__)
563+
#elif defined(__wasi__)
564+
#ifdef ZTS
565+
tsrm_mutex_unlock(zend_shared_alloc_wasi_mutex);
566+
#else
567+
zend_atomic_bool_store_ex(&zend_shared_alloc_wasi_lock, false);
568+
#endif
569+
#else
535570
if (fcntl(lock_file, F_SETLK, &mem_write_unlock) == -1) {
536571
zend_accel_error_noreturn(ACCEL_LOG_ERROR, "Cannot remove lock - %s (%d)", strerror(errno), errno);
537572
}

0 commit comments

Comments
 (0)