blob: e49d328106928e92321b6a57df8c7e07f880aa0c [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
Zachary Turner640cee02017-03-03 21:49:38 +000050uint64_t llvm::get_threadid() { return 0; }
Zachary Turner757dbc92017-03-03 17:15:17 +000051
52void llvm::set_thread_name(const Twine &Name) {}
53
54void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
55
56#else
Teresa Johnson2bd812c2016-10-14 00:13:59 +000057
Zachary Turner91db01f2017-03-03 17:39:24 +000058#include <thread>
Teresa Johnsonc0ef9e42016-10-17 14:56:53 +000059unsigned llvm::heavyweight_hardware_concurrency() {
Zachary Turner91db01f2017-03-03 17:39:24 +000060 // Since we can't get here unless LLVM_ENABLE_THREADS == 1, it is safe to use
61 // `std::thread` directly instead of `llvm::thread` (and indeed, doing so
62 // allows us to not define `thread` in the llvm namespace, which conflicts
63 // with some platforms such as FreeBSD whose headers also define a struct
64 // called `thread` in the global namespace which can cause ambiguity due to
65 // ADL.
Teresa Johnson2bd812c2016-10-14 00:13:59 +000066 int NumPhysical = sys::getHostNumPhysicalCores();
67 if (NumPhysical == -1)
Zachary Turner91db01f2017-03-03 17:39:24 +000068 return std::thread::hardware_concurrency();
Teresa Johnson2bd812c2016-10-14 00:13:59 +000069 return NumPhysical;
70}
Zachary Turner757dbc92017-03-03 17:15:17 +000071
72// Include the platform-specific parts of this class.
73#ifdef LLVM_ON_UNIX
74#include "Unix/Threading.inc"
75#endif
76#ifdef LLVM_ON_WIN32
77#include "Windows/Threading.inc"
78#endif
79
80#endif