xref: /llvm-project/libc/src/__support/threads/sleep.h (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
17789fb66SJoseph Huber //===-- Utilities for suspending threads ----------------------------------===//
27789fb66SJoseph Huber //
37789fb66SJoseph Huber // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
47789fb66SJoseph Huber // See https://llvm.org/LICENSE.txt for license information.
57789fb66SJoseph Huber // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
67789fb66SJoseph Huber //
77789fb66SJoseph Huber //===----------------------------------------------------------------------===//
87789fb66SJoseph Huber 
97789fb66SJoseph Huber #ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_SLEEP_H
107789fb66SJoseph Huber #define LLVM_LIBC_SRC___SUPPORT_THREADS_SLEEP_H
117789fb66SJoseph Huber 
127789fb66SJoseph Huber #include "src/__support/macros/attributes.h"
13*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
147789fb66SJoseph Huber 
15*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
167789fb66SJoseph Huber 
177789fb66SJoseph Huber /// Suspend the thread briefly to assist the thread scheduler during busy loops.
187789fb66SJoseph Huber LIBC_INLINE void sleep_briefly() {
197789fb66SJoseph Huber #if defined(LIBC_TARGET_ARCH_IS_NVPTX)
207789fb66SJoseph Huber   if (__nvvm_reflect("__CUDA_ARCH") >= 700)
217789fb66SJoseph Huber     LIBC_INLINE_ASM("nanosleep.u32 64;" ::: "memory");
227789fb66SJoseph Huber #elif defined(LIBC_TARGET_ARCH_IS_AMDGPU)
237789fb66SJoseph Huber   __builtin_amdgcn_s_sleep(2);
247789fb66SJoseph Huber #elif defined(LIBC_TARGET_ARCH_IS_X86)
257789fb66SJoseph Huber   __builtin_ia32_pause();
261b26bb0dSSchrodinger ZHU Yifan #elif defined(LIBC_TARGET_ARCH_IS_AARCH64) && __has_builtin(__builtin_arm_isb)
277789fb66SJoseph Huber   __builtin_arm_isb(0xf);
281b26bb0dSSchrodinger ZHU Yifan #elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
291b26bb0dSSchrodinger ZHU Yifan   asm volatile("isb\n" ::: "memory");
307789fb66SJoseph Huber #else
317789fb66SJoseph Huber   // Simply do nothing if sleeping isn't supported on this platform.
327789fb66SJoseph Huber #endif
337789fb66SJoseph Huber }
347789fb66SJoseph Huber 
35*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
367789fb66SJoseph Huber 
377789fb66SJoseph Huber #endif // LLVM_LIBC_SRC___SUPPORT_THREADS_SLEEP_H
38