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/System/Atomic.h"
16 #include "llvm/System/Mutex.h"
21 static bool multithreaded_mode = false;
23 static sys::Mutex* global_lock = 0;
25 bool llvm::llvm_start_multithreaded() {
26 #ifdef LLVM_MULTITHREADED
27 assert(!multithreaded_mode && "Already multithreaded!");
28 multithreaded_mode = true;
29 global_lock = new sys::Mutex(true);
31 // We fence here to ensure that all initialization is complete BEFORE we
32 // return from llvm_start_multithreaded().
40 void llvm::llvm_stop_multithreaded() {
41 #ifdef LLVM_MULTITHREADED
42 assert(multithreaded_mode && "Not currently multithreaded!");
44 // We fence here to insure that all threaded operations are complete BEFORE we
45 // return from llvm_stop_multithreaded().
48 multithreaded_mode = false;
53 bool llvm::llvm_is_multithreaded() {
54 return multithreaded_mode;
57 void llvm::llvm_acquire_global_lock() {
58 if (multithreaded_mode) global_lock->acquire();
61 void llvm::llvm_release_global_lock() {
62 if (multithreaded_mode) global_lock->release();