Date: Tue, 19 Jul 2022 01:30:12 +0300
On 7/19/22 00:11, Marcin Jaczewski wrote:
> pon., 18 lip 2022 o 22:27 Andrey Semashev via Std-Discussion
>>
>> Obviously, wait can be implemented as there are real world
>> implementations. The standard isn't written to demand something that is
>> impossible to implement.
>
> Yes, it is possible to implement but not in pure C++ as it could use
> compiler intrinsics
> or assembler. It could use code that have total ordering garatees or
> use memory feces.
>
> I grab random code with "wait" from:
> https://github.com/llvm/llvm-project/blob/main/libcxx/include/atomic
> ```
> template <class _Atp, class _Fn>
> struct __libcpp_atomic_wait_backoff_impl {
> _Atp* __a;
> _Fn __test_fn;
> _LIBCPP_AVAILABILITY_SYNC
> _LIBCPP_INLINE_VISIBILITY bool operator()(chrono::nanoseconds __elapsed) const
> {
> if(__elapsed > chrono::microseconds(64))
> {
> auto const __monitor = __libcpp_atomic_monitor(__a);
> if(__test_fn())
> return true;
> __libcpp_atomic_wait(__a, __monitor);
> }
> else if(__elapsed > chrono::microseconds(4))
> __libcpp_thread_yield();
> else
> {} // poll
> return false;
> }
> };
> ```
> This could be your `wait` implmetation and it will work only in Clang.
> I dubit it any way conform C++ in implmetation, but as whole confom
> behavior of `wait` defined in standard text.
>
>
> Another place where we have sitation like this is `std::vector` you
> can't implmemt it in C++.
> You can copy paste it implmetation and it will work but if you change
> compiler it will break.
There are parts of the standard that are not implementable in pure C++,
but atomic wait in particular isn't one of them. Sure, it relies on
atomics being implemented in hardware as required and the underlying OS
for blocking and wakeup, but beyond that there's nothing magical about
it. In a sense, it is no more magical than std::time().
> pon., 18 lip 2022 o 22:27 Andrey Semashev via Std-Discussion
>>
>> Obviously, wait can be implemented as there are real world
>> implementations. The standard isn't written to demand something that is
>> impossible to implement.
>
> Yes, it is possible to implement but not in pure C++ as it could use
> compiler intrinsics
> or assembler. It could use code that have total ordering garatees or
> use memory feces.
>
> I grab random code with "wait" from:
> https://github.com/llvm/llvm-project/blob/main/libcxx/include/atomic
> ```
> template <class _Atp, class _Fn>
> struct __libcpp_atomic_wait_backoff_impl {
> _Atp* __a;
> _Fn __test_fn;
> _LIBCPP_AVAILABILITY_SYNC
> _LIBCPP_INLINE_VISIBILITY bool operator()(chrono::nanoseconds __elapsed) const
> {
> if(__elapsed > chrono::microseconds(64))
> {
> auto const __monitor = __libcpp_atomic_monitor(__a);
> if(__test_fn())
> return true;
> __libcpp_atomic_wait(__a, __monitor);
> }
> else if(__elapsed > chrono::microseconds(4))
> __libcpp_thread_yield();
> else
> {} // poll
> return false;
> }
> };
> ```
> This could be your `wait` implmetation and it will work only in Clang.
> I dubit it any way conform C++ in implmetation, but as whole confom
> behavior of `wait` defined in standard text.
>
>
> Another place where we have sitation like this is `std::vector` you
> can't implmemt it in C++.
> You can copy paste it implmetation and it will work but if you change
> compiler it will break.
There are parts of the standard that are not implementable in pure C++,
but atomic wait in particular isn't one of them. Sure, it relies on
atomics being implemented in hardware as required and the underlying OS
for blocking and wakeup, but beyond that there's nothing magical about
it. In a sense, it is no more magical than std::time().
Received on 2022-07-18 22:30:16