Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Issue 636783002: Use C++11 atomics (Closed)

Created:
6 years, 2 months ago by JF
Modified:
6 years, 2 months ago
CC:
chromium-reviews, tfarina, erikwright+watch_chromium.org, dmikurube+memory_chromium.org
Base URL:
https://chromium.googlesource.com/chromium/src.git@master
Project:
chromium
Visibility:
Public.

Description

Use C++11 atomics This CL moves the Chrome code base to C++11 atomics instead of inline assembly for the toolchains that support C++11's <atomic>. The patch is constructed to modify as little code as possible, and will be followed-up with other patches to clean up the rest of the code base. This change should allow compilers to emit better code when dealing with atomics (LLVM has recently seen substantial improvements thanks to morisset's work), be more portable, eventually delete a bunch of code, and in subsequent changes atomicity will be based on memory location instead of individual accesses (making the code less error-prone, and easier to analyze). This patch stems from a fix I recently made to V8 to build under NaCl and PNaCl. This was gating V8's C++11 migration: they needed to move to the LLVM-based NaCl toolchain which didn't work with atomicops' inline assembly. V8 currently uses the __sync_* primitives for the NaCl/PNaCl build, which imply sequential consistency and are therefore suboptimal. Before doing further work I want to fix Chrome's own headers, and trickle these changes to V8. Future changes: * The atomicops headers are copied in 8 locations [0] in the Chrome code base, not all of them are up to date. Those should all be updated in their individual repositories (some are in thiry_party). * The signature of functions should be updated to take an atomic location instead of the pointer argument, and all call sites should be updated (there are current 127 such calls [1] in chromium/src in non-implementation and non-test files). * Atomic operations should be used directly. A few things worth noting: * The atomicops_internals_portable.h file contains the main implementation, and has extra notes on implementation choices. * The CL removes x86 CPU features detection from the portable implementation: - Because the headers are copied in a few places and the x86 CPU feature detection uses a static global that parses cpuid, the atomicops_internals_x86_gcc.cc file was only built once, but its struct interface was declared external and used in protobuf and tcmalloc. Removing the struct from Chrome's own base makes the linker sad because of the two uses. This has two implications: # Don't specialize atomics for SSE2 versus non SSE2. This is a non-issue since Chrome requires a minimum of SSE2. The code is therefore faster (skip a branch). # The code doesn't detect the AMD Opteron Rev E memory barrier bug from AMD errata 147 [2]. This bug affects Opterons 1xx/2xx/8xx that shipped in 2003-2004. In general compilers should work around this bug, not library code. GCC has had this workaround since 2009 [3], but it is still an open bug in LLVM [4]. See comment #27 on the code review: this CPU is used by 0.02% of Chrome users, for whom the race would have to occur and the compiler not have the workaround for the bug to manifest. This also makes the code faster by skipping a branch. * The CL moves the declaration of the x86 CPU features struct to atomicops.h, and matches its fields with the ones duplicated across the code base. This is transitional: it fixes a link failure because tcmalloc relied on the x86_gcc.{h,cc} declaration and implementation, and it fixes an ODR violation where the struct didn't always have 3 members (and the other members were sometimes accessed, uninitialized). * tsan already rewrites all atomics to its own primitives, its header is therefore now unnecessary. The implementation takes care to detect and error out if that turns out not to be true. * MemoryBarrier works around a libstdc++ bug in older versions [5]. The workaround is exactly the non-buggy code for libstdc++ (call out to the compiler's builtin). * The assembly generated by GCC 4.8 and LLVM 3.6 for x86-32/x86-64/ARM when using this portable implementation can be found in [6]. [0]: find . -name "atomicops.h" ./base/atomicops.h ./v8/src/base/atomicops.h ./native_client_sdk/src/libraries/sdk_util/atomicops.h ./third_party/webrtc/base/atomicops.h ./third_party/tcmalloc/chromium/src/base/atomicops.h ./third_party/tcmalloc/vendor/src/base/atomicops.h ./third_party/re2/util/atomicops.h ./third_party/protobuf/src/google/protobuf/stubs/atomicops.h [1]: git grep Barrier_ | grep -v atomicops | grep -v unittest | wc -l [2]: http://support.amd.com/us/Processor_TechDocs/25759.pdf [3]: https://gcc.gnu.org/ml/gcc-patches/2009-10/txt00046.txt [4]: http://llvm.org/bugs/show_bug.cgi?id=5934 [5]: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=51038 [6]: https://code.google.com/p/chromium/issues/detail?id=423074 TEST=ninja -C out/Release base_unittests TEST=trybots BUG=246514 BUG=234215 BUG=420970 BUG=423074 R= dvyukov@google.com, thakis@chromium.org, glider@chromium.org, hboehm@google.com, morisset@google.com, willchan@chromium.org Committed: https://crrev.com/57a4e4a50c673c25e9cdaab53e32f6e53aa0b574 Cr-Commit-Position: refs/heads/master@{#299485}

Patch Set 1 #

Total comments: 7

Patch Set 2 : Make the diff much smaller #

Patch Set 3 : Address vyukov's comments #

Patch Set 4 : Make AtomicOps_Internalx86CPUFeatures consistent #

Total comments: 7

Patch Set 5 : Use C++11 objects instead of free-functions #

Total comments: 1

Patch Set 6 : Remove defined(__cplusplus); only rely on __cplusplus >= 201103L #

Patch Set 7 : Work around libstdc++ bug #

Patch Set 8 : Feature detection for <atomic>, with libc++/libstdc++ whitelist #

Patch Set 9 : Don't feature-detect with __has_include #

Patch Set 10 : Whitelist specific minimum versions of libc++ and libstdc++ #

Patch Set 11 : Work around libstdc++ bug for all versions #

Patch Set 12 : Include a C++ header: __GLIBCXX__/_LIBCPP_VERSION aren't defined otherwise #

Total comments: 4

Patch Set 13 : Two spaces before comments #

Total comments: 2

Patch Set 14 : Use #if defined() instead of #ifdef #

Unified diffs Side-by-side diffs Delta from patch set Stats (+302 lines, -231 lines) Patch
M base/BUILD.gn View 1 1 chunk +1 line, -1 line 0 comments Download
M base/atomicops.h View 1 2 3 4 5 6 7 8 9 10 11 12 2 chunks +61 lines, -20 lines 0 comments Download
A base/atomicops_internals_portable.h View 1 2 3 4 5 6 7 8 9 10 11 12 13 1 chunk +227 lines, -0 lines 0 comments Download
D base/atomicops_internals_tsan.h View 1 chunk +0 lines, -186 lines 0 comments Download
D base/atomicops_internals_x86_gcc.h View 1 1 chunk +0 lines, -14 lines 0 comments Download
D base/atomicops_internals_x86_gcc.cc View 1 2 3 4 chunks +12 lines, -9 lines 0 comments Download
M base/base.gypi View 1 1 chunk +1 line, -1 line 0 comments Download

Messages

Total messages: 55 (7 generated)
JF
I build a shed for atomic bikes. Please add appropriate reviewers. A few things need ...
6 years, 2 months ago (2014-10-07 05:05:55 UTC) #2
JF
+rnk for Windows/MSVC expertise.
6 years, 2 months ago (2014-10-07 05:30:51 UTC) #4
Alexander Potapenko
General comments: I think you'd better keep base/atomicops_internals* for now and just create atomicops_internals_portable.h in ...
6 years, 2 months ago (2014-10-07 06:58:32 UTC) #5
Alexander Potapenko
I was about to suggest converting the existing base/atomicops.h API users to use <atomic> instead ...
6 years, 2 months ago (2014-10-07 07:32:46 UTC) #6
Dmitry Vyukov
please svn copy atomicops_internals_x86_gcc.h atomicops_internals_portable.h this will substantially simplify diff verification
6 years, 2 months ago (2014-10-07 07:48:46 UTC) #7
Dmitry Vyukov
https://codereview.chromium.org/636783002/diff/1/base/atomicops_internals_portable.h File base/atomicops_internals_portable.h (right): https://codereview.chromium.org/636783002/diff/1/base/atomicops_internals_portable.h#newcode19 base/atomicops_internals_portable.h:19: // only valid under certain uses of compare exchange. ...
6 years, 2 months ago (2014-10-07 08:07:04 UTC) #8
Nico
How's this supposed to work? We don't have C++11 library features. This won't compile on ...
6 years, 2 months ago (2014-10-07 08:25:46 UTC) #9
morisset
https://codereview.chromium.org/636783002/diff/1/base/atomicops_internals_portable.h File base/atomicops_internals_portable.h (right): https://codereview.chromium.org/636783002/diff/1/base/atomicops_internals_portable.h#newcode26 base/atomicops_internals_portable.h:26: // needs to increment twice (which the compiler should ...
6 years, 2 months ago (2014-10-07 18:27:44 UTC) #10
hboehm
https://codereview.chromium.org/636783002/diff/1/base/atomicops_internals_portable.h File base/atomicops_internals_portable.h (right): https://codereview.chromium.org/636783002/diff/1/base/atomicops_internals_portable.h#newcode19 base/atomicops_internals_portable.h:19: // only valid under certain uses of compare exchange. ...
6 years, 2 months ago (2014-10-07 18:57:02 UTC) #11
hboehm
On 2014/10/07 08:25:46, Nico (away until Oct 27) wrote: > How's this supposed to work? ...
6 years, 2 months ago (2014-10-07 19:03:45 UTC) #12
Nico
Android's currently using stlport, not libc++. That's what I meant above.
6 years, 2 months ago (2014-10-07 22:04:32 UTC) #13
chromium-reviews
FWIW, the intent is to at least make bionics stdatomic.h usable within Android in that ...
6 years, 2 months ago (2014-10-07 23:49:44 UTC) #14
JF
Thanks for the comments. I answered everyone's comments below, please take a look at the ...
6 years, 2 months ago (2014-10-08 03:28:49 UTC) #15
Nico
> Initially I want to capture toolchains that declare C++11 > support. All of our ...
6 years, 2 months ago (2014-10-08 07:17:57 UTC) #16
Dmitry Vyukov
https://codereview.chromium.org/636783002/diff/60001/base/atomicops.h File base/atomicops.h (left): https://codereview.chromium.org/636783002/diff/60001/base/atomicops.h#oldcode142 base/atomicops.h:142: #include "base/atomicops_internals_tsan.h" __cplusplus is not necessary defined under tsan ...
6 years, 2 months ago (2014-10-08 08:05:44 UTC) #17
Alexander Potapenko
Please don't forget to update the CL description.
6 years, 2 months ago (2014-10-08 08:56:00 UTC) #18
JF
Updated, please take a look. I still have to: * Handle Android and Mac properly ...
6 years, 2 months ago (2014-10-08 17:07:12 UTC) #19
chromium-reviews
On Wed, Oct 8, 2014 at 9:07 PM, <jfb@chromium.org> wrote: > Updated, please take a ...
6 years, 2 months ago (2014-10-08 17:27:56 UTC) #20
Dmitry Vyukov
LGTM FWIW wait for somebody from chromium as well
6 years, 2 months ago (2014-10-08 17:28:13 UTC) #21
Alexander Potapenko
Nico is away, so you'll need a base/ owner. https://codereview.chromium.org/636783002/diff/80001/base/atomicops_internals_portable.h File base/atomicops_internals_portable.h (right): https://codereview.chromium.org/636783002/diff/80001/base/atomicops_internals_portable.h#newcode61 base/atomicops_internals_portable.h:61: ...
6 years, 2 months ago (2014-10-08 17:39:36 UTC) #22
JF
> Then please remove the confusing: > #if defined(__cplusplus) Oh that's not to detect whether ...
6 years, 2 months ago (2014-10-08 17:45:38 UTC) #23
Alexander Potapenko
> The bots tell me I'll either have to change inclusion order for > Android ...
6 years, 2 months ago (2014-10-08 17:46:04 UTC) #24
JF
On 2014/10/08 17:46:04, Alexander Potapenko wrote: > > The bots tell me I'll either have ...
6 years, 2 months ago (2014-10-08 18:56:11 UTC) #25
JF
I fixed the linker's undefined reference complaint, it was caused by declaring std::atomic_thread_fence without defining ...
6 years, 2 months ago (2014-10-09 00:24:33 UTC) #26
JF
I did some homework to try to figure out how many people would be impacted ...
6 years, 2 months ago (2014-10-09 04:20:18 UTC) #27
JF
I'm dropping __has_include, and only whitelisting libc++ and libstdc++: the preprocessor is sad when __has_include ...
6 years, 2 months ago (2014-10-09 04:46:40 UTC) #28
JF
I have all the tests passing (ignoring what seems to be a flake in WidgetTest.GetRestoredBounds). ...
6 years, 2 months ago (2014-10-09 21:12:25 UTC) #29
chromium-reviews
I don't think I get a vote, but I would also ignore the AMD bug. ...
6 years, 2 months ago (2014-10-09 22:34:09 UTC) #30
JF
Adding willchan@ for base/OWNERS review. He's on vacation tomorrow, will take a look Monday.
6 years, 2 months ago (2014-10-10 04:22:55 UTC) #32
Alexander Potapenko
https://codereview.chromium.org/636783002/diff/370001/base/atomicops.h File base/atomicops.h (right): https://codereview.chromium.org/636783002/diff/370001/base/atomicops.h#newcode31 base/atomicops.h:31: #include <cassert> // Small C++ header which defines implementation ...
6 years, 2 months ago (2014-10-10 08:35:00 UTC) #33
JF
https://codereview.chromium.org/636783002/diff/370001/base/atomicops.h File base/atomicops.h (right): https://codereview.chromium.org/636783002/diff/370001/base/atomicops.h#newcode31 base/atomicops.h:31: #include <cassert> // Small C++ header which defines implementation ...
6 years, 2 months ago (2014-10-10 16:06:06 UTC) #34
willchan no longer on Chromium
Am I misunderstanding, or did Nico's point out STLPort on Android never get addressed? Boehm ...
6 years, 2 months ago (2014-10-13 17:42:23 UTC) #36
JF
On 2014/10/13 17:42:23, willchan OOO until 03-22-15 wrote: > Am I misunderstanding, or did Nico's ...
6 years, 2 months ago (2014-10-13 17:51:59 UTC) #37
willchan no longer on Chromium
Oh, I think I see. Since you've implemented as a whitelist, then if the necessary ...
6 years, 2 months ago (2014-10-13 17:53:39 UTC) #38
willchan no longer on Chromium
LGTM Please update the changelist description to indicate early on that it's not switching to ...
6 years, 2 months ago (2014-10-13 17:57:57 UTC) #39
willchan no longer on Chromium
For the record, I didn't review the C++11 atomics implementation in detail. Just did a ...
6 years, 2 months ago (2014-10-13 17:58:56 UTC) #40
JF
I filed a bug that I'll use for this and subsequent patches: https://code.google.com/p/chromium/issues/detail?id=423074 It contains ...
6 years, 2 months ago (2014-10-13 20:38:00 UTC) #41
chromium-reviews
My understanding is that the longer term plan is to move away from STLPort. In ...
6 years, 2 months ago (2014-10-13 22:37:57 UTC) #42
commit-bot: I haz the power
CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/patch-status/636783002/510001
6 years, 2 months ago (2014-10-14 00:23:20 UTC) #44
commit-bot: I haz the power
Try jobs failed on following builders: linux_chromium_rel_swarming on tryserver.chromium.linux (http://build.chromium.org/p/tryserver.chromium.linux/builders/linux_chromium_rel_swarming/builds/25303)
6 years, 2 months ago (2014-10-14 01:48:34 UTC) #46
JF
I re-ran the trybot and it passed. I couldn't find any evidence that this is ...
6 years, 2 months ago (2014-10-14 03:45:31 UTC) #47
Alexander Potapenko
Just double-checking: you're not deleting base/atomicops_internals_x86_gcc.{h,cc}, it's just Rietveld showing them as deleted?
6 years, 2 months ago (2014-10-14 14:16:35 UTC) #48
JF
On 2014/10/14 14:16:35, Alexander Potapenko wrote: > Just double-checking: you're not deleting > base/atomicops_internals_x86_gcc.{h,cc}, it's ...
6 years, 2 months ago (2014-10-14 15:01:29 UTC) #49
JF
On 2014/10/14 03:45:31, JF wrote: > I re-ran the trybot and it passed. I couldn't ...
6 years, 2 months ago (2014-10-14 15:03:14 UTC) #50
JF
On 2014/10/14 03:45:31, JF wrote: > I re-ran the trybot and it passed. I couldn't ...
6 years, 2 months ago (2014-10-14 15:03:17 UTC) #51
commit-bot: I haz the power
CQ is trying da patch. Follow status at https://chromium-cq-status.appspot.com/patch-status/636783002/510001
6 years, 2 months ago (2014-10-14 15:05:06 UTC) #53
commit-bot: I haz the power
Committed patchset #14 (id:510001)
6 years, 2 months ago (2014-10-14 15:06:15 UTC) #54
commit-bot: I haz the power
6 years, 2 months ago (2014-10-14 15:07:07 UTC) #55
Message was sent while issue was closed.
Patchset 14 (id:??) landed as
https://crrev.com/57a4e4a50c673c25e9cdaab53e32f6e53aa0b574
Cr-Commit-Position: refs/heads/master@{#299485}

Powered by Google App Engine
This is Rietveld 408576698