1 // Copyright 2021 the V8 project authors. All rights reserved. 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 V8_BASE_PLATFORM_YIELD_PROCESSOR_H_ 6 #define V8_BASE_PLATFORM_YIELD_PROCESSOR_H_ 7 8 // The YIELD_PROCESSOR macro wraps an architecture specific-instruction that 9 // informs the processor we're in a busy wait, so it can handle the branch more 10 // intelligently and e.g. reduce power to our core or give more resources to the 11 // other hyper-thread on this core. See the following for context: 12 // https://software.intel.com/en-us/articles/benefitting-power-and-performance-sleep-loops 13 14 #if defined(V8_CC_MSVC) 15 // MSVC does not support inline assembly via __asm__ and provides compiler 16 // intrinsics instead. Check if there is a usable intrinsic. 17 // 18 // intrin.h is an expensive header, so only include it if we're on a host 19 // architecture that has a usable intrinsic. 20 #if defined(V8_HOST_ARCH_IA32) || defined(V8_HOST_ARCH_X64) 21 #include <intrin.h> 22 #define YIELD_PROCESSOR _mm_pause() 23 #elif defined(V8_HOST_ARCH_ARM64) || \ 24 (defined(V8_HOST_ARCH_ARM) && __ARM_ARCH >= 6) 25 #include <intrin.h> 26 #define YIELD_PROCESSOR __yield() 27 #endif // V8_HOST_ARCH 28 29 #else // !V8_CC_MSVC 30 31 #if defined(V8_HOST_ARCH_IA32) || defined(V8_HOST_ARCH_X64) 32 #define YIELD_PROCESSOR __asm__ __volatile__("pause") 33 #elif defined(V8_HOST_ARCH_ARM64) || \ 34 (defined(V8_HOST_ARCH_ARM) && __ARM_ARCH >= 6) 35 #define YIELD_PROCESSOR __asm__ __volatile__("yield") 36 #elif defined(V8_HOST_ARCH_MIPS) 37 // The MIPS32 docs state that the PAUSE instruction is a no-op on older 38 // architectures (first added in MIPS32r2). To avoid assembler errors when 39 // targeting pre-r2, we must encode the instruction manually. 40 #define YIELD_PROCESSOR __asm__ __volatile__(".word 0x00000140") 41 #elif defined(V8_HOST_ARCH_MIPS64EL) && __mips_isa_rev >= 2 42 // Don't bother doing using .word here since r2 is the lowest supported mips64 43 // that Chromium supports. 44 #define YIELD_PROCESSOR __asm__ __volatile__("pause") 45 #elif defined(V8_HOST_ARCH_PPC64) 46 #define YIELD_PROCESSOR __asm__ __volatile__("or 31,31,31") 47 #endif // V8_HOST_ARCH 48 49 #endif // V8_CC_MSVC 50 51 #ifndef YIELD_PROCESSOR 52 #define YIELD_PROCESSOR ((void)0) 53 #endif 54 55 #endif // V8_BASE_PLATFORM_YIELD_PROCESSOR_H_ 56