blob: a2d7f82715d9440eb533b20bd2dd018666c6a7be [file] [log] [blame]
Owen Andersone3cd5ca2009-06-18 16:54:52 +00001//===-- llvm/System/Threading.cpp- Control multithreading mode --*- C++ -*-==//
Owen Anderson4c7ac182009-06-16 17:33:51 +00002//
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 Andersone3cd5ca2009-06-18 16:54:52 +000014#include "llvm/System/Threading.h"
Owen Anderson4c7ac182009-06-16 17:33:51 +000015#include "llvm/System/Atomic.h"
16#include "llvm/System/Mutex.h"
17#include <cassert>
18
19using namespace llvm;
20
21static bool multithreaded_mode = false;
22
23static sys::Mutex* global_lock = 0;
24
25bool 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);
30
31 // We fence here to ensure that all initialization is complete BEFORE we
32 // return from llvm_start_multithreaded().
33 sys::MemoryFence();
34 return true;
35#else
36 return false;
37#endif
38}
39
40void llvm::llvm_stop_multithreaded() {
41#ifdef LLVM_MULTITHREADED
42 assert(multithreaded_mode && "Not currently multithreaded!");
43
44 // We fence here to insure that all threaded operations are complete BEFORE we
45 // return from llvm_stop_multithreaded().
46 sys::MemoryFence();
47
48 multithreaded_mode = false;
49 delete global_lock;
50#endif
51}
52
53bool llvm::llvm_is_multithreaded() {
54 return multithreaded_mode;
55}
56
57void llvm::llvm_acquire_global_lock() {
58 if (multithreaded_mode) global_lock->acquire();
59}
60
61void llvm::llvm_release_global_lock() {
62 if (multithreaded_mode) global_lock->release();
Owen Anderson1cca27e2009-06-16 20:53:09 +000063}