33943efcd683e8232152fa8a4a8e038f363ce771
[oota-llvm.git] / 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 implements llvm_start_multithreaded() and friends.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Support/Threading.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/Atomic.h"
17 #include "llvm/Support/Mutex.h"
18 #include <cassert>
19
20 using namespace llvm;
21
22 static bool multithreaded_mode = false;
23
24 sys::Mutex& llvm::llvm_get_global_lock() {
25   static sys::Mutex global_lock;
26   return global_lock;
27 }
28
29 bool llvm::llvm_start_multithreaded() {
30 #if LLVM_ENABLE_THREADS != 0
31   assert(!multithreaded_mode && "Already multithreaded!");
32   multithreaded_mode = true;
33
34   // We fence here to ensure that all initialization is complete BEFORE we
35   // return from llvm_start_multithreaded().
36   sys::MemoryFence();
37   return true;
38 #else
39   return false;
40 #endif
41 }
42
43 void llvm::llvm_stop_multithreaded() {
44 #if LLVM_ENABLE_THREADS != 0
45   assert(multithreaded_mode && "Not currently multithreaded!");
46
47   // We fence here to insure that all threaded operations are complete BEFORE we
48   // return from llvm_stop_multithreaded().
49   sys::MemoryFence();
50
51   multithreaded_mode = false;
52 #endif
53 }
54
55 bool llvm::llvm_is_multithreaded() {
56   return multithreaded_mode;
57 }
58
59 #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H)
60 #include <pthread.h>
61
62 struct ThreadInfo {
63   void (*UserFn)(void *);
64   void *UserData;
65 };
66 static void *ExecuteOnThread_Dispatch(void *Arg) {
67   ThreadInfo *TI = reinterpret_cast<ThreadInfo*>(Arg);
68   TI->UserFn(TI->UserData);
69   return nullptr;
70 }
71
72 void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
73                                   unsigned RequestedStackSize) {
74   ThreadInfo Info = { Fn, UserData };
75   pthread_attr_t Attr;
76   pthread_t Thread;
77
78   // Construct the attributes object.
79   if (::pthread_attr_init(&Attr) != 0)
80     return;
81
82   // Set the requested stack size, if given.
83   if (RequestedStackSize != 0) {
84     if (::pthread_attr_setstacksize(&Attr, RequestedStackSize) != 0)
85       goto error;
86   }
87
88   // Construct and execute the thread.
89   if (::pthread_create(&Thread, &Attr, ExecuteOnThread_Dispatch, &Info) != 0)
90     goto error;
91
92   // Wait for the thread and clean up.
93   ::pthread_join(Thread, nullptr);
94
95  error:
96   ::pthread_attr_destroy(&Attr);
97 }
98 #elif LLVM_ENABLE_THREADS!=0 && defined(LLVM_ON_WIN32)
99 #include "Windows/WindowsSupport.h"
100 #include <process.h>
101
102 struct ThreadInfo {
103   void (*func)(void*);
104   void *param;
105 };
106
107 static unsigned __stdcall ThreadCallback(void *param) {
108   struct ThreadInfo *info = reinterpret_cast<struct ThreadInfo *>(param);
109   info->func(info->param);
110
111   return 0;
112 }
113
114 void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
115                                   unsigned RequestedStackSize) {
116   struct ThreadInfo param = { Fn, UserData };
117
118   HANDLE hThread = (HANDLE)::_beginthreadex(NULL,
119                                             RequestedStackSize, ThreadCallback,
120                                             &param, 0, NULL);
121
122   if (hThread) {
123     // We actually don't care whether the wait succeeds or fails, in
124     // the same way we don't care whether the pthread_join call succeeds
125     // or fails.  There's not much we could do if this were to fail. But
126     // on success, this call will wait until the thread finishes executing
127     // before returning.
128     (void)::WaitForSingleObject(hThread, INFINITE);
129     ::CloseHandle(hThread);
130   }
131 }
132 #else
133 // Support for non-Win32, non-pthread implementation.
134 void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
135                                   unsigned RequestedStackSize) {
136   (void) RequestedStackSize;
137   Fn(UserData);
138 }
139
140 #endif