native 0.0.1
Vectors, masks and wide register packs for C++26
Loading...
Searching...
No Matches
native.x86.wait.ccm
1// SPDX-FileCopyrightText: 2024 Edward Kmett <ekmett@gmail.com>
2// SPDX-License-Identifier: BSD-2-Clause OR Apache-2.0
3module;
4#include "native/isa_import.h"
5#include <cstdint>
6#include <x86intrin.h>
7#include <hint.h>
8#include "native/targets.h"
9
11
12namespace native {
17
22
26
29 template <typename T>
30 concept waiter = requires (volatile void * p, std::uint32_t t) {
31 bool(T::supported);
32 T::monitor(p);
33 T::mwait(t);
34 };
35
45 template <waiter W> hint_flatten
46 void wait_until(volatile auto * p, auto f)
47 noexcept(noexcept(!f(p)) && noexcept(W::monitor(p)) && noexcept(W::mwait(0))) hint_blocking {
48 [[assume(W::supported)]];
49 while (!f(p)) {
50 W::monitor(p);
51 if (!f(p))
52 W::mwait(0);
53 }
54 }
55
57 struct mwaitx {
58 using timer_t = std::uint64_t;
61 [[gnu::target("mwaitx")]] static void monitor(volatile void * p) noexcept;
64 [[gnu::target("mwaitx")]] static void mwait(std::uint32_t timer = 0) noexcept hint_blocking;
66 static const bool supported;
67 };
68
69
71 struct umwait {
72 using timer_t = std::uint64_t;
75 [[gnu::target("waitpkg")]] static void monitor(volatile void * p) noexcept;
78 [[gnu::target("waitpkg")]] static std::uint8_t mwait(std::uint64_t timer = 0) noexcept hint_blocking;
80 static const bool supported;
81 };
82
87 template<isa<x86> Arch = NATIVE_BASELINE, unsigned Control = 1>
88 requires(Arch.has(x86_feature::waitpkg) && Control < 2)
89 hint_inline hint_artificial hint_target("waitpkg")
90 std::uint8_t tpause(std::uint64_t deadline) noexcept hint_blocking {
91 return __builtin_ia32_tpause(Control, std::uint32_t(deadline >> 32),
92 std::uint32_t(deadline));
93 }
94
96 struct spin {
97 using timer_t = std::uint64_t;
99 hint_inline hint_artificial static void monitor(volatile void *) noexcept {}
101 hint_inline hint_artificial static void mwait(std::uint32_t = 0) noexcept hint_blocking { _mm_pause(); }
103 inline static constinit bool supported = true;
104 };
105
114 auto with_waiter(auto k)
115 noexcept(noexcept(k.template operator()<mwaitx>()) &&
116 noexcept(k.template operator()<umwait>()) && noexcept(k.template operator()<spin>())) {
117 if (mwaitx::supported) return k.template operator()<mwaitx>();
118 else if (umwait::supported) return k.template operator()<umwait>();
119 else return k.template operator()<spin>();
120 }
121
123}
124
125namespace native {
126 // Global-module instruction definitions preserve intrinsic linkage.
127 // Out-of-line calls let the waiter dispatch remain at the baseline target.
128 [[gnu::target("mwaitx")]] void mwaitx::monitor(volatile void * p) noexcept {
129 _mm_monitorx(const_cast<void *>(p), 0, 0);
130 }
131 [[gnu::target("mwaitx")]] void mwaitx::mwait(std::uint32_t timer) noexcept hint_blocking {
132 _mm_mwaitx(0, 0, timer);
133 }
134 [[gnu::target("waitpkg")]] void umwait::monitor(volatile void * p) noexcept {
135 _umonitor(const_cast<void *>(p));
136 }
137 [[gnu::target("waitpkg")]] std::uint8_t umwait::mwait(std::uint64_t timer) noexcept hint_blocking {
138 return _umwait(1, timer);
139 }
140
141 namespace {
142 // Share the common observer and admission rules, including leaf validity.
143 // Waiting needs no vector state or unrelated SIMD instruction features.
144 const auto wait_capabilities = observe_x86_capabilities();
145 }
146 const bool mwaitx::supported = classify_isa(wait_capabilities, x86_feature::mwaitx).admitted();
147 const bool umwait::supported = classify_isa(wait_capabilities, x86_feature::waitpkg).admitted();
148}
149
150export module native.x86.wait;
151export import native.x86.features;
152
153export namespace native {
154 using ::native::waiter;
155 using ::native::wait_until;
156 using ::native::mwaitx;
157 using ::native::umwait;
158 using ::native::tpause;
159 using ::native::spin;
160 using ::native::with_waiter;
161}
some way to wait for a value to change
std::uint8_t tpause(std::uint64_t deadline) noexcept hint_blocking
auto with_waiter(auto k) noexcept(noexcept(k.template operator()<mwaitx >()) &&noexcept(k.template operator()<umwait >()) &&noexcept(k.template operator()<spin >()))
finds an appropriate waiter for the current CPU Prefer MWAITX, then WAITPKG, otherwise PAUSE....
void wait_until(volatile auto *p, auto f) noexcept(noexcept(!f(p)) &&noexcept(W::monitor(p)) &&noexcept(W::mwait(0))) hint_blocking
Wait until a predicate holds about a given memory location.
static void monitor(volatile void *p) noexcept
static void mwait(std::uint32_t timer=0) noexcept hint_blocking
static void monitor(volatile void *p) noexcept
static std::uint8_t mwait(std::uint64_t timer=0) noexcept hint_blocking
Architecture-tagged vectors, register packs and supporting value types. Native arithmetic follows its...
x86_capabilities observe_x86_capabilities() noexcept
Capture CPUID and, only with XSAVE and OSXSAVE, XCR0. No ISA profile required.
Standard-library adaptations documented here for SIMD value types.
waiter using the MONITORX/MWAITX instruction feature
A universally supported x86 spin waiter using PAUSE; monitor is a no-op.
static void monitor(volatile void *) noexcept
Ignore the address; PAUSE waiting does not arm a hardware monitor.
static void mwait(std::uint32_t=0) noexcept hint_blocking
Issue one PAUSE, ignoring the timer; no sleep duration or memory ordering is promised.
waiter using the WAITPKG instruction feature