Skip to content

ext/opcache: use C11 atomics for "restart_in" #10276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions ext/opcache/ZendAccelerator.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,11 @@ static void preload_shutdown(void);
static void preload_activate(void);
static void preload_restart(void);

#ifdef ZEND_WIN32
#ifdef HAVE_STDATOMIC_H
# define INCREMENT(v) (++ZCSG(v))
# define DECREMENT(v) (--ZCSG(v))
# define LOCKVAL(v) atomic_load(&ZCSG(v))
#elif defined(ZEND_WIN32)
# define INCREMENT(v) InterlockedIncrement64(&ZCSG(v))
# define DECREMENT(v) InterlockedDecrement64(&ZCSG(v))
# define LOCKVAL(v) (ZCSG(v))
Expand Down Expand Up @@ -273,7 +277,7 @@ static ZEND_INI_MH(accel_include_path_on_modify)

static inline void accel_restart_enter(void)
{
#ifdef ZEND_WIN32
#ifdef INCREMENT
INCREMENT(restart_in);
#else
struct flock restart_in_progress;
Expand All @@ -292,7 +296,7 @@ static inline void accel_restart_enter(void)

static inline void accel_restart_leave(void)
{
#ifdef ZEND_WIN32
#ifdef DECREMENT
ZCSG(restart_in_progress) = false;
DECREMENT(restart_in);
#else
Expand All @@ -313,7 +317,9 @@ static inline void accel_restart_leave(void)
static inline int accel_restart_is_active(void)
{
if (ZCSG(restart_in_progress)) {
#ifndef ZEND_WIN32
#ifdef LOCKVAL
return LOCKVAL(restart_in) != 0;
#else
struct flock restart_check;

restart_check.l_type = F_WRLCK;
Expand All @@ -331,8 +337,6 @@ static inline int accel_restart_is_active(void)
} else {
return 1;
}
#else
return LOCKVAL(restart_in) != 0;
#endif
}
return 0;
Expand All @@ -341,7 +345,7 @@ static inline int accel_restart_is_active(void)
/* Creates a read lock for SHM access */
static inline zend_result accel_activate_add(void)
{
#ifdef ZEND_WIN32
#ifdef INCREMENT
SHM_UNPROTECT();
INCREMENT(mem_usage);
SHM_PROTECT();
Expand All @@ -364,7 +368,7 @@ static inline zend_result accel_activate_add(void)
/* Releases a lock for SHM access */
static inline void accel_deactivate_sub(void)
{
#ifdef ZEND_WIN32
#ifdef DECREMENT
if (ZCG(counted)) {
SHM_UNPROTECT();
DECREMENT(mem_usage);
Expand All @@ -387,7 +391,7 @@ static inline void accel_deactivate_sub(void)

static inline void accel_unlock_all(void)
{
#ifdef ZEND_WIN32
#ifdef DECREMENT
accel_deactivate_sub();
#else
struct flock mem_usage_unlock_all;
Expand Down Expand Up @@ -828,7 +832,7 @@ static void accel_use_shm_interned_strings(void)
HANDLE_UNBLOCK_INTERRUPTIONS();
}

#ifndef ZEND_WIN32
#ifndef LOCKVAL
static inline void kill_all_lockers(struct flock *mem_usage_check)
{
int tries;
Expand Down Expand Up @@ -895,7 +899,7 @@ static inline void kill_all_lockers(struct flock *mem_usage_check)

static inline int accel_is_inactive(void)
{
#ifdef ZEND_WIN32
#ifdef LOCKVAL
if (LOCKVAL(mem_usage) == 0) {
return SUCCESS;
}
Expand Down
16 changes: 15 additions & 1 deletion ext/opcache/ZendAccelerator.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
#include "zend_accelerator_hash.h"
#include "zend_accelerator_debug.h"

#ifdef HAVE_STDATOMIC_H
# ifdef __cplusplus
# include <atomic>
# else
# include <stdatomic.h>
# endif
#endif // HAVE_STDATOMIC_H

#ifndef PHPAPI
# ifdef ZEND_WIN32
# define PHPAPI __declspec(dllimport)
Expand Down Expand Up @@ -261,7 +269,13 @@ typedef struct _zend_accel_shared_globals {
bool restart_pending;
zend_accel_restart_reason restart_reason;
bool cache_status_before_restart;
#ifdef ZEND_WIN32
#ifdef HAVE_STDATOMIC_H
# ifdef __cplusplus
std::atomic_llong mem_usage, restart_in;
# else
atomic_llong mem_usage, restart_in;
# endif
#elif defined(ZEND_WIN32)
LONGLONG mem_usage;
LONGLONG restart_in;
#endif
Expand Down
2 changes: 2 additions & 0 deletions ext/opcache/config.m4
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ if test "$PHP_OPCACHE" != "no"; then
dnl Always build as shared extension
ext_shared=yes

AC_CHECK_HEADERS([stdatomic.h])

if test "$PHP_HUGE_CODE_PAGES" = "yes"; then
AC_DEFINE(HAVE_HUGE_CODE_PAGES, 1, [Define to enable copying PHP CODE pages into HUGE PAGES (experimental)])
fi
Expand Down