1 /** 2 * Global State Management 3 * 4 * Copyright: 5 * (C) 2010 Jack Lloyd 6 * (C) 2014-2015 Etienne Cimon 7 * 8 * License: 9 * Botan is released under the Simplified BSD License (see LICENSE.md) 10 */ 11 module botan.libstate.global_state; 12 import botan.constants; 13 import botan.libstate.libstate; 14 import core.thread : Thread; 15 /// Thread-Local, no locks needed. 16 private LibraryState g_lib_state; 17 18 static ~this() { 19 if (g_lib_state) 20 g_lib_state.destroy(); 21 22 if (Thread.getThis() != gs_ctor) return; 23 if (gs_global_prng) 24 gs_global_prng.destroy(); 25 if (gs_sources.length > 0) { 26 gs_sources.clear(); 27 gs_sources.destroy(); 28 } 29 } 30 31 /** 32 * Access the global library state 33 * Returns: reference to the global library state 34 */ 35 LibraryState globalState() 36 { 37 if (!g_lib_state) { 38 39 g_lib_state = new LibraryState; 40 /* Lazy initialization. Botan still needs to be deinitialized later 41 on or memory might leak. 42 */ 43 g_lib_state.initialize(); 44 } 45 return g_lib_state; 46 } 47 48 /** 49 * Set the global state object 50 * Params: 51 * new_state = the new global state to use 52 */ 53 void setGlobalState(LibraryState new_state) 54 { 55 if (g_lib_state) destroy(g_lib_state); 56 g_lib_state = new_state; 57 } 58 59 60 /** 61 * Set the global state object unless it is already set 62 * Params: 63 * new_state = the new global state to use 64 * Returns: true if the state parameter is now being used as the global 65 * state, or false if one was already set, in which case the 66 * parameter was deleted immediately 67 */ 68 bool setGlobalStateUnlessSet(LibraryState new_state) 69 { 70 if (g_lib_state) 71 { 72 return false; 73 } 74 else 75 { 76 g_lib_state = new_state; 77 return true; 78 } 79 } 80 81 /** 82 * Query if the library is currently initialized 83 * Returns: true iff the library is initialized 84 */ 85 bool globalStateExists() 86 { 87 return (g_lib_state !is LibraryState.init); 88 }