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 #include "llvm/Support/thread.h"
19 
20 #include <cassert>
21 #include <errno.h>
22 #include <stdlib.h>
23 #include <string.h>
24 
25 using namespace llvm;
26 
27 //===----------------------------------------------------------------------===//
28 //=== WARNING: Implementation here must contain only TRULY operating system
29 //===          independent code.
30 //===----------------------------------------------------------------------===//
31 
32 bool llvm::llvm_is_multithreaded() {
33 #if LLVM_ENABLE_THREADS != 0
34   return true;
35 #else
36   return false;
37 #endif
38 }
39 
40 #if LLVM_ENABLE_THREADS == 0 ||                                                \
41     (!defined(LLVM_ON_WIN32) && !defined(HAVE_PTHREAD_H))
42 // Support for non-Win32, non-pthread implementation.
43 void llvm::llvm_execute_on_thread(void (*Fn)(void *), void *UserData,
44                                   unsigned RequestedStackSize) {
45   (void)RequestedStackSize;
46   Fn(UserData);
47 }
48 
49 unsigned llvm::heavyweight_hardware_concurrency() { return 1; }
50 
51 uint64_t llvm::get_threadid_np() { return 0; }
52 
53 void llvm::set_thread_name(const Twine &Name) {}
54 
55 void llvm::get_thread_name(SmallVectorImpl<char> &Name) { Name.clear(); }
56 
57 #else
58 
59 unsigned llvm::heavyweight_hardware_concurrency() {
60   int NumPhysical = sys::getHostNumPhysicalCores();
61   if (NumPhysical == -1)
62     return thread::hardware_concurrency();
63   return NumPhysical;
64 }
65 
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
75