LLVM  5.0.0
Threading.cpp
Go to the documentation of this file.
1 //===-- llvm/Support/Threading.cpp- Control multithreading mode --*- C++ -*-==//
2 //
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 //
10 // This file defines helper functions for running LLVM in a multi-threaded
11 // environment.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #include "llvm/Support/Threading.h"
16 #include "llvm/Config/config.h"
17 #include "llvm/Support/Host.h"
18 
19 #include <cassert>
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 using namespace llvm;
25 
26 //===----------------------------------------------------------------------===//
27 //=== WARNING: Implementation here must contain only TRULY operating system
28 //=== independent code.
29 //===----------------------------------------------------------------------===//
30 
32 #if LLVM_ENABLE_THREADS != 0
33  return true;
34 #else
35  return false;
36 #endif
37 }
38 
39 #if LLVM_ENABLE_THREADS == 0 || \
40  (!defined(LLVM_ON_WIN32) && !defined(HAVE_PTHREAD_H))
41 // Support for non-Win32, non-pthread implementation.
42 void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData,
43  unsigned RequestedStackSize) {
44  (void)RequestedStackSize;
45  Fn(UserData);
46 }
47 
48 unsigned llvm::heavyweight_hardware_concurrency() { return 1; }
49 
50 uint64_t llvm::get_threadid() { return 0; }
51 
53 
54 void llvm::set_thread_name(const Twine &Name) {}
55 
57 
58 #else
59 
60 #include <thread>
62  // 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.
68  int NumPhysical = sys::getHostNumPhysicalCores();
69  if (NumPhysical == -1)
70  return std::thread::hardware_concurrency();
71  return NumPhysical;
72 }
73 
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
void set_thread_name(const Twine &Name)
Set the name of the current thread.
unsigned heavyweight_hardware_concurrency()
Get the amount of currency to use for tasks requiring significant memory or other resources...
Definition: Threading.cpp:61
int getHostNumPhysicalCores()
Get the number of physical cores (as opposed to logical cores returned from thread::hardware_concurre...
Definition: Host.cpp:1304
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
uint32_t get_max_thread_name_length()
Get the maximum length of a thread name on this platform.
void get_thread_name(SmallVectorImpl< char > &Name)
Get the name of the current thread.
const char * Name
bool llvm_is_multithreaded()
Returns true if LLVM is compiled with support for multi-threading, and false otherwise.
Definition: Threading.cpp:31
uint64_t get_threadid()
Return the current thread id, as used in various OS system calls.
void llvm_execute_on_thread(void(*UserFn)(void *), void *UserData, unsigned RequestedStackSize=0)
llvm_execute_on_thread - Execute the given UserFn on a separate thread, passing it the provided UserD...