You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
<mutex> : std::mutex::mutex is not constexpr #2285
We are explicitly forbidden from strengthening constexpr by WG21-N4861 16.5.5.7 [constexpr.functions]/1: "An implementation shall not declare any standard library function signature as constexpr except for those where it is explicitly required."
Perhaps an LWG issue should be submitted to allow shared_mutex's constructor to be constexpr, as static initialization is preferable to dynamic initialization. As we just store nullptr, we would easily be able to implement such a resolution.
Perhaps an LWG issue should be submitted to allow shared_mutex's constructor to be constexpr, as static initialization is preferable to dynamic initialization. As we just store nullptr, we would easily be able to implement such a resolution.
Looks like it would be feasible for POSIX platforms as well (https://godbolt.org/z/nqxs1n9xn) if they implement shared_mutex naively.
Per the discussion in #2286, we believe mutex's default constructor can feasibly be made constexpr absent the need to support Vista. We'll need to figure out precisely how to wiggle the class definition so the in-memory representation of a default-constructed mutex looks exactly like the current result of dynamic-initialization on a Windows-7-or-later box.
I've tried to look into it. So we need to implement Concurrency::details::stl_critical_section_win7 and _Mtx_internal_imp_t which contains it in a header. These are not _Ugly. Should we:
Implement _Ugly version that does not match existing names?
Per the discussion in #2286, we believe mutex's default constructor can feasibly be made constexpr absent the need to support Vista. We'll need to figure out precisely how to wiggle the class definition so the in-memory representation of a default-constructed mutex looks exactly like the current result of dynamic-initialization on a Windows-7-or-later box.
I don't believe this is true, at least for std::mutex's current guarantees, because the vtbl must live in the STL's DLL. Otherwise, mutexes created by a user DLL become radioactive if that user DLL is unloaded. (There's a similar problem in system_error for generic_category and system_category, but error_codes tend not to be super long-lived objects likely to trigger the problem vs. mutexes)
Billy O'Neal (@BillyONeal) suggested a possible workaround for his concern: use __builtin_isconstant_evaluated() and do constant initialization to a new implementation only in constexpr context, while keeping old initialization for runtime.
Still there are problems in getting vtable class into mutex:
mutex subobjects can't contain a vtable directly or via an union member due to is_standard_layout requirement
vtable cannot be copied via __builtin_bit_cast due to requirement of trivially_copyable
vtable cannot be set with placement new, as only allocating new is valid in constexpr context
construct_at does not help either as there should be a pointer to vtable class, and reinterpreting casts won't work in constexpr
fake vtable could have worked. That is a structure with pointers to functions. __thiscall methods can be emulated with __fastcall function. But this all is way too scary
I think this can definitely be fixed for /MT and /MTd - I've successfully tried the way that forward-declares a dummy variable and then let the linker alias it with Concurrency::details::stl_critical_section_win7's vtable. The address is a constant expression and works for constexpr constructor.
But it didn't work for /MD or /MDd to merely do so. Perhaps we need to export the vtable.
Edit: I see. I'll create a fake vtable when _DLL is defined.
(suggested by AlexGuteniev on Discord) Move Concurrency::details::stl_critical_section_win7's vtable to an import lib. I don't know how to do this, should we move _Mtx_init_in_situ to import lib?
Additionally export ??_7stl_critical_section_win7@details@Concurrency@@6B@ (the mangled name of Concurrency::details::stl_critical_section_win7's vtable) for DLL. Nope. This seems ABI breaking.
Use a fake vtable for static initialization, and use _Mtx_init_in_situ for dynamic initialization. Presumably all statically initialized mutexes that are created within a user DLL are destroyed when the DLL is unloaded. I think I've made a successful attempt by "scary" emulation with __fastcall.
Activity
AlexGuteniev commentedon Oct 20, 2021
It is not even documented. I added a PR form
std::mutexdocumentation: https://github.com/MicrosoftDocs/cpp-docs/pull/3467But probably it also should be mentioned here: https://docs.microsoft.com/en-us/cpp/overview/visual-cpp-language-conformance?view=msvc-160
AdamBucior commentedon Oct 20, 2021
Could we make
shared_mutexconstructorconstexprso that people who need it can have an alternative?AlexGuteniev commentedon Oct 20, 2021
See #845:
StephanTLavavej commentedon Oct 21, 2021
Perhaps an LWG issue should be submitted to allow
shared_mutex's constructor to beconstexpr, as static initialization is preferable to dynamic initialization. As we just storenullptr, we would easily be able to implement such a resolution.CaseyCarter commentedon Oct 21, 2021
Looks like it would be feasible for POSIX platforms as well (https://godbolt.org/z/nqxs1n9xn) if they implement
shared_mutexnaively.CaseyCarter commentedon Oct 22, 2021
Per the discussion in #2286, we believe
mutex's default constructor can feasibly be madeconstexprabsent the need to support Vista. We'll need to figure out precisely how to wiggle the class definition so the in-memory representation of a default-constructedmutexlooks exactly like the current result of dynamic-initialization on a Windows-7-or-later box.AlexGuteniev commentedon Oct 30, 2021
I've tried to look into it. So we need to implement
Concurrency::details::stl_critical_section_win7and_Mtx_internal_imp_twhich contains it in a header. These are not _Ugly. Should we:constexpr mutex
BillyONeal commentedon Oct 31, 2021
I don't believe this is true, at least for std::mutex's current guarantees, because the vtbl must live in the STL's DLL. Otherwise, mutexes created by a user DLL become radioactive if that user DLL is unloaded. (There's a similar problem in
system_errorforgeneric_categoryandsystem_category, buterror_codes tend not to be super long-lived objects likely to trigger the problem vs. mutexes)AlexGuteniev commentedon Oct 31, 2021
Billy O'Neal (@BillyONeal) suggested a possible workaround for his concern: use
__builtin_isconstant_evaluated()and do constant initialization to a new implementation only inconstexprcontext, while keeping old initialization for runtime.Still there are problems in getting vtable class into mutex:
is_standard_layoutrequirement__builtin_bit_castdue to requirement oftrivially_copyableconstruct_atdoes not help either as there should be a pointer to vtable class, and reinterpreting casts won't work in constexpr__thiscallmethods can be emulated with__fastcallfunction. But this all is way too scaryFedr commentedon Oct 30, 2022
There is a question on Stackoverflow about this issue.
pdimov commentedon Jan 18, 2023
FWIW, I just got a bug report against Boost.System caused by
std::mutex's default constructor not using static initialization: boostorg/system#102.frederick-vs-ja commentedon Feb 16, 2023
I think this can definitely be fixed for
/MTand/MTd- I've successfully tried the way that forward-declares a dummy variable and then let the linker alias it withConcurrency::details::stl_critical_section_win7's vtable. The address is a constant expression and works forconstexprconstructor.But it didn't work for
/MDor/MDdto merely do so. Perhaps we need to export the vtable.Edit: I see. I'll create a fake vtable when
_DLLis defined.frederick-vs-ja commentedon Feb 17, 2023
Possible approches (IMO) for
/MD&/MDd:Concurrency::details::stl_critical_section_win7's vtable to an import lib. I don't know how to do this, should we move_Mtx_init_in_situto import lib?Additionally exportNope. This seems ABI breaking.??_7stl_critical_section_win7@details@Concurrency@@6B@(the mangled name ofConcurrency::details::stl_critical_section_win7's vtable) for DLL._Mtx_init_in_situfor dynamic initialization. Presumably all statically initialized mutexes that are created within a user DLL are destroyed when the DLL is unloaded. I think I've made a successful attempt by "scary" emulation with__fastcall.