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 bool llvm::llvm_start_multithreaded() {
25 #if LLVM_ENABLE_THREADS != 0
26 assert(!multithreaded_mode && "Already multithreaded!");
27 multithreaded_mode = true;
29 // We fence here to ensure that all initialization is complete BEFORE we
30 // return from llvm_start_multithreaded().
38 void llvm::llvm_stop_multithreaded() {
39 #if LLVM_ENABLE_THREADS != 0
40 assert(multithreaded_mode && "Not currently multithreaded!");
42 // We fence here to insure that all threaded operations are complete BEFORE we
43 // return from llvm_stop_multithreaded().
46 multithreaded_mode = false;
50 bool llvm::llvm_is_multithreaded() {
51 return multithreaded_mode;
54 #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H)
58 void (*UserFn)(void *);
61 static void *ExecuteOnThread_Dispatch(void *Arg) {
62 ThreadInfo *TI = reinterpret_cast<ThreadInfo*>(Arg);
63 TI->UserFn(TI->UserData);
67 void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
68 unsigned RequestedStackSize) {
69 ThreadInfo Info = { Fn, UserData };
73 // Construct the attributes object.
74 if (::pthread_attr_init(&Attr) != 0)
77 // Set the requested stack size, if given.
78 if (RequestedStackSize != 0) {
79 if (::pthread_attr_setstacksize(&Attr, RequestedStackSize) != 0)
83 // Construct and execute the thread.
84 if (::pthread_create(&Thread, &Attr, ExecuteOnThread_Dispatch, &Info) != 0)
87 // Wait for the thread and clean up.
88 ::pthread_join(Thread, nullptr);
91 ::pthread_attr_destroy(&Attr);
93 #elif LLVM_ENABLE_THREADS!=0 && defined(LLVM_ON_WIN32)
94 #include "Windows/WindowsSupport.h"
102 static unsigned __stdcall ThreadCallback(void *param) {
103 struct ThreadInfo *info = reinterpret_cast<struct ThreadInfo *>(param);
104 info->func(info->param);
109 void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
110 unsigned RequestedStackSize) {
111 struct ThreadInfo param = { Fn, UserData };
113 HANDLE hThread = (HANDLE)::_beginthreadex(NULL,
114 RequestedStackSize, ThreadCallback,
118 // We actually don't care whether the wait succeeds or fails, in
119 // the same way we don't care whether the pthread_join call succeeds
120 // or fails. There's not much we could do if this were to fail. But
121 // on success, this call will wait until the thread finishes executing
123 (void)::WaitForSingleObject(hThread, INFINITE);
124 ::CloseHandle(hThread);
128 // Support for non-Win32, non-pthread implementation.
129 void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
130 unsigned RequestedStackSize) {
131 (void) RequestedStackSize;