Skip to content

Commit 7789fb6

Browse files
authored
[libc][NFC] Move 'sleep_briefly' function to common header (llvm#83074)
Summary: The llvm#83026 patch has another use for this function. Additionally add support for the Arm instruction barrier if this is ever used by other targets.
1 parent 796d26a commit 7789fb6

File tree

3 files changed

+42
-15
lines changed

3 files changed

+42
-15
lines changed

libc/src/__support/RPC/rpc_util.h

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,28 +11,15 @@
1111

1212
#include "src/__support/CPP/type_traits.h"
1313
#include "src/__support/GPU/utils.h"
14-
#include "src/__support/macros/attributes.h" // LIBC_INLINE
14+
#include "src/__support/macros/attributes.h"
1515
#include "src/__support/macros/properties/architectures.h"
16+
#include "src/__support/threads/sleep.h"
1617
#include "src/string/memory_utils/generic/byte_per_byte.h"
1718
#include "src/string/memory_utils/inline_memcpy.h"
1819

1920
namespace LIBC_NAMESPACE {
2021
namespace rpc {
2122

22-
/// Suspend the thread briefly to assist the thread scheduler during busy loops.
23-
LIBC_INLINE void sleep_briefly() {
24-
#if defined(LIBC_TARGET_ARCH_IS_NVPTX)
25-
if (__nvvm_reflect("__CUDA_ARCH") >= 700)
26-
LIBC_INLINE_ASM("nanosleep.u32 64;" ::: "memory");
27-
#elif defined(LIBC_TARGET_ARCH_IS_AMDGPU)
28-
__builtin_amdgcn_s_sleep(2);
29-
#elif defined(LIBC_TARGET_ARCH_IS_X86)
30-
__builtin_ia32_pause();
31-
#else
32-
// Simply do nothing if sleeping isn't supported on this platform.
33-
#endif
34-
}
35-
3623
/// Conditional to indicate if this process is running on the GPU.
3724
LIBC_INLINE constexpr bool is_process_gpu() {
3825
#if defined(LIBC_TARGET_ARCH_IS_GPU)

libc/src/__support/threads/CMakeLists.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@ add_header_library(
44
mutex_common.h
55
)
66

7+
add_header_library(
8+
sleep
9+
HDRS
10+
sleep.h
11+
)
12+
713
if(EXISTS ${CMAKE_CURRENT_SOURCE_DIR}/${LIBC_TARGET_OS})
814
add_subdirectory(${LIBC_TARGET_OS})
915
endif()

libc/src/__support/threads/sleep.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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.
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

0 commit comments

Comments
 (0)