blob: e8f5622d0e5c84bfee227c963ca6ba9dbd4be8e1 [file] [log] [blame]
Charles Davis54c9eb62010-11-29 19:44:50 +00001//===-- llvm/Support/Threading.cpp- Control multithreading mode --*- C++ -*-==//
Owen Anderson4cb4b612009-06-16 17:33:51 +00002//
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//
Chandler Carruth39cd2162014-06-27 15:13:01 +000010// This file defines helper functions for running LLVM in a multi-threaded
11// environment.
Owen Anderson4cb4b612009-06-16 17:33:51 +000012//
13//===----------------------------------------------------------------------===//
14
Michael J. Spencer447762d2010-11-29 18:16:10 +000015#include "llvm/Support/Threading.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000016#include "llvm/Config/config.h"
Benjamin Kramer17388a62014-03-03 18:02:34 +000017#include "llvm/Support/Atomic.h"
Michael J. Spencer447762d2010-11-29 18:16:10 +000018#include "llvm/Support/Mutex.h"
Chandler Carruthdd146382016-06-02 18:22:12 +000019#include "llvm/Support/thread.h"
Owen Anderson4cb4b612009-06-16 17:33:51 +000020#include <cassert>
Owen Anderson2370b4d2009-07-06 21:24:37 +000021
Owen Anderson4cb4b612009-06-16 17:33:51 +000022using namespace llvm;
23
Chandler Carruth39cd2162014-06-27 15:13:01 +000024bool llvm::llvm_is_multithreaded() {
Dylan Noblesmithefddf202011-11-28 00:48:58 +000025#if LLVM_ENABLE_THREADS != 0
Owen Anderson4cb4b612009-06-16 17:33:51 +000026 return true;
27#else
28 return false;
29#endif
30}
31
Dylan Noblesmithefddf202011-11-28 00:48:58 +000032#if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H)
Daniel Dunbarcdd4c542010-11-04 01:26:25 +000033#include <pthread.h>
34
35struct ThreadInfo {
36 void (*UserFn)(void *);
37 void *UserData;
38};
39static void *ExecuteOnThread_Dispatch(void *Arg) {
40 ThreadInfo *TI = reinterpret_cast<ThreadInfo*>(Arg);
41 TI->UserFn(TI->UserData);
Craig Topperc10719f2014-04-07 04:17:22 +000042 return nullptr;
Daniel Dunbarcdd4c542010-11-04 01:26:25 +000043}
44
45void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
46 unsigned RequestedStackSize) {
47 ThreadInfo Info = { Fn, UserData };
48 pthread_attr_t Attr;
49 pthread_t Thread;
50
51 // Construct the attributes object.
52 if (::pthread_attr_init(&Attr) != 0)
53 return;
54
55 // Set the requested stack size, if given.
56 if (RequestedStackSize != 0) {
57 if (::pthread_attr_setstacksize(&Attr, RequestedStackSize) != 0)
58 goto error;
59 }
60
61 // Construct and execute the thread.
62 if (::pthread_create(&Thread, &Attr, ExecuteOnThread_Dispatch, &Info) != 0)
63 goto error;
64
65 // Wait for the thread and clean up.
Craig Topperc10719f2014-04-07 04:17:22 +000066 ::pthread_join(Thread, nullptr);
Michael J. Spencer447762d2010-11-29 18:16:10 +000067
Daniel Dunbarcdd4c542010-11-04 01:26:25 +000068 error:
69 ::pthread_attr_destroy(&Attr);
70}
Dylan Noblesmithefddf202011-11-28 00:48:58 +000071#elif LLVM_ENABLE_THREADS!=0 && defined(LLVM_ON_WIN32)
Reid Klecknerd59e2fa2014-02-12 21:26:20 +000072#include "Windows/WindowsSupport.h"
NAKAMURA Takumidbd883b2011-09-19 07:41:43 +000073#include <process.h>
Daniel Dunbarcdd4c542010-11-04 01:26:25 +000074
Chandler Carruthd6c5bc22016-06-02 18:42:23 +000075// Windows will at times define MemoryFence.
76#ifdef MemoryFence
77#undef MemoryFence
78#endif
79
NAKAMURA Takumidbd883b2011-09-19 07:41:43 +000080struct ThreadInfo {
81 void (*func)(void*);
82 void *param;
83};
84
85static unsigned __stdcall ThreadCallback(void *param) {
86 struct ThreadInfo *info = reinterpret_cast<struct ThreadInfo *>(param);
87 info->func(info->param);
88
89 return 0;
90}
91
92void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
93 unsigned RequestedStackSize) {
94 struct ThreadInfo param = { Fn, UserData };
95
96 HANDLE hThread = (HANDLE)::_beginthreadex(NULL,
97 RequestedStackSize, ThreadCallback,
98 &param, 0, NULL);
99
100 if (hThread) {
101 // We actually don't care whether the wait succeeds or fails, in
102 // the same way we don't care whether the pthread_join call succeeds
103 // or fails. There's not much we could do if this were to fail. But
104 // on success, this call will wait until the thread finishes executing
105 // before returning.
106 (void)::WaitForSingleObject(hThread, INFINITE);
107 ::CloseHandle(hThread);
108 }
109}
Daniel Dunbarcdd4c542010-11-04 01:26:25 +0000110#else
NAKAMURA Takumidbd883b2011-09-19 07:41:43 +0000111// Support for non-Win32, non-pthread implementation.
Daniel Dunbarcdd4c542010-11-04 01:26:25 +0000112void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData,
113 unsigned RequestedStackSize) {
114 (void) RequestedStackSize;
115 Fn(UserData);
116}
117
118#endif