blob: 473c84808af16eabe8c36fd26be22d751bedd21f [file] [log] [blame]
Charles Davis54c9eb62010-11-29 19:44:50 +00001//===-- llvm/Support/Threading.cpp- Control multithreading mode --*- C++ -*-==//
Owen Anderson4cb4b612009-06-16 17:33:51 +00002//
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 Carruth39cd2162014-06-27 15:13:01 +000010// This file defines helper functions for running LLVM in a multi-threaded
11// environment.
Owen Anderson4cb4b612009-06-16 17:33:51 +000012//
13//===----------------------------------------------------------------------===//
14
Michael J. Spencer447762d2010-11-29 18:16:10 +000015#include "llvm/Support/Threading.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Config/config.h"
Teresa Johnson2bd812c2016-10-14 00:13:59 +000017#include "llvm/Support/Host.h"
Zachary Turner757dbc92017-03-03 17:15:17 +000018
Owen Anderson4cb4b612009-06-16 17:33:51 +000019#include <cassert>
Zachary Turner757dbc92017-03-03 17:15:17 +000020#include <errno.h>
21#include <stdlib.h>
22#include <string.h>
Owen Anderson2370b4d2009-07-06 21:24:37 +000023
Owen Anderson4cb4b612009-06-16 17:33:51 +000024using namespace llvm;
25
Zachary Turner757dbc92017-03-03 17:15:17 +000026//===----------------------------------------------------------------------===//
27//=== WARNING: Implementation here must contain only TRULY operating system
28//=== independent code.
29//===----------------------------------------------------------------------===//
30
Chandler Carruth39cd2162014-06-27 15:13:01 +000031bool llvm::llvm_is_multithreaded() {
Dylan Noblesmithefddf202011-11-28 00:48:58 +000032#if LLVM_ENABLE_THREADS != 0
Owen Anderson4cb4b612009-06-16 17:33:51 +000033 return true;
34#else
35 return false;
36#endif
37}
38
Zachary Turner757dbc92017-03-03 17:15:17 +000039#if LLVM_ENABLE_THREADS == 0 || \
40 (!defined(LLVM_ON_WIN32) && !defined(HAVE_PTHREAD_H))
NAKAMURA Takumidbd883b2011-09-19 07:41:43 +000041// Support for non-Win32, non-pthread implementation.
Zachary Turner757dbc92017-03-03 17:15:17 +000042void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData,
Daniel Dunbarcdd4c542010-11-04 01:26:25 +000043 unsigned RequestedStackSize) {
Zachary Turner757dbc92017-03-03 17:15:17 +000044 (void)RequestedStackSize;
Daniel Dunbarcdd4c542010-11-04 01:26:25 +000045 Fn(UserData);
46}
47
Zachary Turner757dbc92017-03-03 17:15:17 +000048unsigned llvm::heavyweight_hardware_concurrency() { return 1; }
49
Rafael Espindola8c0ff952017-10-04 20:27:01 +000050unsigned llvm::hardware_concurrency() { return 1; }
51
Zachary Turner640cee02017-03-03 21:49:38 +000052uint64_t llvm::get_threadid() { return 0; }
Zachary Turner757dbc92017-03-03 17:15:17 +000053
Zachary Turner1f004c42017-03-04 18:53:09 +000054uint32_t llvm::get_max_thread_name_length() { return 0; }
55
Zachary Turner757dbc92017-03-03 17:15:17 +000056void llvm::set_thread_name(const Twine &Name) {}
57
58void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
59
60#else
Teresa Johnson2bd812c2016-10-14 00:13:59 +000061
Zachary Turner91db01f2017-03-03 17:39:24 +000062#include <thread>
Teresa Johnsonc0ef9e42016-10-17 14:56:53 +000063unsigned llvm::heavyweight_hardware_concurrency() {
Zachary Turner91db01f2017-03-03 17:39:24 +000064 // Since we can't get here unless LLVM_ENABLE_THREADS == 1, it is safe to use
65 // `std::thread` directly instead of `llvm::thread` (and indeed, doing so
66 // allows us to not define `thread` in the llvm namespace, which conflicts
67 // with some platforms such as FreeBSD whose headers also define a struct
68 // called `thread` in the global namespace which can cause ambiguity due to
69 // ADL.
Teresa Johnson2bd812c2016-10-14 00:13:59 +000070 int NumPhysical = sys::getHostNumPhysicalCores();
71 if (NumPhysical == -1)
Zachary Turner91db01f2017-03-03 17:39:24 +000072 return std::thread::hardware_concurrency();
Teresa Johnson2bd812c2016-10-14 00:13:59 +000073 return NumPhysical;
74}
Zachary Turner757dbc92017-03-03 17:15:17 +000075
Rafael Espindola8c0ff952017-10-04 20:27:01 +000076unsigned llvm::hardware_concurrency() {
77#if defined(HAVE_SCHED_GETAFFINITY) && defined(HAVE_CPU_COUNT)
78 cpu_set_t Set;
79 if (sched_getaffinity(0, sizeof(Set), &Set))
80 return CPU_COUNT(&Set);
81#endif
82 // Guard against std::thread::hardware_concurrency() returning 0.
83 if (unsigned Val = std::thread::hardware_concurrency())
84 return Val;
85 return 1;
86}
87
Zachary Turner757dbc92017-03-03 17:15:17 +000088// Include the platform-specific parts of this class.
89#ifdef LLVM_ON_UNIX
90#include "Unix/Threading.inc"
91#endif
92#ifdef LLVM_ON_WIN32
93#include "Windows/Threading.inc"
94#endif
95
96#endif