LLVM API Documentation

Threading.cpp

Go to the documentation of this file.
00001 //===-- llvm/System/Threading.cpp- Control multithreading mode --*- C++ -*-==//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file implements llvm_start_multithreaded() and friends.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "llvm/System/Threading.h"
00015 #include "llvm/System/Atomic.h"
00016 #include "llvm/System/Mutex.h"
00017 #include "llvm/Config/config.h"
00018 #include <cassert>
00019 
00020 using namespace llvm;
00021 
00022 static bool multithreaded_mode = false;
00023 
00024 static sys::Mutex* global_lock = 0;
00025 
00026 bool llvm::llvm_start_multithreaded() {
00027 #ifdef LLVM_MULTITHREADED
00028   assert(!multithreaded_mode && "Already multithreaded!");
00029   multithreaded_mode = true;
00030   global_lock = new sys::Mutex(true);
00031   
00032   // We fence here to ensure that all initialization is complete BEFORE we
00033   // return from llvm_start_multithreaded().
00034   sys::MemoryFence();
00035   return true;
00036 #else
00037   return false;
00038 #endif
00039 }
00040 
00041 void llvm::llvm_stop_multithreaded() {
00042 #ifdef LLVM_MULTITHREADED
00043   assert(multithreaded_mode && "Not currently multithreaded!");
00044   
00045   // We fence here to insure that all threaded operations are complete BEFORE we
00046   // return from llvm_stop_multithreaded().
00047   sys::MemoryFence();
00048   
00049   multithreaded_mode = false;
00050   delete global_lock;
00051 #endif
00052 }
00053 
00054 bool llvm::llvm_is_multithreaded() {
00055   return multithreaded_mode;
00056 }
00057 
00058 void llvm::llvm_acquire_global_lock() {
00059   if (multithreaded_mode) global_lock->acquire();
00060 }
00061 
00062 void llvm::llvm_release_global_lock() {
00063   if (multithreaded_mode) global_lock->release();
00064 }