blob: a9ecf9b10bf4a0804a1805f679def4378c6d6f27 [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"
Chandler Carruthdd146382016-06-02 18:22:12 +000018#include "llvm/Support/thread.h"
Zachary Turner757dbc92017-03-03 17:15:17 +000019
Owen Anderson4cb4b612009-06-16 17:33:51 +000020#include <cassert>
Zachary Turner757dbc92017-03-03 17:15:17 +000021#include <errno.h>
22#include <stdlib.h>
23#include <string.h>
Owen Anderson2370b4d2009-07-06 21:24:37 +000024
Owen Anderson4cb4b612009-06-16 17:33:51 +000025using namespace llvm;
26
Zachary Turner757dbc92017-03-03 17:15:17 +000027//===----------------------------------------------------------------------===//
28//=== WARNING: Implementation here must contain only TRULY operating system
29//=== independent code.
30//===----------------------------------------------------------------------===//
31
Chandler Carruth39cd2162014-06-27 15:13:01 +000032bool llvm::llvm_is_multithreaded() {
Dylan Noblesmithefddf202011-11-28 00:48:58 +000033#if LLVM_ENABLE_THREADS != 0
Owen Anderson4cb4b612009-06-16 17:33:51 +000034 return true;
35#else
36 return false;
37#endif
38}
39
Zachary Turner757dbc92017-03-03 17:15:17 +000040#if LLVM_ENABLE_THREADS == 0 || \
41 (!defined(LLVM_ON_WIN32) && !defined(HAVE_PTHREAD_H))
NAKAMURA Takumidbd883b2011-09-19 07:41:43 +000042// Support for non-Win32, non-pthread implementation.
Zachary Turner757dbc92017-03-03 17:15:17 +000043void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData,
Daniel Dunbarcdd4c542010-11-04 01:26:25 +000044 unsigned RequestedStackSize) {
Zachary Turner757dbc92017-03-03 17:15:17 +000045 (void)RequestedStackSize;
Daniel Dunbarcdd4c542010-11-04 01:26:25 +000046 Fn(UserData);
47}
48
Zachary Turner757dbc92017-03-03 17:15:17 +000049unsigned llvm::heavyweight_hardware_concurrency() { return 1; }
50
51uint64_t llvm::get_threadid_np() { return 0; }
52
53void llvm::set_thread_name(const Twine &Name) {}
54
55void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
56
57#else
Teresa Johnson2bd812c2016-10-14 00:13:59 +000058
Teresa Johnsonc0ef9e42016-10-17 14:56:53 +000059unsigned llvm::heavyweight_hardware_concurrency() {
Teresa Johnson2bd812c2016-10-14 00:13:59 +000060 int NumPhysical = sys::getHostNumPhysicalCores();
61 if (NumPhysical == -1)
62 return thread::hardware_concurrency();
63 return NumPhysical;
64}
Zachary Turner757dbc92017-03-03 17:15:17 +000065
66// Include the platform-specific parts of this class.
67#ifdef LLVM_ON_UNIX
68#include "Unix/Threading.inc"
69#endif
70#ifdef LLVM_ON_WIN32
71#include "Windows/Threading.inc"
72#endif
73
74#endif