1 //===-- llvm/Support/Threading.cpp- Control multithreading mode --*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements llvm_start_multithreaded() and friends.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Support/Threading.h"
15 #include "llvm/Config/config.h"
16 #include "llvm/Support/Atomic.h"
17 #include "llvm/Support/Mutex.h"
22 static bool multithreaded_mode = false;
24 sys::Mutex& llvm::llvm_get_global_lock() {
25 static sys::Mutex global_lock;
29 bool llvm::llvm_start_multithreaded() {
30 #if LLVM_ENABLE_THREADS != 0
31 assert(!multithreaded_mode && "Already multithreaded!");
32 multithreaded_mode = true;
34 // We fence here to ensure that all initialization is complete BEFORE we
35 // return from llvm_start_multithreaded().
43 void llvm::llvm_stop_multithreaded() {
44 #if LLVM_ENABLE_THREADS != 0
45 assert(multithreaded_mode && "Not currently multithreaded!");
47 // We fence here to insure that all threaded operations are complete BEFORE we
48 // return from llvm_stop_multithreaded().
51 multithreaded_mode = false;
55 bool llvm::llvm_is_multithreaded() {
56 return multithreaded_mode;
59 #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H)
63 void (*UserFn)(void *);
66 static void *ExecuteOnThread_Dispatch(void *Arg) {
67 ThreadInfo *TI = reinterpret_cast<ThreadInfo*>(Arg);
68 TI->UserFn(TI->UserData);
72 void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
73 unsigned RequestedStackSize) {
74 ThreadInfo Info = { Fn, UserData };
78 // Construct the attributes object.
79 if (::pthread_attr_init(&Attr) != 0)
82 // Set the requested stack size, if given.
83 if (RequestedStackSize != 0) {
84 if (::pthread_attr_setstacksize(&Attr, RequestedStackSize) != 0)
88 // Construct and execute the thread.
89 if (::pthread_create(&Thread, &Attr, ExecuteOnThread_Dispatch, &Info) != 0)
92 // Wait for the thread and clean up.
93 ::pthread_join(Thread, nullptr);
96 ::pthread_attr_destroy(&Attr);
98 #elif LLVM_ENABLE_THREADS!=0 && defined(LLVM_ON_WIN32)
99 #include "Windows/WindowsSupport.h"
107 static unsigned __stdcall ThreadCallback(void *param) {
108 struct ThreadInfo *info = reinterpret_cast<struct ThreadInfo *>(param);
109 info->func(info->param);
114 void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
115 unsigned RequestedStackSize) {
116 struct ThreadInfo param = { Fn, UserData };
118 HANDLE hThread = (HANDLE)::_beginthreadex(NULL,
119 RequestedStackSize, ThreadCallback,
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
128 (void)::WaitForSingleObject(hThread, INFINITE);
129 ::CloseHandle(hThread);
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;