Charles Davis | 54c9eb6 | 2010-11-29 19:44:50 +0000 | [diff] [blame] | 1 | //===-- llvm/Support/Threading.cpp- Control multithreading mode --*- C++ -*-==// |
Owen Anderson | 4cb4b61 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 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 |
Owen Anderson | 4cb4b61 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
Chandler Carruth | 39cd216 | 2014-06-27 15:13:01 +0000 | [diff] [blame] | 9 | // This file defines helper functions for running LLVM in a multi-threaded |
| 10 | // environment. |
Owen Anderson | 4cb4b61 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Threading.h" |
Sam McCall | a9c3c17 | 2019-10-23 15:34:48 +0200 | [diff] [blame] | 15 | #include "llvm/ADT/Optional.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/Config/config.h" |
Teresa Johnson | 2bd812c | 2016-10-14 00:13:59 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Host.h" |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 18 | |
Owen Anderson | 4cb4b61 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 19 | #include <cassert> |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 20 | #include <errno.h> |
| 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
Owen Anderson | 2370b4d | 2009-07-06 21:24:37 +0000 | [diff] [blame] | 23 | |
Owen Anderson | 4cb4b61 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 24 | using namespace llvm; |
| 25 | |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 26 | //===----------------------------------------------------------------------===// |
| 27 | //=== WARNING: Implementation here must contain only TRULY operating system |
| 28 | //=== independent code. |
| 29 | //===----------------------------------------------------------------------===// |
| 30 | |
Chandler Carruth | 39cd216 | 2014-06-27 15:13:01 +0000 | [diff] [blame] | 31 | bool llvm::llvm_is_multithreaded() { |
Dylan Noblesmith | efddf20 | 2011-11-28 00:48:58 +0000 | [diff] [blame] | 32 | #if LLVM_ENABLE_THREADS != 0 |
Owen Anderson | 4cb4b61 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 33 | return true; |
| 34 | #else |
| 35 | return false; |
| 36 | #endif |
| 37 | } |
| 38 | |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 39 | #if LLVM_ENABLE_THREADS == 0 || \ |
Nico Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 40 | (!defined(_WIN32) && !defined(HAVE_PTHREAD_H)) |
NAKAMURA Takumi | dbd883b | 2011-09-19 07:41:43 +0000 | [diff] [blame] | 41 | // Support for non-Win32, non-pthread implementation. |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 42 | void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData, |
Sam McCall | a9c3c17 | 2019-10-23 15:34:48 +0200 | [diff] [blame] | 43 | llvm::Optional<unsigned> StackSizeInBytes) { |
| 44 | (void)StackSizeInBytes; |
Daniel Dunbar | cdd4c54 | 2010-11-04 01:26:25 +0000 | [diff] [blame] | 45 | Fn(UserData); |
| 46 | } |
| 47 | |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 48 | unsigned llvm::heavyweight_hardware_concurrency() { return 1; } |
| 49 | |
Rafael Espindola | 8c0ff95 | 2017-10-04 20:27:01 +0000 | [diff] [blame] | 50 | unsigned llvm::hardware_concurrency() { return 1; } |
| 51 | |
Zachary Turner | 640cee0 | 2017-03-03 21:49:38 +0000 | [diff] [blame] | 52 | uint64_t llvm::get_threadid() { return 0; } |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 53 | |
Zachary Turner | 1f004c4 | 2017-03-04 18:53:09 +0000 | [diff] [blame] | 54 | uint32_t llvm::get_max_thread_name_length() { return 0; } |
| 55 | |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 56 | void llvm::set_thread_name(const Twine &Name) {} |
| 57 | |
| 58 | void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); } |
| 59 | |
Sam McCall | a9c3c17 | 2019-10-23 15:34:48 +0200 | [diff] [blame] | 60 | #if LLVM_ENABLE_THREADS == 0 |
| 61 | void llvm::llvm_execute_on_thread_async( |
| 62 | llvm::unique_function<void()> Func, |
| 63 | llvm::Optional<unsigned> StackSizeInBytes) { |
| 64 | (void)Func; |
| 65 | (void)StackSizeInBytes; |
| 66 | report_fatal_error("Spawning a detached thread doesn't make sense with no " |
| 67 | "threading support"); |
| 68 | } |
| 69 | #else |
| 70 | // Support for non-Win32, non-pthread implementation. |
| 71 | void llvm::llvm_execute_on_thread_async( |
| 72 | llvm::unique_function<void()> Func, |
| 73 | llvm::Optional<unsigned> StackSizeInBytes) { |
| 74 | (void)StackSizeInBytes; |
| 75 | std::thread(std::move(Func)).detach(); |
| 76 | } |
| 77 | #endif |
| 78 | |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 79 | #else |
Teresa Johnson | 2bd812c | 2016-10-14 00:13:59 +0000 | [diff] [blame] | 80 | |
Zachary Turner | 91db01f | 2017-03-03 17:39:24 +0000 | [diff] [blame] | 81 | #include <thread> |
Teresa Johnson | c0ef9e4 | 2016-10-17 14:56:53 +0000 | [diff] [blame] | 82 | unsigned llvm::heavyweight_hardware_concurrency() { |
Zachary Turner | 91db01f | 2017-03-03 17:39:24 +0000 | [diff] [blame] | 83 | // Since we can't get here unless LLVM_ENABLE_THREADS == 1, it is safe to use |
| 84 | // `std::thread` directly instead of `llvm::thread` (and indeed, doing so |
| 85 | // allows us to not define `thread` in the llvm namespace, which conflicts |
| 86 | // with some platforms such as FreeBSD whose headers also define a struct |
| 87 | // called `thread` in the global namespace which can cause ambiguity due to |
| 88 | // ADL. |
Teresa Johnson | 2bd812c | 2016-10-14 00:13:59 +0000 | [diff] [blame] | 89 | int NumPhysical = sys::getHostNumPhysicalCores(); |
| 90 | if (NumPhysical == -1) |
Zachary Turner | 91db01f | 2017-03-03 17:39:24 +0000 | [diff] [blame] | 91 | return std::thread::hardware_concurrency(); |
Teresa Johnson | 2bd812c | 2016-10-14 00:13:59 +0000 | [diff] [blame] | 92 | return NumPhysical; |
| 93 | } |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 94 | |
Rafael Espindola | 8c0ff95 | 2017-10-04 20:27:01 +0000 | [diff] [blame] | 95 | unsigned llvm::hardware_concurrency() { |
| 96 | #if defined(HAVE_SCHED_GETAFFINITY) && defined(HAVE_CPU_COUNT) |
| 97 | cpu_set_t Set; |
| 98 | if (sched_getaffinity(0, sizeof(Set), &Set)) |
| 99 | return CPU_COUNT(&Set); |
| 100 | #endif |
| 101 | // Guard against std::thread::hardware_concurrency() returning 0. |
| 102 | if (unsigned Val = std::thread::hardware_concurrency()) |
| 103 | return Val; |
| 104 | return 1; |
| 105 | } |
| 106 | |
Sam McCall | a9c3c17 | 2019-10-23 15:34:48 +0200 | [diff] [blame] | 107 | namespace { |
| 108 | struct SyncThreadInfo { |
| 109 | void (*UserFn)(void *); |
| 110 | void *UserData; |
| 111 | }; |
| 112 | |
| 113 | using AsyncThreadInfo = llvm::unique_function<void()>; |
| 114 | |
| 115 | enum class JoiningPolicy { Join, Detach }; |
| 116 | } // namespace |
| 117 | |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 118 | // Include the platform-specific parts of this class. |
| 119 | #ifdef LLVM_ON_UNIX |
| 120 | #include "Unix/Threading.inc" |
| 121 | #endif |
Nico Weber | 712e8d2 | 2018-04-29 00:45:03 +0000 | [diff] [blame] | 122 | #ifdef _WIN32 |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 123 | #include "Windows/Threading.inc" |
| 124 | #endif |
| 125 | |
Sam McCall | a9c3c17 | 2019-10-23 15:34:48 +0200 | [diff] [blame] | 126 | void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData, |
| 127 | llvm::Optional<unsigned> StackSizeInBytes) { |
| 128 | |
| 129 | SyncThreadInfo Info = {Fn, UserData}; |
| 130 | llvm_execute_on_thread_impl(threadFuncSync, &Info, StackSizeInBytes, |
| 131 | JoiningPolicy::Join); |
| 132 | } |
| 133 | |
| 134 | void llvm::llvm_execute_on_thread_async( |
| 135 | llvm::unique_function<void()> Func, |
| 136 | llvm::Optional<unsigned> StackSizeInBytes) { |
| 137 | llvm_execute_on_thread_impl(&threadFuncAsync, |
| 138 | new AsyncThreadInfo(std::move(Func)), |
| 139 | StackSizeInBytes, JoiningPolicy::Detach); |
| 140 | } |
| 141 | |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 142 | #endif |