📨 chic 0.0.0
Realtime-safe channels in C
Loading...
Searching...
No Matches
wait.h
Go to the documentation of this file.
1// This Source Code Form is subject to the terms of the Mozilla Public
2// License, v. 2.0. If a copy of the MPL was not distributed with this
3// file, You can obtain one at https://mozilla.org/MPL/2.0/.
4
9
10#pragma once
11#include <stddef.h>
12#if defined(__i386__) || defined(_M_IX86)
13 #include <immintrin.h>
14#elif defined (_M_ARM64) || defined(_M_ARM)
15 #include <intrin.h>
16#elif defined(__aarch64__) || defined(__arm__)
17 #if defined(__ARM_ACLE)
18 #include <arm_acle.h>
19 #elif defined (__GNUC__)
20 // HACK: GCC for ARM doesn't fully implement the ARM C Language
21 // extensions and thus chooses to not define __ARM_ACLE.
22 // Since ACLE are not available, use inline assembly, which
23 // GCC permits.
24
25 __attribute__((__always_inline__))
26 static inline void __yield(void) { __asm__ volatile ("yield"); }
27
28 __attribute__((__always_inline__))
29 static inline void __isb(unsigned int) {
30 __asm__ volatile ("isb 15" ::: "memory");
31 }
32 #else
33 #error Compiling for ARM, but ACLE are not available?
34 #endif
35#endif
36
37#ifdef __cplusplus
38 #include <atomic>
39 using atomic_size_t = std::atomic<size_t>;
40 extern "C" {
41#else
42 #include <stdatomic.h>
43#endif
44
48extern atomic_size_t chic_catastrophe_threshold;
49
52void chic_yield(void);
53
63static inline void busy_wait_hint(void) {
64#if defined(__i386__) || defined(_M_IX86)
65 _mm_pause();
66#elif defined(__aarch64__) || defined(_M_ARM64)
67 __isb(15);
68#elif defined(__arm__) || defined(_M_ARM)
69 __yield();
70#elif defined(__riscv) && defined(__riscv_zihintpause)
71 __asm__ volatile ("pause");
72#endif
73}
74
85static inline void busy_wait_on(const void *addr) {
86#if defined(__riscv) && defined(__riscv_zawrs)
87 __asm__ volatile (
88 "lr.w %0, (%0)\n\t"
89 "wrs.sto"
90 : "+r" (addr)
91 );
92#else
99 (void)(addr);
100#endif
101}
102
103#ifdef __cplusplus
104 }
105#endif
static void busy_wait_on(const void *addr)
Definition wait.h:85
atomic_size_t chic_catastrophe_threshold
Maximum number of permitted busy-wait loop iterations per send/recieve operation before the thread is...
static void busy_wait_hint(void)
Platform-dependent hint to the processor that it is in a busy-wait loop.
Definition wait.h:63
void chic_yield(void)
Yields the current thread execution, allowing other threads to run.