• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===-- Utilities for suspending threads ----------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #ifndef LLVM_LIBC_SRC___SUPPORT_THREADS_SLEEP_H
10 #define LLVM_LIBC_SRC___SUPPORT_THREADS_SLEEP_H
11 
12 #include "src/__support/macros/attributes.h"
13 
14 namespace LIBC_NAMESPACE {
15 
16 /// Suspend the thread briefly to assist the thread scheduler during busy loops.
sleep_briefly()17 LIBC_INLINE void sleep_briefly() {
18 #if defined(LIBC_TARGET_ARCH_IS_NVPTX)
19   if (__nvvm_reflect("__CUDA_ARCH") >= 700)
20     LIBC_INLINE_ASM("nanosleep.u32 64;" ::: "memory");
21 #elif defined(LIBC_TARGET_ARCH_IS_AMDGPU)
22   __builtin_amdgcn_s_sleep(2);
23 #elif defined(LIBC_TARGET_ARCH_IS_X86)
24   __builtin_ia32_pause();
25 #elif defined(LIBC_TARGET_ARCH_IS_AARCH64)
26   __builtin_arm_isb(0xf);
27 #else
28   // Simply do nothing if sleeping isn't supported on this platform.
29 #endif
30 }
31 
32 } // namespace LIBC_NAMESPACE
33 
34 #endif // LLVM_LIBC_SRC___SUPPORT_THREADS_SLEEP_H
35