Skip to content

Commit 148c036

Browse files
committed
Replace call_once usage in libc++ with atomics
This avoid a dependency on pthread API (which are used to implement call_once under the hood) which makes libc++ much more usable from Wasm workers (there pthreads are not available). The downside here is that when thread race to be first one to use a given facet we would "leak" an ID which could cause the `facets_` vector to become more spare that it otherwise would be. IIUC this `facets_` vector can already be sparse so it not clear to me what impact this would have in practice. See: #26375
1 parent a699999 commit 148c036

13 files changed

Lines changed: 47 additions & 40 deletions

system/lib/libcxx/include/__locale

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# include <__locale_dir/locale_base_api.h>
1818
# include <__memory/addressof.h>
1919
# include <__memory/shared_count.h>
20-
# include <__mutex/once_flag.h>
2120
# include <__type_traits/make_unsigned.h>
2221
# include <__utility/no_destroy.h>
2322
# include <__utility/private_constructor_tag.h>
@@ -146,7 +145,6 @@ private:
146145
};
147146

148147
class _LIBCPP_EXPORTED_FROM_ABI locale::id {
149-
once_flag __flag_;
150148
int32_t __id_;
151149

152150
static int32_t __next_id;

system/lib/libcxx/src/locale.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -589,7 +589,14 @@ void locale::facet::__on_zero_shared() noexcept { delete this; }
589589
constinit int32_t locale::id::__next_id = 0;
590590

591591
long locale::id::__get() {
592-
call_once(__flag_, [&] { __id_ = __libcpp_atomic_add(&__next_id, 1); });
592+
if (__libcpp_atomic_load(&__id_) == 0) {
593+
int32_t proposed_id = __libcpp_atomic_add(&__next_id, 1);
594+
int32_t expected = 0;
595+
// If we race with another thread here the CAS will fail and
596+
// the proposed_id will be leaked, but __id_ will be non-zero
597+
// in either case.
598+
__libcpp_atomic_compare_exchange(&__id_, &expected, proposed_id);
599+
}
593600
return __id_ - 1;
594601
}
595602

test/codesize/hello_libcxx.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,3 @@ int main() {
99
std::cout << "hello, world!" << std::endl;
1010
return 0;
1111
}
12-

test/codesize/test_codesize_cxx_ctors1.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 19214,
33
"a.out.js.gz": 7982,
4-
"a.out.nodebug.wasm": 132865,
5-
"a.out.nodebug.wasm.gz": 49889,
6-
"total": 152079,
7-
"total_gz": 57871,
4+
"a.out.nodebug.wasm": 132617,
5+
"a.out.nodebug.wasm.gz": 49801,
6+
"total": 151831,
7+
"total_gz": 57783,
88
"sent": [
99
"__cxa_throw",
1010
"_abort_js",

test/codesize/test_codesize_cxx_ctors2.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 19191,
33
"a.out.js.gz": 7968,
4-
"a.out.nodebug.wasm": 132285,
5-
"a.out.nodebug.wasm.gz": 49542,
6-
"total": 151476,
7-
"total_gz": 57510,
4+
"a.out.nodebug.wasm": 132036,
5+
"a.out.nodebug.wasm.gz": 49456,
6+
"total": 151227,
7+
"total_gz": 57424,
88
"sent": [
99
"__cxa_throw",
1010
"_abort_js",

test/codesize/test_codesize_cxx_except.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 22875,
33
"a.out.js.gz": 8959,
4-
"a.out.nodebug.wasm": 172743,
5-
"a.out.nodebug.wasm.gz": 57374,
6-
"total": 195618,
7-
"total_gz": 66333,
4+
"a.out.nodebug.wasm": 173885,
5+
"a.out.nodebug.wasm.gz": 57439,
6+
"total": 196760,
7+
"total_gz": 66398,
88
"sent": [
99
"__cxa_begin_catch",
1010
"__cxa_end_catch",

test/codesize/test_codesize_cxx_except_wasm.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 19025,
33
"a.out.js.gz": 7906,
4-
"a.out.nodebug.wasm": 148138,
5-
"a.out.nodebug.wasm.gz": 55261,
6-
"total": 167163,
7-
"total_gz": 63167,
4+
"a.out.nodebug.wasm": 149501,
5+
"a.out.nodebug.wasm.gz": 55425,
6+
"total": 168526,
7+
"total_gz": 63331,
88
"sent": [
99
"_abort_js",
1010
"_tzset_js",

test/codesize/test_codesize_cxx_except_wasm_legacy.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 19099,
33
"a.out.js.gz": 7926,
4-
"a.out.nodebug.wasm": 145944,
5-
"a.out.nodebug.wasm.gz": 54887,
6-
"total": 165043,
7-
"total_gz": 62813,
4+
"a.out.nodebug.wasm": 147317,
5+
"a.out.nodebug.wasm.gz": 55080,
6+
"total": 166416,
7+
"total_gz": 63006,
88
"sent": [
99
"_abort_js",
1010
"_tzset_js",

test/codesize/test_codesize_cxx_lto.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 18562,
33
"a.out.js.gz": 7670,
4-
"a.out.nodebug.wasm": 102126,
5-
"a.out.nodebug.wasm.gz": 39426,
6-
"total": 120688,
7-
"total_gz": 47096,
4+
"a.out.nodebug.wasm": 104147,
5+
"a.out.nodebug.wasm.gz": 39808,
6+
"total": 122709,
7+
"total_gz": 47478,
88
"sent": [
99
"a (emscripten_resize_heap)",
1010
"b (_setitimer_js)",

test/codesize/test_codesize_cxx_mangle.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
{
22
"a.out.js": 22925,
33
"a.out.js.gz": 8978,
4-
"a.out.nodebug.wasm": 239177,
5-
"a.out.nodebug.wasm.gz": 79774,
6-
"total": 262102,
7-
"total_gz": 88752,
4+
"a.out.nodebug.wasm": 240319,
5+
"a.out.nodebug.wasm.gz": 79859,
6+
"total": 263244,
7+
"total_gz": 88837,
88
"sent": [
99
"__cxa_begin_catch",
1010
"__cxa_end_catch",

0 commit comments

Comments
 (0)