Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 1 | //===-- ManagedStatic.cpp - Static Global wrapper -------------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file implements the ManagedStatic class and llvm_shutdown(). |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/Support/ManagedStatic.h" |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 14 | #include "llvm/Config/config.h" |
Chandler Carruth | dd14638 | 2016-06-02 18:22:12 +0000 | [diff] [blame] | 15 | #include "llvm/Support/Threading.h" |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 16 | #include <cassert> |
Benjamin Kramer | 3d5360a | 2019-08-07 10:57:25 +0000 | [diff] [blame] | 17 | #include <mutex> |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 18 | using namespace llvm; |
| 19 | |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 20 | static const ManagedStaticBase *StaticList = nullptr; |
Benjamin Kramer | 928071a | 2019-08-19 19:49:57 +0000 | [diff] [blame] | 21 | static std::recursive_mutex *ManagedStaticMutex = nullptr; |
Kamil Rytarowski | 5d2bd8d | 2017-02-05 21:13:06 +0000 | [diff] [blame] | 22 | static llvm::once_flag mutex_init_flag; |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 23 | |
Chandler Carruth | dd14638 | 2016-06-02 18:22:12 +0000 | [diff] [blame] | 24 | static void initializeMutex() { |
Benjamin Kramer | 928071a | 2019-08-19 19:49:57 +0000 | [diff] [blame] | 25 | ManagedStaticMutex = new std::recursive_mutex(); |
Chandler Carruth | dd14638 | 2016-06-02 18:22:12 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Benjamin Kramer | 928071a | 2019-08-19 19:49:57 +0000 | [diff] [blame] | 28 | static std::recursive_mutex *getManagedStaticMutex() { |
Chandler Carruth | fe1ffb9 | 2016-06-04 19:57:55 +0000 | [diff] [blame] | 29 | llvm::call_once(mutex_init_flag, initializeMutex); |
Zachary Turner | d119fa0 | 2014-06-21 00:24:51 +0000 | [diff] [blame] | 30 | return ManagedStaticMutex; |
Zachary Turner | 6ad2444 | 2014-06-19 16:17:42 +0000 | [diff] [blame] | 31 | } |
| 32 | |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 33 | void ManagedStaticBase::RegisterManagedStatic(void *(*Creator)(), |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 34 | void (*Deleter)(void*)) const { |
David Blaikie | 5b01593 | 2014-04-17 20:30:35 +0000 | [diff] [blame] | 35 | assert(Creator); |
Owen Anderson | 4cb4b61 | 2009-06-16 17:33:51 +0000 | [diff] [blame] | 36 | if (llvm_is_multithreaded()) { |
Benjamin Kramer | 928071a | 2019-08-19 19:49:57 +0000 | [diff] [blame] | 37 | std::lock_guard<std::recursive_mutex> Lock(*getManagedStaticMutex()); |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 38 | |
Benjamin Kramer | 832d042 | 2016-06-29 15:04:07 +0000 | [diff] [blame] | 39 | if (!Ptr.load(std::memory_order_relaxed)) { |
| 40 | void *Tmp = Creator(); |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 41 | |
Benjamin Kramer | 832d042 | 2016-06-29 15:04:07 +0000 | [diff] [blame] | 42 | Ptr.store(Tmp, std::memory_order_release); |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 43 | DeleterFn = Deleter; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 44 | |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 45 | // Add to list of managed statics. |
| 46 | Next = StaticList; |
| 47 | StaticList = this; |
| 48 | } |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 49 | } else { |
Craig Topper | 2617dcc | 2014-04-15 06:32:26 +0000 | [diff] [blame] | 50 | assert(!Ptr && !DeleterFn && !Next && |
Bill Wendling | 09f17a8 | 2009-05-30 01:09:53 +0000 | [diff] [blame] | 51 | "Partially initialized ManagedStatic!?"); |
David Blaikie | 5b01593 | 2014-04-17 20:30:35 +0000 | [diff] [blame] | 52 | Ptr = Creator(); |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 53 | DeleterFn = Deleter; |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 54 | |
Owen Anderson | b9a4a57 | 2009-05-20 00:39:20 +0000 | [diff] [blame] | 55 | // Add to list of managed statics. |
| 56 | Next = StaticList; |
| 57 | StaticList = this; |
| 58 | } |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | void ManagedStaticBase::destroy() const { |
Chris Lattner | 4510c99 | 2007-02-20 06:18:57 +0000 | [diff] [blame] | 62 | assert(DeleterFn && "ManagedStatic not initialized correctly!"); |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 63 | assert(StaticList == this && |
| 64 | "Not destroyed in reverse order of construction?"); |
| 65 | // Unlink from list. |
| 66 | StaticList = Next; |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 67 | Next = nullptr; |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 68 | |
| 69 | // Destroy memory. |
| 70 | DeleterFn(Ptr); |
Fangrui Song | f78650a | 2018-07-30 19:41:25 +0000 | [diff] [blame] | 71 | |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 72 | // Cleanup. |
Craig Topper | c10719f | 2014-04-07 04:17:22 +0000 | [diff] [blame] | 73 | Ptr = nullptr; |
| 74 | DeleterFn = nullptr; |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | /// llvm_shutdown - Deallocate and destroy all ManagedStatic variables. |
Chris Lattner | adf40953 | 2006-09-29 18:43:14 +0000 | [diff] [blame] | 78 | void llvm::llvm_shutdown() { |
Benjamin Kramer | 928071a | 2019-08-19 19:49:57 +0000 | [diff] [blame] | 79 | std::lock_guard<std::recursive_mutex> Lock(*getManagedStaticMutex()); |
Zachary Turner | 6ad2444 | 2014-06-19 16:17:42 +0000 | [diff] [blame] | 80 | |
NAKAMURA Takumi | 256d37a | 2014-10-14 15:58:16 +0000 | [diff] [blame] | 81 | while (StaticList) |
| 82 | StaticList->destroy(); |
Chris Lattner | 9daff49 | 2006-09-28 00:31:55 +0000 | [diff] [blame] | 83 | } |