Skip to content

Commit c2f9d4f

Browse files
committed
Abandon C++ std::atomic
It doesn't work in an extern "C" section.
1 parent f3b3458 commit c2f9d4f

File tree

1 file changed

+13
-16
lines changed

1 file changed

+13
-16
lines changed

Zend/zend_globals.h

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,19 @@
3737
#include "zend_arena.h"
3838

3939
/* Unlike stdbool.h, not all supported compilers support stdatomic.h in C++
40-
* mode. So, conditionally include <atomic> or <stdatomic.h> depending on
41-
* whether the compiler is in C++ mode or not. This restricts us to functions,
42-
* types, and macros which are present in both, which is a drag.
40+
* mode. There have beeen discussions to fix this for C++23/C23, but for
41+
* existing compilers there isn't much to do. Using C++'s std::atomic_bool
42+
* doesn't work because templates aren't available in extern C code. In this
43+
* case, fall back to no atomics.
44+
* TODO: move this stuff to a configure script because for this to be ABI safe,
45+
* a check that sizeof(atomic_bool) == sizeof(bool) needs to done.
4346
*/
44-
#ifdef __cplusplus
45-
#include <atomic>
46-
using std::atomic_bool;
47-
#elif !defined(__STDC_NO_ATOMICS__)
48-
#include <stdatomic.h>
49-
#else
47+
#if defined(__cplusplus) || __STDC_VERSION__ < 201112L || defined(__STDC_NO_ATOMICS__)
5048
// fall back to non-atomics
51-
typedef bool atomic_bool
49+
typedef bool maybe_atomic_bool;
50+
#else
51+
#include <stdatomic.h>
52+
typedef atomic_bool maybe_atomic_bool;
5253
#endif
5354

5455
/* Define ZTS if you want a thread-safe Zend */
@@ -199,12 +200,8 @@ struct _zend_executor_globals {
199200

200201
HashTable *in_autoload;
201202

202-
/* sizeof atomic_bool is not guaranteed to be the same as sizeof bool,
203-
* but it's somewhat common. The atomic_bools are left by the other
204-
* bools so that the struct packs nicely for such platforms.
205-
*/
206-
atomic_bool vm_interrupt;
207-
atomic_bool timed_out;
203+
maybe_atomic_bool vm_interrupt;
204+
maybe_atomic_bool timed_out;
208205
bool full_tables_cleanup;
209206

210207
/* for extended information support */

0 commit comments

Comments
 (0)