25#define DISPENSO_MAJOR_VERSION 1
26#define DISPENSO_MINOR_VERSION 4
27#define DISPENSO_PATCH_VERSION 1
30#if __cplusplus >= 202002L && defined(__cpp_concepts) && __cpp_concepts >= 201907L
31#define DISPENSO_HAS_CONCEPTS 1
34#define DISPENSO_HAS_CONCEPTS 0
51#if DISPENSO_HAS_CONCEPTS
52#define DISPENSO_REQUIRES(...) requires(__VA_ARGS__)
54#define DISPENSO_REQUIRES(...)
57#if defined(DISPENSO_SHARED_LIB)
60#if defined(DISPENSO_LIB_EXPORT)
61#define DISPENSO_DLL_ACCESS __declspec(dllexport)
63#define DISPENSO_DLL_ACCESS __declspec(dllimport)
66#elif defined(__clang__) || defined(__GNUC__)
67#define DISPENSO_DLL_ACCESS __attribute__((visibility("default")))
71#if !defined(DISPENSO_DLL_ACCESS)
72#define DISPENSO_DLL_ACCESS
75using ssize_t = std::make_signed<std::size_t>::type;
77#if defined(__clang__) || defined(__GNUC__)
78#define DISPENSO_INLINE __attribute__((always_inline)) inline
79#elif defined(_MSC_VER) || defined(__INTEL_COMPILER)
80#define DISPENSO_INLINE __forceinline
82#define DISPENSO_INLINE inline
98#define DISPENSO_THREAD_LOCAL __declspec(thread)
99#elif defined(__GNUC__) || defined(__clang__)
100#define DISPENSO_THREAD_LOCAL __thread
102#error Supply lightweight thread-locals for this compiler. Can define to thread_local if lightweight not available
105#if (defined(__GNUC__) || defined(__clang__))
106#define DISPENSO_EXPECT(a, b) __builtin_expect(a, b)
108#define DISPENSO_EXPECT(a, b) a
112#if (defined(__GNUC__) || defined(__clang__))
113#define DO_PRAGMA(X) _Pragma(#X)
114#define DISPENSO_DISABLE_WARNING_PUSH DO_PRAGMA(GCC diagnostic push)
115#define DISPENSO_DISABLE_WARNING_POP DO_PRAGMA(GCC diagnostic pop)
116#define DISPENSO_DISABLE_WARNING(warningName) DO_PRAGMA(GCC diagnostic ignored #warningName)
117#if !defined(__clang__)
118#define DISPENSO_DISABLE_WARNING_ZERO_VARIADIC_MACRO_ARGUMENTS
119#define DISPENSO_DISABLE_WARNING_GLOBAL_CONSTRUCTORS
121#define DISPENSO_DISABLE_WARNING_ZERO_VARIADIC_MACRO_ARGUMENTS \
122 DISPENSO_DISABLE_WARNING(-Wgnu-zero-variadic-macro-arguments)
123#define DISPENSO_DISABLE_WARNING_GLOBAL_CONSTRUCTORS \
124 DISPENSO_DISABLE_WARNING(-Wglobal-constructors)
126#elif defined(_MSC_VER)
127#define DISPENSO_DISABLE_WARNING_PUSH __pragma(warning(push))
128#define DISPENSO_DISABLE_WARNING_POP __pragma(warning(pop))
129#define DISPENSO_DISABLE_WARNING(warningNumber) __pragma(warning(disable : warningNumber))
130#define DISPENSO_DISABLE_WARNING_ZERO_VARIADIC_MACRO_ARGUMENTS
131#define DISPENSO_DISABLE_WARNING_GLOBAL_CONSTRUCTORS
133#define DISPENSO_DISABLE_WARNING_PUSH
134#define DISPENSO_DISABLE_WARNING_POP
135#define DISPENSO_DISABLE_WARNING_ZERO_VARIADIC_MACRO_ARGUMENTS
136#define DISPENSO_DISABLE_WARNING_GLOBAL_CONSTRUCTORS
157 operator const T&()
const {
169 alignas(
alignof(T))
char b[
sizeof(T)];
173struct alignas(kCacheLineSize)
AlignedAtomic :
public std::atomic<T*> {};
175inline void* alignedMalloc(
size_t bytes,
size_t alignment) {
176 alignment = std::max(alignment,
sizeof(uintptr_t));
177 char* ptr =
reinterpret_cast<char*
>(::malloc(bytes + alignment));
178 uintptr_t base =
reinterpret_cast<uintptr_t
>(ptr);
179 uintptr_t oldBase = base;
180 uintptr_t mask = alignment - 1;
184 uintptr_t* recovery =
reinterpret_cast<uintptr_t*
>(base -
sizeof(uintptr_t));
186 return reinterpret_cast<void*
>(base);
189inline void* alignedMalloc(
size_t bytes) {
190 return alignedMalloc(bytes, kCacheLineSize);
193inline void alignedFree(
void* ptr) {
197 char* p =
reinterpret_cast<char*
>(ptr);
198 uintptr_t recovered = *
reinterpret_cast<uintptr_t*
>(p -
sizeof(uintptr_t));
199 ::free(
reinterpret_cast<void*
>(recovered));
203struct AlignedFreeDeleter {
204 void operator()(T* ptr) {
206 detail::alignedFree(ptr);
210struct AlignedFreeDeleter<void> {
211 void operator()(
void* ptr) {
212 detail::alignedFree(ptr);
216template <
typename T,
class... Args>
217std::shared_ptr<T> make_shared(Args&&... args) {
218 void* tv = alignedMalloc(
sizeof(T),
alignof(T));
219 T* t =
new (tv) T(std::forward<Args>(args)...);
220 return std::shared_ptr<T>(t, AlignedFreeDeleter<T>());
223inline constexpr uintptr_t alignToCacheLine(uintptr_t val) {
230#if defined __x86_64__ || defined __i386__
231inline void cpuRelax() {
232 asm volatile(
"pause" :::
"memory");
234#elif defined __arm64__ || defined __aarch64__
235inline void cpuRelax() {
236 asm volatile(
"yield" :::
"memory");
238#elif defined __powerpc__ || defined __POWERPC__
240inline void cpuRelax() {
241 asm volatile(
"or r27,r27,r27" :::
"memory");
244inline void cpuRelax() {
245 asm volatile(
"or 27,27,27" :::
"memory");
250inline void cpuRelax() {}
260 ssize_t transitionTaskIndex;
261 ssize_t ceilChunkSize;
264inline StaticChunking staticChunkSize(ssize_t items, ssize_t chunks) {
267 chunking.ceilChunkSize = (items + chunks - 1) / chunks;
268 ssize_t numLeft = chunking.ceilChunkSize * chunks - items;
269 chunking.transitionTaskIndex = chunks - numLeft;
detail::AlignedAtomic< T > AlignedAtomic
Cache-line aligned atomic pointer.
detail::AlignedBuffer< T > AlignedBuffer
Buffer with proper alignment for type T.
detail::StaticChunking StaticChunking
Information for statically chunking a range across threads.