📨 chic
0.0.0
Realtime-safe channels in C
Toggle main menu visibility
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
50
extern
atomic_size_t
chic_catastrophe_threshold
;
51
54
void
chic_yield
(
void
);
55
65
static
inline
void
busy_wait_hint
(
void
) {
66
#if defined(__i386__) || defined(_M_IX86)
67
_mm_pause();
68
#elif defined(__aarch64__) || defined(_M_ARM64)
69
__isb(15);
70
#elif defined(__arm__) || defined(_M_ARM)
71
__yield();
72
#elif defined(__riscv) && defined(__riscv_zihintpause)
73
__asm__
volatile
(
"pause"
);
74
#endif
75
}
76
87
static
inline
void
busy_wait_on
(
const
void
*addr) {
88
#if defined(__riscv) && defined(__riscv_zawrs)
89
__asm__
volatile
(
90
"lr.w %0, (%0)\n\t"
91
"wrs.sto"
92
:
"+r"
(addr)
93
);
94
#else
98
busy_wait_hint
();
99
busy_wait_hint
();
100
busy_wait_hint
();
101
busy_wait_hint
();
102
(void)(addr);
103
#endif
104
}
105
106
#ifdef __cplusplus
107
}
108
#endif
busy_wait_on
static void busy_wait_on(const void *addr)
Definition
wait.h:87
chic_catastrophe_threshold
atomic_size_t chic_catastrophe_threshold
Maximum number of permitted busy-wait loop iterations per send/recieve operation before the thread is...
busy_wait_hint
static void busy_wait_hint(void)
Platform-dependent hint to the processor that it is in a busy-wait loop.
Definition
wait.h:65
chic_yield
void chic_yield(void)
Yields the current thread execution, allowing other threads to run.
include
wait.h
Generated on
for 📨 chic by
1.17.0