2010-11-29 20:44:50 +01:00
|
|
|
//===-- llvm/Support/Threading.cpp- Control multithreading mode --*- C++ -*-==//
|
2009-06-16 19:33:51 +02:00
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2009-06-16 19:33:51 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2014-06-27 17:13:01 +02:00
|
|
|
// This file defines helper functions for running LLVM in a multi-threaded
|
|
|
|
// environment.
|
2009-06-16 19:33:51 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2010-11-29 19:16:10 +01:00
|
|
|
#include "llvm/Support/Threading.h"
|
2012-12-03 17:50:05 +01:00
|
|
|
#include "llvm/Config/config.h"
|
2016-10-14 02:13:59 +02:00
|
|
|
#include "llvm/Support/Host.h"
|
2017-03-03 18:15:17 +01:00
|
|
|
|
2009-06-16 19:33:51 +02:00
|
|
|
#include <cassert>
|
2017-03-03 18:15:17 +01:00
|
|
|
#include <errno.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-07-06 23:24:37 +02:00
|
|
|
|
2009-06-16 19:33:51 +02:00
|
|
|
using namespace llvm;
|
|
|
|
|
2017-03-03 18:15:17 +01:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//=== WARNING: Implementation here must contain only TRULY operating system
|
|
|
|
//=== independent code.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2014-06-27 17:13:01 +02:00
|
|
|
bool llvm::llvm_is_multithreaded() {
|
2011-11-28 01:48:58 +01:00
|
|
|
#if LLVM_ENABLE_THREADS != 0
|
2009-06-16 19:33:51 +02:00
|
|
|
return true;
|
|
|
|
#else
|
|
|
|
return false;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-03-03 18:15:17 +01:00
|
|
|
#if LLVM_ENABLE_THREADS == 0 || \
|
2018-04-29 02:45:03 +02:00
|
|
|
(!defined(_WIN32) && !defined(HAVE_PTHREAD_H))
|
2017-03-03 18:15:17 +01:00
|
|
|
// Support for non-Win32, non-pthread implementation.
|
|
|
|
void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData,
|
2010-11-04 02:26:25 +01:00
|
|
|
unsigned RequestedStackSize) {
|
2017-03-03 18:15:17 +01:00
|
|
|
(void)RequestedStackSize;
|
|
|
|
Fn(UserData);
|
2010-11-04 02:26:25 +01:00
|
|
|
}
|
2016-06-02 20:42:23 +02:00
|
|
|
|
2017-03-03 18:15:17 +01:00
|
|
|
unsigned llvm::heavyweight_hardware_concurrency() { return 1; }
|
2010-11-04 02:26:25 +01:00
|
|
|
|
2017-10-04 22:27:01 +02:00
|
|
|
unsigned llvm::hardware_concurrency() { return 1; }
|
|
|
|
|
2017-03-03 22:49:38 +01:00
|
|
|
uint64_t llvm::get_threadid() { return 0; }
|
2011-09-19 09:41:43 +02:00
|
|
|
|
2017-03-04 19:53:09 +01:00
|
|
|
uint32_t llvm::get_max_thread_name_length() { return 0; }
|
|
|
|
|
2017-03-03 18:15:17 +01:00
|
|
|
void llvm::set_thread_name(const Twine &Name) {}
|
2011-09-19 09:41:43 +02:00
|
|
|
|
2017-03-03 18:15:17 +01:00
|
|
|
void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
|
2011-09-19 09:41:43 +02:00
|
|
|
|
|
|
|
#else
|
2016-10-14 02:13:59 +02:00
|
|
|
|
2017-03-03 18:39:24 +01:00
|
|
|
#include <thread>
|
2016-10-17 16:56:53 +02:00
|
|
|
unsigned llvm::heavyweight_hardware_concurrency() {
|
2017-03-03 18:39:24 +01: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 02:13:59 +02:00
|
|
|
int NumPhysical = sys::getHostNumPhysicalCores();
|
|
|
|
if (NumPhysical == -1)
|
2017-03-03 18:39:24 +01:00
|
|
|
return std::thread::hardware_concurrency();
|
2016-10-14 02:13:59 +02:00
|
|
|
return NumPhysical;
|
|
|
|
}
|
2017-03-03 18:15:17 +01:00
|
|
|
|
2017-10-04 22:27:01 +02: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-03 18:15:17 +01:00
|
|
|
// Include the platform-specific parts of this class.
|
|
|
|
#ifdef LLVM_ON_UNIX
|
|
|
|
#include "Unix/Threading.inc"
|
|
|
|
#endif
|
2018-04-29 02:45:03 +02:00
|
|
|
#ifdef _WIN32
|
2017-03-03 18:15:17 +01:00
|
|
|
#include "Windows/Threading.inc"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|