Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 1 | //===-- ManagedStatic.cpp - Static Global wrapper -------------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 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 | 9daff49 | 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 | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 15 | #include "llvm/Config/config.h" |
Benjamin Kramer | 17388a6 | 2014-03-03 18:02:34 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Atomic.h" |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 17 | #include <cassert> |
Zachary Turner | 6ad2444 | 2014-06-19 16:17:42 +0000 | [diff] [blame^] | 18 | #include <mutex> |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 19 | using namespace llvm; |
| 20 | |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 21 | static const ManagedStaticBase *StaticList = nullptr; |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 22 | |
Zachary Turner | 6ad2444 | 2014-06-19 16:17:42 +0000 | [diff] [blame^] | 23 | // ManagedStatics can get created during execution of static constructors. As a |
| 24 | // result, we cannot use a global static std::mutex object for the lock since it |
| 25 | // may not have been constructed. Instead, we do a call-once initialization of |
| 26 | // a pointer to a mutex. |
| 27 | static std::once_flag MutexInitializationFlag; |
| 28 | static std::recursive_mutex* ManagedStaticMutex = nullptr; |
| 29 | |
| 30 | // Not all supported platforms (in particular VS2012) have thread-safe function |
| 31 | // static initialization, so roll our own. |
| 32 | static std::recursive_mutex& GetManagedStaticMutex() { |
| 33 | std::call_once(MutexInitializationFlag, |
| 34 | []() { ManagedStaticMutex = new std::recursive_mutex(); } ); |
| 35 | |
| 36 | return *ManagedStaticMutex; |
| 37 | } |
| 38 | |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 39 | void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(), |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 40 | void (*Deleter)(void*)) const { |
David Blaikie | 5b01593 | 2014-04-17 20:30:35 +0000 | [diff] [blame] | 41 | assert(Creator); |
Owen Anderson | 4cb4b61 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 42 | if (llvm_is_multithreaded()) { |
Zachary Turner | 6ad2444 | 2014-06-19 16:17:42 +0000 | [diff] [blame^] | 43 | std::lock_guard<std::recursive_mutex> Lock(GetManagedStaticMutex()); |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 44 | |
Craig Topper | 8d399f8 | 2014-04-09 04:20:00 +0000 | [diff] [blame] | 45 | if (!Ptr) { |
David Blaikie | 5b01593 | 2014-04-17 20:30:35 +0000 | [diff] [blame] | 46 | void* tmp = Creator(); |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 47 | |
Nick Lewycky | fe85611 | 2011-11-14 20:50:16 +0000 | [diff] [blame] | 48 | TsanHappensBefore(this); |
Benjamin Kramer | 17388a6 | 2014-03-03 18:02:34 +0000 | [diff] [blame] | 49 | sys::MemoryFence(); |
Nick Lewycky | fe85611 | 2011-11-14 20:50:16 +0000 | [diff] [blame] | 50 | |
| 51 | // This write is racy against the first read in the ManagedStatic |
| 52 | // accessors. The race is benign because it does a second read after a |
| 53 | // memory fence, at which point it isn't possible to get a partial value. |
| 54 | TsanIgnoreWritesBegin(); |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 55 | Ptr = tmp; |
Nick Lewycky | fe85611 | 2011-11-14 20:50:16 +0000 | [diff] [blame] | 56 | TsanIgnoreWritesEnd(); |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 57 | DeleterFn = Deleter; |
| 58 | |
| 59 | // Add to list of managed statics. |
| 60 | Next = StaticList; |
| 61 | StaticList = this; |
| 62 | } |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 63 | } else { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 64 | assert(!Ptr && !DeleterFn && !Next && |
Bill Wendling | 09f17a8 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 65 | "Partially initialized ManagedStatic!?"); |
David Blaikie | 5b01593 | 2014-04-17 20:30:35 +0000 | [diff] [blame] | 66 | Ptr = Creator(); |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 67 | DeleterFn = Deleter; |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 68 | |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 69 | // Add to list of managed statics. |
| 70 | Next = StaticList; |
| 71 | StaticList = this; |
| 72 | } |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | void ManagedStaticBase::destroy() const { |
Chris Lattner | 4510c99 | 2007-02-20 06:18:57 +0000 | [diff] [blame] | 76 | assert(DeleterFn && "ManagedStatic not initialized correctly!"); |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 77 | assert(StaticList == this && |
| 78 | "Not destroyed in reverse order of construction?"); |
| 79 | // Unlink from list. |
| 80 | StaticList = Next; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 81 | Next = nullptr; |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 82 | |
| 83 | // Destroy memory. |
| 84 | DeleterFn(Ptr); |
| 85 | |
| 86 | // Cleanup. |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 87 | Ptr = nullptr; |
| 88 | DeleterFn = nullptr; |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 89 | } |
| 90 | |
| 91 | /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables. |
Chris Lattner | adf40953 | 2006-09-29 18:43:14 +0000 | [diff] [blame] | 92 | void llvm::llvm_shutdown() { |
Zachary Turner | 6ad2444 | 2014-06-19 16:17:42 +0000 | [diff] [blame^] | 93 | std::lock_guard<std::recursive_mutex> Lock(GetManagedStaticMutex()); |
| 94 | |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 95 | while (StaticList) |
| 96 | StaticList->destroy(); |
Zachary Turner | ccbf3d0 | 2014-06-16 22:49:41 +0000 | [diff] [blame] | 97 | |
| 98 | if (llvm_is_multithreaded()) llvm_stop_multithreaded(); |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 99 | } |