Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 1 | //===-- ManagedStatic.cpp - Static Global wrapper -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements the ManagedStatic class and llvm_shutdown(). |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #include "llvm/Support/ManagedStatic.h" |
Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 15 | #include "llvm/Config/config.h" |
| 16 | #include "llvm/System/Atomic.h" |
| 17 | #include "llvm/System/Mutex.h" |
Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 18 | #include <cassert> |
| 19 | using namespace llvm; |
| 20 | |
| 21 | static const ManagedStaticBase *StaticList = 0; |
| 22 | |
Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 23 | static sys::Mutex* ManagedStaticMutex = 0; |
| 24 | |
| 25 | void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(), |
Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 26 | void (*Deleter)(void*)) const { |
Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 27 | if (ManagedStaticMutex) { |
| 28 | ManagedStaticMutex->acquire(); |
| 29 | |
| 30 | if (Ptr == 0) { |
| 31 | void* tmp = Creator ? Creator() : 0; |
| 32 | |
| 33 | sys::MemoryFence(); |
| 34 | Ptr = tmp; |
| 35 | DeleterFn = Deleter; |
| 36 | |
| 37 | // Add to list of managed statics. |
| 38 | Next = StaticList; |
| 39 | StaticList = this; |
| 40 | } |
| 41 | |
| 42 | ManagedStaticMutex->release(); |
| 43 | } else { |
| 44 | assert(Ptr == 0 && DeleterFn == 0 && Next == 0 && |
Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 45 | "Partially initialized ManagedStatic!?"); |
Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 46 | Ptr = Creator ? Creator() : 0; |
| 47 | DeleterFn = Deleter; |
Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 48 | |
Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 49 | // Add to list of managed statics. |
| 50 | Next = StaticList; |
| 51 | StaticList = this; |
| 52 | } |
Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 53 | } |
| 54 | |
| 55 | void ManagedStaticBase::destroy() const { |
Chris Lattner | d283566 | 2007-02-20 06:18:57 +0000 | [diff] [blame] | 56 | assert(DeleterFn && "ManagedStatic not initialized correctly!"); |
Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 57 | assert(StaticList == this && |
| 58 | "Not destroyed in reverse order of construction?"); |
| 59 | // Unlink from list. |
| 60 | StaticList = Next; |
| 61 | Next = 0; |
| 62 | |
| 63 | // Destroy memory. |
| 64 | DeleterFn(Ptr); |
| 65 | |
| 66 | // Cleanup. |
| 67 | Ptr = 0; |
| 68 | DeleterFn = 0; |
| 69 | } |
| 70 | |
Owen Anderson | 6afe2fa | 2009-05-20 21:03:06 +0000 | [diff] [blame] | 71 | bool llvm::llvm_start_multithreaded() { |
Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 72 | #if LLVM_MULTITHREADED |
| 73 | assert(ManagedStaticMutex == 0 && "Multithreaded LLVM already initialized!"); |
| 74 | ManagedStaticMutex = new sys::Mutex(true); |
Owen Anderson | 6afe2fa | 2009-05-20 21:03:06 +0000 | [diff] [blame] | 75 | return true; |
Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 76 | #else |
Owen Anderson | 6afe2fa | 2009-05-20 21:03:06 +0000 | [diff] [blame] | 77 | return false; |
Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 78 | #endif |
| 79 | } |
| 80 | |
Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 81 | /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables. |
Chris Lattner | 151880b | 2006-09-29 18:43:14 +0000 | [diff] [blame] | 82 | void llvm::llvm_shutdown() { |
Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 83 | while (StaticList) |
| 84 | StaticList->destroy(); |
Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 85 | |
| 86 | if (ManagedStaticMutex) { |
| 87 | delete ManagedStaticMutex; |
| 88 | ManagedStaticMutex = 0; |
| 89 | } |
Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 90 | } |
| 91 | |