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
267274static 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
286293static 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)
306313static 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 */
335342static 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 */
358365static 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
381388static 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
893900static 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 ;
0 commit comments