Charles Davis | 54c9eb6 | 2010-11-29 19:44:50 +0000 | [diff] [blame] | 1 | //===-- llvm/Support/Threading.cpp- Control multithreading mode --*- C++ -*-==// |
Owen Anderson | 4cb4b61 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 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 | // |
NAKAMURA Takumi | 104e5f6 | 2014-06-24 13:36:31 +0000 | [diff] [blame] | 10 | // This file implements llvm_start_multithreaded() and friends. |
Owen Anderson | 4cb4b61 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 14 | #include "llvm/Support/Threading.h" |
Chandler Carruth | ed0881b | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 15 | #include "llvm/Config/config.h" |
Benjamin Kramer | 17388a6 | 2014-03-03 18:02:34 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Atomic.h" |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Mutex.h" |
Owen Anderson | 4cb4b61 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 18 | #include <cassert> |
Owen Anderson | 2370b4d | 2009-07-06 21:24:37 +0000 | [diff] [blame] | 19 | |
Owen Anderson | 4cb4b61 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
NAKAMURA Takumi | 104e5f6 | 2014-06-24 13:36:31 +0000 | [diff] [blame] | 22 | static bool multithreaded_mode = false; |
| 23 | |
| 24 | bool llvm::llvm_start_multithreaded() { |
Dylan Noblesmith | efddf20 | 2011-11-28 00:48:58 +0000 | [diff] [blame] | 25 | #if LLVM_ENABLE_THREADS != 0 |
NAKAMURA Takumi | 104e5f6 | 2014-06-24 13:36:31 +0000 | [diff] [blame] | 26 | assert(!multithreaded_mode && "Already multithreaded!"); |
| 27 | multithreaded_mode = true; |
| 28 | |
| 29 | // We fence here to ensure that all initialization is complete BEFORE we |
| 30 | // return from llvm_start_multithreaded(). |
| 31 | sys::MemoryFence(); |
Owen Anderson | 4cb4b61 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 32 | return true; |
| 33 | #else |
| 34 | return false; |
| 35 | #endif |
| 36 | } |
| 37 | |
NAKAMURA Takumi | 104e5f6 | 2014-06-24 13:36:31 +0000 | [diff] [blame] | 38 | void llvm::llvm_stop_multithreaded() { |
| 39 | #if LLVM_ENABLE_THREADS != 0 |
| 40 | assert(multithreaded_mode && "Not currently multithreaded!"); |
| 41 | |
| 42 | // We fence here to insure that all threaded operations are complete BEFORE we |
| 43 | // return from llvm_stop_multithreaded(). |
| 44 | sys::MemoryFence(); |
| 45 | |
| 46 | multithreaded_mode = false; |
| 47 | #endif |
| 48 | } |
| 49 | |
| 50 | bool llvm::llvm_is_multithreaded() { |
| 51 | return multithreaded_mode; |
| 52 | } |
| 53 | |
Dylan Noblesmith | efddf20 | 2011-11-28 00:48:58 +0000 | [diff] [blame] | 54 | #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H) |
Daniel Dunbar | cdd4c54 | 2010-11-04 01:26:25 +0000 | [diff] [blame] | 55 | #include <pthread.h> |
| 56 | |
| 57 | struct ThreadInfo { |
| 58 | void (*UserFn)(void *); |
| 59 | void *UserData; |
| 60 | }; |
| 61 | static void *ExecuteOnThread_Dispatch(void *Arg) { |
| 62 | ThreadInfo *TI = reinterpret_cast<ThreadInfo*>(Arg); |
| 63 | TI->UserFn(TI->UserData); |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 64 | return nullptr; |
Daniel Dunbar | cdd4c54 | 2010-11-04 01:26:25 +0000 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData, |
| 68 | unsigned RequestedStackSize) { |
| 69 | ThreadInfo Info = { Fn, UserData }; |
| 70 | pthread_attr_t Attr; |
| 71 | pthread_t Thread; |
| 72 | |
| 73 | // Construct the attributes object. |
| 74 | if (::pthread_attr_init(&Attr) != 0) |
| 75 | return; |
| 76 | |
| 77 | // Set the requested stack size, if given. |
| 78 | if (RequestedStackSize != 0) { |
| 79 | if (::pthread_attr_setstacksize(&Attr, RequestedStackSize) != 0) |
| 80 | goto error; |
| 81 | } |
| 82 | |
| 83 | // Construct and execute the thread. |
| 84 | if (::pthread_create(&Thread, &Attr, ExecuteOnThread_Dispatch, &Info) != 0) |
| 85 | goto error; |
| 86 | |
| 87 | // Wait for the thread and clean up. |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 88 | ::pthread_join(Thread, nullptr); |
Michael J. Spencer | 447762d | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 89 | |
Daniel Dunbar | cdd4c54 | 2010-11-04 01:26:25 +0000 | [diff] [blame] | 90 | error: |
| 91 | ::pthread_attr_destroy(&Attr); |
| 92 | } |
Dylan Noblesmith | efddf20 | 2011-11-28 00:48:58 +0000 | [diff] [blame] | 93 | #elif LLVM_ENABLE_THREADS!=0 && defined(LLVM_ON_WIN32) |
Reid Kleckner | d59e2fa | 2014-02-12 21:26:20 +0000 | [diff] [blame] | 94 | #include "Windows/WindowsSupport.h" |
NAKAMURA Takumi | dbd883b | 2011-09-19 07:41:43 +0000 | [diff] [blame] | 95 | #include <process.h> |
Daniel Dunbar | cdd4c54 | 2010-11-04 01:26:25 +0000 | [diff] [blame] | 96 | |
NAKAMURA Takumi | dbd883b | 2011-09-19 07:41:43 +0000 | [diff] [blame] | 97 | struct ThreadInfo { |
| 98 | void (*func)(void*); |
| 99 | void *param; |
| 100 | }; |
| 101 | |
| 102 | static unsigned __stdcall ThreadCallback(void *param) { |
| 103 | struct ThreadInfo *info = reinterpret_cast<struct ThreadInfo *>(param); |
| 104 | info->func(info->param); |
| 105 | |
| 106 | return 0; |
| 107 | } |
| 108 | |
| 109 | void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData, |
| 110 | unsigned RequestedStackSize) { |
| 111 | struct ThreadInfo param = { Fn, UserData }; |
| 112 | |
| 113 | HANDLE hThread = (HANDLE)::_beginthreadex(NULL, |
| 114 | RequestedStackSize, ThreadCallback, |
| 115 | ¶m, 0, NULL); |
| 116 | |
| 117 | if (hThread) { |
| 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 |
| 122 | // before returning. |
| 123 | (void)::WaitForSingleObject(hThread, INFINITE); |
| 124 | ::CloseHandle(hThread); |
| 125 | } |
| 126 | } |
Daniel Dunbar | cdd4c54 | 2010-11-04 01:26:25 +0000 | [diff] [blame] | 127 | #else |
NAKAMURA Takumi | dbd883b | 2011-09-19 07:41:43 +0000 | [diff] [blame] | 128 | // Support for non-Win32, non-pthread implementation. |
Daniel Dunbar | cdd4c54 | 2010-11-04 01:26:25 +0000 | [diff] [blame] | 129 | void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData, |
| 130 | unsigned RequestedStackSize) { |
| 131 | (void) RequestedStackSize; |
| 132 | Fn(UserData); |
| 133 | } |
| 134 | |
| 135 | #endif |