2010-11-30 03:44:50 +08:00
|
|
|
//===-- llvm/Support/Threading.cpp- Control multithreading mode --*- C++ -*-==//
|
2009-06-17 01:33:51 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-06-27 23:13:01 +08:00
|
|
|
// This file defines helper functions for running LLVM in a multi-threaded
|
|
|
|
// environment.
|
2009-06-17 01:33:51 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-30 02:16:10 +08:00
|
|
|
#include "llvm/Support/Threading.h"
|
2012-12-04 00:50:05 +08:00
|
|
|
#include "llvm/Config/config.h"
|
2016-10-14 08:13:59 +08:00
|
|
|
#include "llvm/Support/Host.h"
|
2017-03-04 01:15:17 +08:00
|
|
|
|
2009-06-17 01:33:51 +08:00
|
|
|
#include <cassert>
|
2017-03-04 01:15:17 +08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-07-07 05:24:37 +08:00
|
|
|
|
2009-06-17 01:33:51 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-03-04 01:15:17 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only TRULY operating system
|
|
|
|
//=== independent code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-06-27 23:13:01 +08:00
|
|
|
bool llvm::llvm_is_multithreaded() {
|
2011-11-28 08:48:58 +08:00
|
|
|
#if LLVM_ENABLE_THREADS != 0
|
2009-06-17 01:33:51 +08:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-04 01:15:17 +08:00
|
|
|
#if LLVM_ENABLE_THREADS == 0 || \
|
|
|
|
(!defined(LLVM_ON_WIN32) && !defined(HAVE_PTHREAD_H))
|
|
|
|
// Support for non-Win32, non-pthread implementation.
|
|
|
|
void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData,
|
2010-11-04 09:26:25 +08:00
|
|
|
unsigned RequestedStackSize) {
|
2017-03-04 01:15:17 +08:00
|
|
|
(void)RequestedStackSize;
|
|
|
|
Fn(UserData);
|
2010-11-04 09:26:25 +08:00
|
|
|
}
|
2016-06-03 02:42:23 +08:00
|
|
|
|
2017-03-04 01:15:17 +08:00
|
|
|
unsigned llvm::heavyweight_hardware_concurrency() { return 1; }
|
2010-11-04 09:26:25 +08:00
|
|
|
|
2017-10-05 04:27:01 +08:00
|
|
|
unsigned llvm::hardware_concurrency() { return 1; }
|
|
|
|
|
2017-03-04 05:49:38 +08:00
|
|
|
uint64_t llvm::get_threadid() { return 0; }
|
2011-09-19 15:41:43 +08:00
|
|
|
|
2017-03-05 02:53:09 +08:00
|
|
|
uint32_t llvm::get_max_thread_name_length() { return 0; }
|
|
|
|
|
2017-03-04 01:15:17 +08:00
|
|
|
void llvm::set_thread_name(const Twine &Name) {}
|
2011-09-19 15:41:43 +08:00
|
|
|
|
2017-03-04 01:15:17 +08:00
|
|
|
void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
|
2011-09-19 15:41:43 +08:00
|
|
|
|
|
|
|
#else
|
2016-10-14 08:13:59 +08:00
|
|
|
|
2017-03-04 01:39:24 +08:00
|
|
|
#include <thread>
|
2016-10-17 22:56:53 +08:00
|
|
|
unsigned llvm::heavyweight_hardware_concurrency() {
|
2017-03-04 01:39:24 +08:00
|
|
|
// Since we can't get here unless LLVM_ENABLE_THREADS == 1, it is safe to use
|
|
|
|
// `std::thread` directly instead of `llvm::thread` (and indeed, doing so
|
|
|
|
// allows us to not define `thread` in the llvm namespace, which conflicts
|
|
|
|
// with some platforms such as FreeBSD whose headers also define a struct
|
|
|
|
// called `thread` in the global namespace which can cause ambiguity due to
|
|
|
|
// ADL.
|
2016-10-14 08:13:59 +08:00
|
|
|
int NumPhysical = sys::getHostNumPhysicalCores();
|
|
|
|
if (NumPhysical == -1)
|
2017-03-04 01:39:24 +08:00
|
|
|
return std::thread::hardware_concurrency();
|
2016-10-14 08:13:59 +08:00
|
|
|
return NumPhysical;
|
|
|
|
}
|
2017-03-04 01:15:17 +08:00
|
|
|
|
2017-10-05 04:27:01 +08:00
|
|
|
unsigned llvm::hardware_concurrency() {
|
|
|
|
#if defined(HAVE_SCHED_GETAFFINITY) && defined(HAVE_CPU_COUNT)
|
|
|
|
cpu_set_t Set;
|
|
|
|
if (sched_getaffinity(0, sizeof(Set), &Set))
|
|
|
|
return CPU_COUNT(&Set);
|
|
|
|
#endif
|
|
|
|
// Guard against std::thread::hardware_concurrency() returning 0.
|
|
|
|
if (unsigned Val = std::thread::hardware_concurrency())
|
|
|
|
return Val;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2017-03-04 01:15:17 +08:00
|
|
|
// Include the platform-specific parts of this class.
|
|
|
|
#ifdef LLVM_ON_UNIX
|
|
|
|
#include "Unix/Threading.inc"
|
|
|
|
#endif
|
|
|
|
#ifdef LLVM_ON_WIN32
|
|
|
|
#include "Windows/Threading.inc"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|