]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Support/Threading.cpp
Import bmake-20170711
[FreeBSD/FreeBSD.git] / contrib / llvm / lib / Support / Threading.cpp
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/Atomic.h"
18 #include "llvm/Support/Host.h"
19 #include "llvm/Support/Mutex.h"
20 #include "llvm/Support/thread.h"
21 #include <cassert>
22
23 using namespace llvm;
24
25 bool llvm::llvm_is_multithreaded() {
26 #if LLVM_ENABLE_THREADS != 0
27   return true;
28 #else
29   return false;
30 #endif
31 }
32
33 #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H)
34 #include <pthread.h>
35
36 struct ThreadInfo {
37   void (*UserFn)(void *);
38   void *UserData;
39 };
40 static void *ExecuteOnThread_Dispatch(void *Arg) {
41   ThreadInfo *TI = reinterpret_cast<ThreadInfo*>(Arg);
42   TI->UserFn(TI->UserData);
43   return nullptr;
44 }
45
46 void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
47                                   unsigned RequestedStackSize) {
48   ThreadInfo Info = { Fn, UserData };
49   pthread_attr_t Attr;
50   pthread_t Thread;
51
52   // Construct the attributes object.
53   if (::pthread_attr_init(&Attr) != 0)
54     return;
55
56   // Set the requested stack size, if given.
57   if (RequestedStackSize != 0) {
58     if (::pthread_attr_setstacksize(&Attr, RequestedStackSize) != 0)
59       goto error;
60   }
61
62   // Construct and execute the thread.
63   if (::pthread_create(&Thread, &Attr, ExecuteOnThread_Dispatch, &Info) != 0)
64     goto error;
65
66   // Wait for the thread and clean up.
67   ::pthread_join(Thread, nullptr);
68
69  error:
70   ::pthread_attr_destroy(&Attr);
71 }
72 #elif LLVM_ENABLE_THREADS!=0 && defined(LLVM_ON_WIN32)
73 #include "Windows/WindowsSupport.h"
74 #include <process.h>
75
76 // Windows will at times define MemoryFence.
77 #ifdef MemoryFence
78 #undef MemoryFence
79 #endif
80
81 struct ThreadInfo {
82   void (*func)(void*);
83   void *param;
84 };
85
86 static unsigned __stdcall ThreadCallback(void *param) {
87   struct ThreadInfo *info = reinterpret_cast<struct ThreadInfo *>(param);
88   info->func(info->param);
89
90   return 0;
91 }
92
93 void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
94                                   unsigned RequestedStackSize) {
95   struct ThreadInfo param = { Fn, UserData };
96
97   HANDLE hThread = (HANDLE)::_beginthreadex(NULL,
98                                             RequestedStackSize, ThreadCallback,
99                                             &param, 0, NULL);
100
101   if (hThread) {
102     // We actually don't care whether the wait succeeds or fails, in
103     // the same way we don't care whether the pthread_join call succeeds
104     // or fails.  There's not much we could do if this were to fail. But
105     // on success, this call will wait until the thread finishes executing
106     // before returning.
107     (void)::WaitForSingleObject(hThread, INFINITE);
108     ::CloseHandle(hThread);
109   }
110 }
111 #else
112 // Support for non-Win32, non-pthread implementation.
113 void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
114                                   unsigned RequestedStackSize) {
115   (void) RequestedStackSize;
116   Fn(UserData);
117 }
118
119 #endif
120
121 unsigned llvm::heavyweight_hardware_concurrency() {
122 #if !LLVM_ENABLE_THREADS
123   return 1;
124 #endif
125   int NumPhysical = sys::getHostNumPhysicalCores();
126   if (NumPhysical == -1)
127     return thread::hardware_concurrency();
128   return NumPhysical;
129 }