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" |
Zachary Turner | d119fa0 | 2014-06-21 00:24:51 +0000 | [diff] [blame] | 16 | #include "llvm/Support/Mutex.h" |
| 17 | #include "llvm/Support/MutexGuard.h" |
Chandler Carruth | dd14638 | 2016-06-02 18:22:12 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Threading.h" |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 19 | #include <cassert> |
| 20 | using namespace llvm; |
| 21 | |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 22 | static const ManagedStaticBase *StaticList = nullptr; |
Chandler Carruth | dd14638 | 2016-06-02 18:22:12 +0000 | [diff] [blame] | 23 | static sys::Mutex *ManagedStaticMutex = nullptr; |
Kamil Rytarowski | 5d2bd8d | 2017-02-05 21:13:06 +0000 | [diff] [blame] | 24 | static llvm::once_flag mutex_init_flag; |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 25 | |
Chandler Carruth | dd14638 | 2016-06-02 18:22:12 +0000 | [diff] [blame] | 26 | static void initializeMutex() { |
| 27 | ManagedStaticMutex = new sys::Mutex(); |
| 28 | } |
| 29 | |
| 30 | static sys::Mutex* getManagedStaticMutex() { |
Chandler Carruth | fe1ffb9 | 2016-06-04 19:57:55 +0000 | [diff] [blame] | 31 | llvm::call_once(mutex_init_flag, initializeMutex); |
Zachary Turner | d119fa0 | 2014-06-21 00:24:51 +0000 | [diff] [blame] | 32 | return ManagedStaticMutex; |
Zachary Turner | 6ad2444 | 2014-06-19 16:17:42 +0000 | [diff] [blame] | 33 | } |
| 34 | |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 35 | void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(), |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 36 | void (*Deleter)(void*)) const { |
David Blaikie | 5b01593 | 2014-04-17 20:30:35 +0000 | [diff] [blame] | 37 | assert(Creator); |
Owen Anderson | 4cb4b61 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 38 | if (llvm_is_multithreaded()) { |
Chandler Carruth | dd14638 | 2016-06-02 18:22:12 +0000 | [diff] [blame] | 39 | MutexGuard Lock(*getManagedStaticMutex()); |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 40 | |
Benjamin Kramer | 832d042 | 2016-06-29 15:04:07 +0000 | [diff] [blame] | 41 | if (!Ptr.load(std::memory_order_relaxed)) { |
| 42 | void *Tmp = Creator(); |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 43 | |
Benjamin Kramer | 832d042 | 2016-06-29 15:04:07 +0000 | [diff] [blame] | 44 | Ptr.store(Tmp, std::memory_order_release); |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 45 | DeleterFn = Deleter; |
| 46 | |
| 47 | // Add to list of managed statics. |
| 48 | Next = StaticList; |
| 49 | StaticList = this; |
| 50 | } |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 51 | } else { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 52 | assert(!Ptr && !DeleterFn && !Next && |
Bill Wendling | 09f17a8 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 53 | "Partially initialized ManagedStatic!?"); |
David Blaikie | 5b01593 | 2014-04-17 20:30:35 +0000 | [diff] [blame] | 54 | Ptr = Creator(); |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 55 | DeleterFn = Deleter; |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 56 | |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 57 | // Add to list of managed statics. |
| 58 | Next = StaticList; |
| 59 | StaticList = this; |
| 60 | } |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | void ManagedStaticBase::destroy() const { |
Chris Lattner | 4510c99 | 2007-02-20 06:18:57 +0000 | [diff] [blame] | 64 | assert(DeleterFn && "ManagedStatic not initialized correctly!"); |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 65 | assert(StaticList == this && |
| 66 | "Not destroyed in reverse order of construction?"); |
| 67 | // Unlink from list. |
| 68 | StaticList = Next; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 69 | Next = nullptr; |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 70 | |
| 71 | // Destroy memory. |
| 72 | DeleterFn(Ptr); |
| 73 | |
| 74 | // Cleanup. |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 75 | Ptr = nullptr; |
| 76 | DeleterFn = nullptr; |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables. |
Chris Lattner | adf40953 | 2006-09-29 18:43:14 +0000 | [diff] [blame] | 80 | void llvm::llvm_shutdown() { |
Chandler Carruth | dd14638 | 2016-06-02 18:22:12 +0000 | [diff] [blame] | 81 | MutexGuard Lock(*getManagedStaticMutex()); |
Zachary Turner | 6ad2444 | 2014-06-19 16:17:42 +0000 | [diff] [blame] | 82 | |
NAKAMURA Takumi | 256d37a | 2014-10-14 15:58:16 +0000 | [diff] [blame] | 83 | while (StaticList) |
| 84 | StaticList->destroy(); |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 85 | } |