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/Support/Atomic.h"
16 #include "llvm/Support/Mutex.h"
17 #include "llvm/Config/config.h"
22 static bool multithreaded_mode = false;
24 static sys::Mutex* global_lock = 0;
26 bool llvm::llvm_start_multithreaded() {
27 #if LLVM_ENABLE_THREADS != 0
28 assert(!multithreaded_mode && "Already multithreaded!");
29 multithreaded_mode = true;
30 global_lock = new sys::Mutex(true);
32 // We fence here to ensure that all initialization is complete BEFORE we
33 // return from llvm_start_multithreaded().
41 void llvm::llvm_stop_multithreaded() {
42 #if LLVM_ENABLE_THREADS != 0
43 assert(multithreaded_mode && "Not currently multithreaded!");
45 // We fence here to insure that all threaded operations are complete BEFORE we
46 // return from llvm_stop_multithreaded().
49 multithreaded_mode = false;
54 bool llvm::llvm_is_multithreaded() {
55 return multithreaded_mode;
58 void llvm::llvm_acquire_global_lock() {
59 if (multithreaded_mode) global_lock->acquire();
62 void llvm::llvm_release_global_lock() {
63 if (multithreaded_mode) global_lock->release();
66 #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H)
70 void (*UserFn)(void *);
73 static void *ExecuteOnThread_Dispatch(void *Arg) {
74 ThreadInfo *TI = reinterpret_cast<ThreadInfo*>(Arg);
75 TI->UserFn(TI->UserData);
79 void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
80 unsigned RequestedStackSize) {
81 ThreadInfo Info = { Fn, UserData };
85 // Construct the attributes object.
86 if (::pthread_attr_init(&Attr) != 0)
89 // Set the requested stack size, if given.
90 if (RequestedStackSize != 0) {
91 if (::pthread_attr_setstacksize(&Attr, RequestedStackSize) != 0)
95 // Construct and execute the thread.
96 if (::pthread_create(&Thread, &Attr, ExecuteOnThread_Dispatch, &Info) != 0)
99 // Wait for the thread and clean up.
100 ::pthread_join(Thread, 0);
103 ::pthread_attr_destroy(&Attr);
105 #elif LLVM_ENABLE_THREADS!=0 && defined(LLVM_ON_WIN32)
106 #include "Windows/Windows.h"
114 static unsigned __stdcall ThreadCallback(void *param) {
115 struct ThreadInfo *info = reinterpret_cast<struct ThreadInfo *>(param);
116 info->func(info->param);
121 void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
122 unsigned RequestedStackSize) {
123 struct ThreadInfo param = { Fn, UserData };
125 HANDLE hThread = (HANDLE)::_beginthreadex(NULL,
126 RequestedStackSize, ThreadCallback,
130 // We actually don't care whether the wait succeeds or fails, in
131 // the same way we don't care whether the pthread_join call succeeds
132 // or fails. There's not much we could do if this were to fail. But
133 // on success, this call will wait until the thread finishes executing
135 (void)::WaitForSingleObject(hThread, INFINITE);
136 ::CloseHandle(hThread);
140 // Support for non-Win32, non-pthread implementation.
141 void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
142 unsigned RequestedStackSize) {
143 (void) RequestedStackSize;