xref: /aosp_15_r20/external/cronet/base/allocator/partition_allocator/src/partition_alloc/yield_processor.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2020 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #ifndef PARTITION_ALLOC_YIELD_PROCESSOR_H_
6 #define PARTITION_ALLOC_YIELD_PROCESSOR_H_
7 
8 #include "build/build_config.h"
9 #include "partition_alloc/partition_alloc_config.h"
10 
11 // The PA_YIELD_PROCESSOR macro wraps an architecture specific-instruction that
12 // informs the processor we're in a busy wait, so it can handle the branch more
13 // intelligently and e.g. reduce power to our core or give more resources to the
14 // other hyper-thread on this core. See the following for context:
15 // https://software.intel.com/en-us/articles/benefitting-power-and-performance-sleep-loops
16 
17 #if PA_CONFIG(IS_NONCLANG_MSVC)
18 
19 // MSVC is in its own assemblyless world (crbug.com/1351310#c6).
20 #include <windows.h>
21 #define PA_YIELD_PROCESSOR (YieldProcessor())
22 
23 #else
24 
25 #if defined(ARCH_CPU_X86_64) || defined(ARCH_CPU_X86)
26 #define PA_YIELD_PROCESSOR __asm__ __volatile__("pause")
27 #elif (defined(ARCH_CPU_ARMEL) && __ARM_ARCH >= 6) || defined(ARCH_CPU_ARM64)
28 #define PA_YIELD_PROCESSOR __asm__ __volatile__("yield")
29 #elif defined(ARCH_CPU_MIPSEL)
30 // The MIPS32 docs state that the PAUSE instruction is a no-op on older
31 // architectures (first added in MIPS32r2). To avoid assembler errors when
32 // targeting pre-r2, we must encode the instruction manually.
33 #define PA_YIELD_PROCESSOR __asm__ __volatile__(".word 0x00000140")
34 #elif defined(ARCH_CPU_MIPS64EL) && __mips_isa_rev >= 2
35 // Don't bother doing using .word here since r2 is the lowest supported mips64
36 // that Chromium supports.
37 #define PA_YIELD_PROCESSOR __asm__ __volatile__("pause")
38 #elif defined(ARCH_CPU_PPC64_FAMILY)
39 #define PA_YIELD_PROCESSOR __asm__ __volatile__("or 31,31,31")
40 #elif defined(ARCH_CPU_S390_FAMILY)
41 // just do nothing
42 #define PA_YIELD_PROCESSOR ((void)0)
43 #endif  // ARCH
44 
45 #ifndef PA_YIELD_PROCESSOR
46 #define PA_YIELD_PROCESSOR ((void)0)
47 #endif
48 
49 #endif  // PA_CONFIG(IS_NONCLANG_MSVC)
50 
51 #endif  // PARTITION_ALLOC_YIELD_PROCESSOR_H_
52