| Charles Davis | 53ca1f3 | 2010-11-29 19:44:50 +0000 | [diff] [blame] | 1 | //===-- llvm/Support/Threading.cpp- Control multithreading mode --*- C++ -*-==// |
| Owen Anderson | 4c7ac18 | 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 | // |
| Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 10 | // This file defines helper functions for running LLVM in a multi-threaded |
| 11 | // environment. |
| Owen Anderson | 4c7ac18 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Threading.h" |
| Chandler Carruth | d04a8d4 | 2012-12-03 16:50:05 +0000 | [diff] [blame] | 16 | #include "llvm/Config/config.h" |
| Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Atomic.h" |
| 18 | #include "llvm/Support/Mutex.h" |
| Owen Anderson | 4c7ac18 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 19 | #include <cassert> |
| Owen Anderson | 643dda1 | 2009-07-06 21:24:37 +0000 | [diff] [blame] | 20 | |
| Owen Anderson | 4c7ac18 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 21 | using namespace llvm; |
| 22 | |
| Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 23 | bool llvm::llvm_is_multithreaded() { |
| Dylan Noblesmith | 08b73a3 | 2011-11-28 00:48:58 +0000 | [diff] [blame] | 24 | #if LLVM_ENABLE_THREADS != 0 |
| Owen Anderson | 4c7ac18 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 25 | return true; |
| 26 | #else |
| 27 | return false; |
| 28 | #endif |
| 29 | } |
| 30 | |
| Dylan Noblesmith | 08b73a3 | 2011-11-28 00:48:58 +0000 | [diff] [blame] | 31 | #if LLVM_ENABLE_THREADS != 0 && defined(HAVE_PTHREAD_H) |
| Daniel Dunbar | ea8e206 | 2010-11-04 01:26:25 +0000 | [diff] [blame] | 32 | #include <pthread.h> |
| 33 | |
| 34 | struct ThreadInfo { |
| 35 | void (*UserFn)(void *); |
| 36 | void *UserData; |
| 37 | }; |
| 38 | static void *ExecuteOnThread_Dispatch(void *Arg) { |
| 39 | ThreadInfo *TI = reinterpret_cast<ThreadInfo*>(Arg); |
| 40 | TI->UserFn(TI->UserData); |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 41 | return nullptr; |
| Daniel Dunbar | ea8e206 | 2010-11-04 01:26:25 +0000 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData, |
| 45 | unsigned RequestedStackSize) { |
| 46 | ThreadInfo Info = { Fn, UserData }; |
| 47 | pthread_attr_t Attr; |
| 48 | pthread_t Thread; |
| 49 | |
| 50 | // Construct the attributes object. |
| 51 | if (::pthread_attr_init(&Attr) != 0) |
| 52 | return; |
| 53 | |
| 54 | // Set the requested stack size, if given. |
| 55 | if (RequestedStackSize != 0) { |
| 56 | if (::pthread_attr_setstacksize(&Attr, RequestedStackSize) != 0) |
| 57 | goto error; |
| 58 | } |
| 59 | |
| 60 | // Construct and execute the thread. |
| 61 | if (::pthread_create(&Thread, &Attr, ExecuteOnThread_Dispatch, &Info) != 0) |
| 62 | goto error; |
| 63 | |
| 64 | // Wait for the thread and clean up. |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 65 | ::pthread_join(Thread, nullptr); |
| Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 66 | |
| Daniel Dunbar | ea8e206 | 2010-11-04 01:26:25 +0000 | [diff] [blame] | 67 | error: |
| 68 | ::pthread_attr_destroy(&Attr); |
| 69 | } |
| Dylan Noblesmith | 08b73a3 | 2011-11-28 00:48:58 +0000 | [diff] [blame] | 70 | #elif LLVM_ENABLE_THREADS!=0 && defined(LLVM_ON_WIN32) |
| Stephen Hines | 36b5688 | 2014-04-23 16:57:46 -0700 | [diff] [blame] | 71 | #include "Windows/WindowsSupport.h" |
| NAKAMURA Takumi | 92ec8e1 | 2011-09-19 07:41:43 +0000 | [diff] [blame] | 72 | #include <process.h> |
| Daniel Dunbar | ea8e206 | 2010-11-04 01:26:25 +0000 | [diff] [blame] | 73 | |
| NAKAMURA Takumi | 92ec8e1 | 2011-09-19 07:41:43 +0000 | [diff] [blame] | 74 | struct ThreadInfo { |
| 75 | void (*func)(void*); |
| 76 | void *param; |
| 77 | }; |
| 78 | |
| 79 | static unsigned __stdcall ThreadCallback(void *param) { |
| 80 | struct ThreadInfo *info = reinterpret_cast<struct ThreadInfo *>(param); |
| 81 | info->func(info->param); |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData, |
| 87 | unsigned RequestedStackSize) { |
| 88 | struct ThreadInfo param = { Fn, UserData }; |
| 89 | |
| 90 | HANDLE hThread = (HANDLE)::_beginthreadex(NULL, |
| 91 | RequestedStackSize, ThreadCallback, |
| 92 | ¶m, 0, NULL); |
| 93 | |
| 94 | if (hThread) { |
| 95 | // We actually don't care whether the wait succeeds or fails, in |
| 96 | // the same way we don't care whether the pthread_join call succeeds |
| 97 | // or fails. There's not much we could do if this were to fail. But |
| 98 | // on success, this call will wait until the thread finishes executing |
| 99 | // before returning. |
| 100 | (void)::WaitForSingleObject(hThread, INFINITE); |
| 101 | ::CloseHandle(hThread); |
| 102 | } |
| 103 | } |
| Daniel Dunbar | ea8e206 | 2010-11-04 01:26:25 +0000 | [diff] [blame] | 104 | #else |
| NAKAMURA Takumi | 92ec8e1 | 2011-09-19 07:41:43 +0000 | [diff] [blame] | 105 | // Support for non-Win32, non-pthread implementation. |
| Daniel Dunbar | ea8e206 | 2010-11-04 01:26:25 +0000 | [diff] [blame] | 106 | void llvm::llvm_execute_on_thread(void (*Fn)(void*), void *UserData, |
| 107 | unsigned RequestedStackSize) { |
| 108 | (void) RequestedStackSize; |
| 109 | Fn(UserData); |
| 110 | } |
| 111 | |
| 112 | #endif |