Repository navigation
C11 atomics backport - #664
Conversation
In the long term, we'll transition to C99-style inline semantics. In the short-term, this will allow both styles to coexist without breaking one another.
|
Hmm, none of my testing hit the C++14 case, and stdatomic.h can't even be #include'd without error. I'll make C11-atomics the option of last resort. |
a3ea3f0 to
54c0771
Compare
|
This seems to solve my problems compiling jemalloc (dev, 379dd44) on aarch64, CentOS 7.3, GCC 7.0.1. To get it compiling on 379dd44 I had to add: |
jasone
left a comment
There was a problem hiding this comment.
This looks really good overall. There are lots of niggly details that I'm just assuming you got right, especially with regard to memory ordering.
In summary:
s/implementationd/implementations/
The AppVeyor build logs suggest there are missing casts. Once you have that sorted out along with the various style nits, I'll pull the diff and give it one more look in a text editor.
| #elif defined(JEMALLOC_C11_ATOMICS) | ||
| # include "jemalloc/internal/atomic_c11.h" | ||
| #else | ||
| # error "Don't have atomics implemented on this platform." |
There was a problem hiding this comment.
Quotes are unnecessary around #error args. We aren't currently consistent about this, but I try to omit the quotes on new code.
There was a problem hiding this comment.
This broke with one of the compilers I tested on because of the apostrophe.
| * counterparts of the C11 atomic functions for type, as so: | ||
| * JEMALLOC_GENERATE_ATOMICS(int *, pi, 3); | ||
| * and then write things like: | ||
| * int* some_ptr; |
There was a problem hiding this comment.
Style nit (here and one more below): int *some_ptr
| * In order to let us transition atomics usage piecemeal (and reason locally | ||
| * about memory orders), we'll support the previous API for a while. | ||
| */ | ||
| #define JEMALLOC_GENERATE_COMPATABILITY_ATOMICS(type, short_type) \ |
There was a problem hiding this comment.
Something looks weird with regard to the tab-aligned (?) backslash continuation throughout this diff.
There was a problem hiding this comment.
I believe this is github formatting things weirdly. These were all done via vim macro to insert spaces up to virtcol 80, assuming a tab is exactly 8 characters wide.
There was a problem hiding this comment.
Ah, so the style for multi-line macro definitions is to use tabs rather than spaces to put the continuation character at column 72.
There was a problem hiding this comment.
Recording the vimscript incantation here so I don't forget how to do it later:
s@\s*\\$@\=repeat('^I', (80-virtcol('.'))/8).'\'@
| ATOMIC_SEQ_CST); \ | ||
| } \ | ||
| /* \ | ||
| * We use '3' below, since the implementation header might have used '1' and \ |
There was a problem hiding this comment.
Maybe simplify by just not allowing terminating semicolons?
| typedef struct { \ | ||
| char c; \ | ||
| } atomic_allow_terminating_semicolon4_##short_type | ||
|
|
| case atomic_memory_order_seq_cst: | ||
| return __ATOMIC_SEQ_CST; | ||
| } | ||
| /* Can't actually happen. */ |
There was a problem hiding this comment.
... and, broke the build in the process. It's tricky to use a non-hermetic header from a hermetic one. What do you think about delaying until after the header refactoring?
| #define ATOMIC_INTERLOCKED_SUFFIX_1 16 | ||
| #define ATOMIC_INTERLOCKED_SUFFIX_2 | ||
| #define ATOMIC_INTERLOCKED_SUFFIX_3 64 | ||
|
|
| @@ -74,26 +74,6 @@ arena_tdata_get | |||
| arena_tdata_get_hard | |||
| arenas | |||
| arenas_tdata_cleanup | |||
There was a problem hiding this comment.
This diff makes it so that we no longer have any non-inline library-internal functions which need name mangling, right?
There was a problem hiding this comment.
Actually, now that you mention it, no; this diff generated externally visible symbols. Fixed.
| TEST_STRUCT(unsigned, u); | ||
| TEST_BEGIN(test_atomic_u) { | ||
| TEST_BODY(u, unsigned, unsigned, u, "#x"); | ||
| TEST_BODY(unsigned, u); |
There was a problem hiding this comment.
Why isn't this one INTEGER_TEST_BODY()?
There was a problem hiding this comment.
Yep, it should be. Fixed.
| val = atomic_load_##ta(&atom, ATOMIC_RELAXED); \ | ||
| assert_##ta##_eq(val2, val, "Exchange store invalid value"); \ | ||
| \ | ||
| /* Weak CAS. Spurious failures are allowed, so we loop a few times. */\ |
There was a problem hiding this comment.
Hmm, are we going to have intermittent test failures as a result of this?
There was a problem hiding this comment.
Not meaningfully. This only matters for LL/SC architectures in screwy situations (e.g. taking an interrupt in between the load and store components of the CAS). This is more a theoretical possibility than a real one.
|
The warnings about signed vs. unsigned are unfortunate, because we don't necessarily know the signed version of the type; we'd have to pass it in as another macro argument. What do you think about just pragma-ing out the warning on MSVC? |
|
Re: the MSVC warnings, if you can get the pragma approach to work, go for it. |
696a973 to
f31833c
Compare
|
@davidlt, I believe the most recent version of the pull request should work on ARM if you've got a second to test. I tried it out on qemu, but in a different enough system that independent verification would be encouraging. |
|
@davidtgoldblatt I did some tests and results are below. Your PR allows it to compile, while the current tip of dev branch does not compile out of the box. I already reported Huge Page issue: #669 I noticed that GCC 7.0.1 and I doubt any of these are related. I also have ability to test on ./configure (GCC 4.8.5, ld 2.25.1-22.base.el7, CentOS 7.3.1611 [last update 2017-03-03], kernel 4.5.0-22.el7.aarch64)./configure --enable-prof (GCC 4.8.5, ld 2.25.1-22.base.el7, CentOS 7.3.1611 [last update 2017-03-03], kernel 4.5.0-22.el7.aarch64)./configure CC="gcc" CXX="g++" (GCC 7.0.1 [20170225], ld 2.27, CentOS 7.3.1611 [last update 2017-03-03], kernel 4.5.0-22.el7.aarch64)./configure --enable-prof CC="gcc" CXX="g++" (GCC 7.0.1 [20170225], ld 2.27, CentOS 7.3.1611 [last update 2017-03-03], kernel 4.5.0-22.el7.aarch64)./configure CC="clang" CXX="clang++" (LLVM/Clang 4.0.0 RC2, GCC 7.0.1 [20170225], ld 2.27, CentOS 7.3.1611 [last update 2017-03-03], kernel 4.5.0-22.el7.aarch64)./configure --enable-prof CC="clang" CXX="clang++" (LLVM/Clang 4.0.0 RC2, GCC 7.0.1 [20170225], ld 2.27, CentOS 7.3.1611 [last update 2017-03-03], kernel 4.5.0-22.el7.aarch64) |
This introduces a backport of C11 atomics. It has four implementations; ranked in order of preference, they are: - GCC/Clang __atomic builtins - GCC/Clang __sync builtins - MSVC _Interlocked builtins - C11 atomics, from <stdatomic.h> The primary advantages are: - Close adherence to the standard API gives us a defined memory model. - Type safety: atomic objects are now separate types from non-atomic ones, so that it's impossible to mix up atomic and non-atomic updates (which is undefined behavior that compilers are starting to take advantage of). - Efficiency: we can specify ordering for operations, avoiding fences and atomic operations on strongly ordered architectures (example: `atomic_write_u32(ptr, val);` involves a CAS loop, whereas `atomic_store(ptr, val, ATOMIC_RELEASE);` is a plain store. This diff leaves in the current atomics API (implementing them in terms of the backport). This lets us transition uses over piecemeal. Testing: This is by nature hard to test. I've manually tested the first three options on Linux on gcc by futzing with the #defines manually, on freebsd with gcc and clang, on MSVC, and on OS X with clang. All of these were x86 machines though, and we don't have any test infrastructure set up for non-x86 platforms.
2 commits:
The only API change for these atomics vs. C11 atomics is that C11 has two versions of every function:
atomic_store(dest, val);and
atomic_store_explicit(dest, val, memory_order);with the former giving sequential consistency, and the latter allowing more complicated ordering decisions.
I figured we'll always care enough about performance and specificity to prefer explicit ordering, so these only define the latter, and drop the _explicit suffix, so that we always have to specify the memory order. I could be persuaded that we should include both versions of every function, though.
Ideas for better testing strategies are appreciated. I'm thinking about setting up QEMU bots for ARM and POWER.