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 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chandler Carruth | 39cd216 | 2014-06-27 15:13:01 +0000 | [diff] [blame] | 10 | // This file defines helper functions for running LLVM in a multi-threaded |
| 11 | // environment. |
Owen Anderson | 4cb4b61 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Threading.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 || \ |
| 40 | (!defined(LLVM_ON_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, |
Daniel Dunbar | cdd4c54 | 2010-11-04 01:26:25 +0000 | [diff] [blame] | 43 | unsigned RequestedStackSize) { |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 44 | (void)RequestedStackSize; |
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 | |
Zachary Turner | 640cee0 | 2017-03-03 21:49:38 +0000 | [diff] [blame] | 50 | uint64_t llvm::get_threadid() { return 0; } |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 51 | |
Zachary Turner | 1f004c4 | 2017-03-04 18:53:09 +0000 | [diff] [blame] | 52 | uint32_t llvm::get_max_thread_name_length() { return 0; } |
| 53 | |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 54 | void llvm::set_thread_name(const Twine &Name) {} |
| 55 | |
| 56 | void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); } |
| 57 | |
| 58 | #else |
Teresa Johnson | 2bd812c | 2016-10-14 00:13:59 +0000 | [diff] [blame] | 59 | |
Zachary Turner | 91db01f | 2017-03-03 17:39:24 +0000 | [diff] [blame] | 60 | #include <thread> |
Teresa Johnson | c0ef9e4 | 2016-10-17 14:56:53 +0000 | [diff] [blame] | 61 | unsigned llvm::heavyweight_hardware_concurrency() { |
Zachary Turner | 91db01f | 2017-03-03 17:39:24 +0000 | [diff] [blame] | 62 | // Since we can't get here unless LLVM_ENABLE_THREADS == 1, it is safe to use |
| 63 | // `std::thread` directly instead of `llvm::thread` (and indeed, doing so |
| 64 | // allows us to not define `thread` in the llvm namespace, which conflicts |
| 65 | // with some platforms such as FreeBSD whose headers also define a struct |
| 66 | // called `thread` in the global namespace which can cause ambiguity due to |
| 67 | // ADL. |
Teresa Johnson | 2bd812c | 2016-10-14 00:13:59 +0000 | [diff] [blame] | 68 | int NumPhysical = sys::getHostNumPhysicalCores(); |
| 69 | if (NumPhysical == -1) |
Zachary Turner | 91db01f | 2017-03-03 17:39:24 +0000 | [diff] [blame] | 70 | return std::thread::hardware_concurrency(); |
Teresa Johnson | 2bd812c | 2016-10-14 00:13:59 +0000 | [diff] [blame] | 71 | return NumPhysical; |
| 72 | } |
Zachary Turner | 757dbc9 | 2017-03-03 17:15:17 +0000 | [diff] [blame] | 73 | |
| 74 | // Include the platform-specific parts of this class. |
| 75 | #ifdef LLVM_ON_UNIX |
| 76 | #include "Unix/Threading.inc" |
| 77 | #endif |
| 78 | #ifdef LLVM_ON_WIN32 |
| 79 | #include "Windows/Threading.inc" |
| 80 | #endif |
| 81 | |
| 82 | #endif |