| 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" |
| Michael J. Spencer | 1f6efa3 | 2010-11-29 18:16:10 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Atomic.h" |
| Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 17 | #include "llvm/Support/Mutex.h" |
| 18 | #include "llvm/Support/MutexGuard.h" |
| Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 19 | #include <cassert> |
| 20 | using namespace llvm; |
| 21 | |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 22 | static const ManagedStaticBase *StaticList = nullptr; |
| Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 23 | |
| Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 24 | static sys::Mutex& getManagedStaticMutex() { |
| 25 | // We need to use a function local static here, since this can get called |
| 26 | // during a static constructor and we need to guarantee that it's initialized |
| 27 | // correctly. |
| 28 | static sys::Mutex ManagedStaticMutex; |
| 29 | return ManagedStaticMutex; |
| 30 | } |
| 31 | |
| Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 32 | void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(), |
| Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 33 | void (*Deleter)(void*)) const { |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 34 | assert(Creator); |
| Owen Anderson | 4c7ac18 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 35 | if (llvm_is_multithreaded()) { |
| Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 36 | MutexGuard Lock(getManagedStaticMutex()); |
| Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 37 | |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 38 | if (!Ptr) { |
| 39 | void* tmp = Creator(); |
| Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 40 | |
| Nick Lewycky | 4d0a9ff | 2011-11-14 20:50:16 +0000 | [diff] [blame] | 41 | TsanHappensBefore(this); |
| Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 42 | sys::MemoryFence(); |
| Nick Lewycky | 4d0a9ff | 2011-11-14 20:50:16 +0000 | [diff] [blame] | 43 | |
| 44 | // This write is racy against the first read in the ManagedStatic |
| 45 | // accessors. The race is benign because it does a second read after a |
| 46 | // memory fence, at which point it isn't possible to get a partial value. |
| 47 | TsanIgnoreWritesBegin(); |
| Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 48 | Ptr = tmp; |
| Nick Lewycky | 4d0a9ff | 2011-11-14 20:50:16 +0000 | [diff] [blame] | 49 | TsanIgnoreWritesEnd(); |
| Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 50 | DeleterFn = Deleter; |
| 51 | |
| 52 | // Add to list of managed statics. |
| 53 | Next = StaticList; |
| 54 | StaticList = this; |
| 55 | } |
| Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 56 | } else { |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 57 | assert(!Ptr && !DeleterFn && !Next && |
| Bill Wendling | 51b16f4 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 58 | "Partially initialized ManagedStatic!?"); |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 59 | Ptr = Creator(); |
| Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 60 | DeleterFn = Deleter; |
| Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 61 | |
| Owen Anderson | b4d97b7 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 62 | // Add to list of managed statics. |
| 63 | Next = StaticList; |
| 64 | StaticList = this; |
| 65 | } |
| Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | void ManagedStaticBase::destroy() const { |
| Chris Lattner | d283566 | 2007-02-20 06:18:57 +0000 | [diff] [blame] | 69 | assert(DeleterFn && "ManagedStatic not initialized correctly!"); |
| Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 70 | assert(StaticList == this && |
| 71 | "Not destroyed in reverse order of construction?"); |
| 72 | // Unlink from list. |
| 73 | StaticList = Next; |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 74 | Next = nullptr; |
| Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 75 | |
| 76 | // Destroy memory. |
| 77 | DeleterFn(Ptr); |
| 78 | |
| 79 | // Cleanup. |
| Stephen Hines | dce4a40 | 2014-05-29 02:49:00 -0700 | [diff] [blame] | 80 | Ptr = nullptr; |
| 81 | DeleterFn = nullptr; |
| Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables. |
| Chris Lattner | 151880b | 2006-09-29 18:43:14 +0000 | [diff] [blame] | 85 | void llvm::llvm_shutdown() { |
| Stephen Hines | c6a4f5e | 2014-07-21 00:45:20 -0700 | [diff] [blame] | 86 | MutexGuard Lock(getManagedStaticMutex()); |
| 87 | |
| Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 88 | while (StaticList) |
| 89 | StaticList->destroy(); |
| Chris Lattner | 771cbf3 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 90 | } |