Skip to content

Commit 061fcdb

Browse files
ext/opcache: use C11 atomics for "restart_in" (#10276)
Cheaper than fcntl(F_SETLK). The same is done already on Windows, so if that works, why not use it everywhere? (Of course, only if the compiler supports this C11 feature.) As a bonus, the code in this commit also works on C++ via C++11 std::atomic, just in case somebody adds some C++ code to the opcache extension one day.
1 parent 4c9ae81 commit 061fcdb

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

ext/opcache/ZendAccelerator.c

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,11 @@ static void preload_shutdown(void);
135135
static void preload_activate(void);
136136
static void preload_restart(void);
137137

138-
#ifdef ZEND_WIN32
138+
#ifdef HAVE_STDATOMIC_H
139+
# define INCREMENT(v) (++ZCSG(v))
140+
# define DECREMENT(v) (--ZCSG(v))
141+
# define LOCKVAL(v) atomic_load(&ZCSG(v))
142+
#elif defined(ZEND_WIN32)
139143
# define INCREMENT(v) InterlockedIncrement64(&ZCSG(v))
140144
# define DECREMENT(v) InterlockedDecrement64(&ZCSG(v))
141145
# define LOCKVAL(v) (ZCSG(v))
@@ -273,7 +277,7 @@ static ZEND_INI_MH(accel_include_path_on_modify)
273277

274278
static inline void accel_restart_enter(void)
275279
{
276-
#ifdef ZEND_WIN32
280+
#ifdef INCREMENT
277281
INCREMENT(restart_in);
278282
#else
279283
struct flock restart_in_progress;
@@ -292,7 +296,7 @@ static inline void accel_restart_enter(void)
292296

293297
static inline void accel_restart_leave(void)
294298
{
295-
#ifdef ZEND_WIN32
299+
#ifdef DECREMENT
296300
ZCSG(restart_in_progress) = false;
297301
DECREMENT(restart_in);
298302
#else
@@ -313,7 +317,9 @@ static inline void accel_restart_leave(void)
313317
static inline int accel_restart_is_active(void)
314318
{
315319
if (ZCSG(restart_in_progress)) {
316-
#ifndef ZEND_WIN32
320+
#ifdef LOCKVAL
321+
return LOCKVAL(restart_in) != 0;
322+
#else
317323
struct flock restart_check;
318324

319325
restart_check.l_type = F_WRLCK;
@@ -331,8 +337,6 @@ static inline int accel_restart_is_active(void)
331337
} else {
332338
return 1;
333339
}
334-
#else
335-
return LOCKVAL(restart_in) != 0;
336340
#endif
337341
}
338342
return 0;
@@ -341,7 +345,7 @@ static inline int accel_restart_is_active(void)
341345
/* Creates a read lock for SHM access */
342346
static inline zend_result accel_activate_add(void)
343347
{
344-
#ifdef ZEND_WIN32
348+
#ifdef INCREMENT
345349
SHM_UNPROTECT();
346350
INCREMENT(mem_usage);
347351
SHM_PROTECT();
@@ -364,7 +368,7 @@ static inline zend_result accel_activate_add(void)
364368
/* Releases a lock for SHM access */
365369
static inline void accel_deactivate_sub(void)
366370
{
367-
#ifdef ZEND_WIN32
371+
#ifdef DECREMENT
368372
if (ZCG(counted)) {
369373
SHM_UNPROTECT();
370374
DECREMENT(mem_usage);
@@ -387,7 +391,7 @@ static inline void accel_deactivate_sub(void)
387391

388392
static inline void accel_unlock_all(void)
389393
{
390-
#ifdef ZEND_WIN32
394+
#ifdef DECREMENT
391395
accel_deactivate_sub();
392396
#else
393397
struct flock mem_usage_unlock_all;
@@ -828,7 +832,7 @@ static void accel_use_shm_interned_strings(void)
828832
HANDLE_UNBLOCK_INTERRUPTIONS();
829833
}
830834

831-
#ifndef ZEND_WIN32
835+
#ifndef LOCKVAL
832836
static inline void kill_all_lockers(struct flock *mem_usage_check)
833837
{
834838
int tries;
@@ -895,7 +899,7 @@ static inline void kill_all_lockers(struct flock *mem_usage_check)
895899

896900
static inline int accel_is_inactive(void)
897901
{
898-
#ifdef ZEND_WIN32
902+
#ifdef LOCKVAL
899903
if (LOCKVAL(mem_usage) == 0) {
900904
return SUCCESS;
901905
}

ext/opcache/ZendAccelerator.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,14 @@
5555
#include "zend_accelerator_hash.h"
5656
#include "zend_accelerator_debug.h"
5757

58+
#ifdef HAVE_STDATOMIC_H
59+
# ifdef __cplusplus
60+
# include <atomic>
61+
# else
62+
# include <stdatomic.h>
63+
# endif
64+
#endif // HAVE_STDATOMIC_H
65+
5866
#ifndef PHPAPI
5967
# ifdef ZEND_WIN32
6068
# define PHPAPI __declspec(dllimport)
@@ -261,7 +269,13 @@ typedef struct _zend_accel_shared_globals {
261269
bool restart_pending;
262270
zend_accel_restart_reason restart_reason;
263271
bool cache_status_before_restart;
264-
#ifdef ZEND_WIN32
272+
#ifdef HAVE_STDATOMIC_H
273+
# ifdef __cplusplus
274+
std::atomic_llong mem_usage, restart_in;
275+
# else
276+
atomic_llong mem_usage, restart_in;
277+
# endif
278+
#elif defined(ZEND_WIN32)
265279
LONGLONG mem_usage;
266280
LONGLONG restart_in;
267281
#endif

ext/opcache/config.m4

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ if test "$PHP_OPCACHE" != "no"; then
2323
dnl Always build as shared extension
2424
ext_shared=yes
2525

26+
AC_CHECK_HEADERS([stdatomic.h])
27+
2628
if test "$PHP_HUGE_CODE_PAGES" = "yes"; then
2729
AC_DEFINE(HAVE_HUGE_CODE_PAGES, 1, [Define to enable copying PHP CODE pages into HUGE PAGES (experimental)])
2830
fi

0 commit comments

Comments
 (0)