blob: 6a10b988d4648a9e95e42c2761b34f0717c52122 [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
Zachary Turner1f004c42017-03-04 18:53:09 +000052uint32_t llvm::get_max_thread_name_length() { return 0; }
53
Zachary Turner757dbc92017-03-03 17:15:17 +000054void llvm::set_thread_name(const Twine &Name) {}
55
56void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
57
58#else
Teresa Johnson2bd812c2016-10-14 00:13:59 +000059
Zachary Turner91db01f2017-03-03 17:39:24 +000060#include <thread>
Teresa Johnsonc0ef9e42016-10-17 14:56:53 +000061unsigned llvm::heavyweight_hardware_concurrency() {
Zachary Turner91db01f2017-03-03 17:39:24 +000062 // 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 Johnson2bd812c2016-10-14 00:13:59 +000068 int NumPhysical = sys::getHostNumPhysicalCores();
69 if (NumPhysical == -1)
Zachary Turner91db01f2017-03-03 17:39:24 +000070 return std::thread::hardware_concurrency();
Teresa Johnson2bd812c2016-10-14 00:13:59 +000071 return NumPhysical;
72}
Zachary Turner757dbc92017-03-03 17:15:17 +000073
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