Owen Anderson | e3cd5ca | 2009-06-18 16:54:52 +0000 | [diff] [blame] | 1 | //===-- llvm/System/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 | // |
| 10 | // This file implements llvm_start_multithreaded() and friends. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Owen Anderson | 3d7622d | 2009-07-06 18:50:47 +0000 | [diff] [blame^] | 14 | #include "llvm/Config/config.h" |
Owen Anderson | e3cd5ca | 2009-06-18 16:54:52 +0000 | [diff] [blame] | 15 | #include "llvm/System/Threading.h" |
Owen Anderson | 4c7ac18 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 16 | #include "llvm/System/Atomic.h" |
| 17 | #include "llvm/System/Mutex.h" |
Chris Lattner | c880815 | 2009-07-06 17:24:48 +0000 | [diff] [blame] | 18 | #include "llvm/Config/config.h" |
Owen Anderson | 4c7ac18 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 19 | #include <cassert> |
Owen Anderson | 4c7ac18 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 20 | using namespace llvm; |
| 21 | |
| 22 | static bool multithreaded_mode = false; |
| 23 | |
| 24 | static sys::Mutex* global_lock = 0; |
| 25 | |
| 26 | bool llvm::llvm_start_multithreaded() { |
| 27 | #ifdef LLVM_MULTITHREADED |
| 28 | assert(!multithreaded_mode && "Already multithreaded!"); |
| 29 | multithreaded_mode = true; |
| 30 | global_lock = new sys::Mutex(true); |
| 31 | |
| 32 | // We fence here to ensure that all initialization is complete BEFORE we |
| 33 | // return from llvm_start_multithreaded(). |
| 34 | sys::MemoryFence(); |
| 35 | return true; |
| 36 | #else |
| 37 | return false; |
| 38 | #endif |
| 39 | } |
| 40 | |
| 41 | void llvm::llvm_stop_multithreaded() { |
| 42 | #ifdef LLVM_MULTITHREADED |
| 43 | assert(multithreaded_mode && "Not currently multithreaded!"); |
| 44 | |
| 45 | // We fence here to insure that all threaded operations are complete BEFORE we |
| 46 | // return from llvm_stop_multithreaded(). |
| 47 | sys::MemoryFence(); |
| 48 | |
| 49 | multithreaded_mode = false; |
| 50 | delete global_lock; |
| 51 | #endif |
| 52 | } |
| 53 | |
| 54 | bool llvm::llvm_is_multithreaded() { |
| 55 | return multithreaded_mode; |
| 56 | } |
| 57 | |
| 58 | void llvm::llvm_acquire_global_lock() { |
| 59 | if (multithreaded_mode) global_lock->acquire(); |
| 60 | } |
| 61 | |
| 62 | void llvm::llvm_release_global_lock() { |
| 63 | if (multithreaded_mode) global_lock->release(); |
Owen Anderson | 1cca27e | 2009-06-16 20:53:09 +0000 | [diff] [blame] | 64 | } |