Spicy
global-state.h
1 // Copyright (c) 2020-2021 by the Zeek Project. See LICENSE for details.
2 
3 #pragma once
4 #include <sys/resource.h>
5 
6 #include <memory>
7 #include <optional>
8 #include <vector>
9 
10 #include <hilti/rt/context.h>
11 #include <hilti/rt/debug-logger.h>
12 #include <hilti/rt/init.h>
13 
14 // We collect all (or most) of the runtime's global state centrally. That's
15 // 1st good to see what we have (global state should be minimal) and 2nd
16 // helpful to ensure that JIT maps things correctly. Note that all code
17 // accessing any of this state is in charge of ensuring thread-safety itself.
18 // These globals are generally initialized through hilti::rt::init();
19 //
20 // TODO(robin): Accesses to global state are *not* completely thread-safe yet.
21 
22 namespace hilti::rt {
23 struct Configuration;
24 
25 namespace detail {
26 class DebugLogger;
27 }
28 
29 } // namespace hilti::rt
30 
31 namespace hilti::rt::detail {
32 
34 struct GlobalState {
35  GlobalState() = default;
36  ~GlobalState();
37 
38  GlobalState(const GlobalState&) = delete;
39  GlobalState(GlobalState&&) = delete;
40  GlobalState& operator=(const GlobalState&) = delete;
41  GlobalState& operator=(GlobalState&&) = delete;
42 
44  bool runtime_is_initialized = false;
45 
48 
51 
53  std::unique_ptr<hilti::rt::Configuration> configuration;
54 
56  std::unique_ptr<hilti::rt::detail::DebugLogger> debug_logger;
57 
59  std::unique_ptr<hilti::rt::Context> master_context;
60 
68  std::vector<hilti::rt::detail::HiltiModule> hilti_modules;
69 };
70 
75 extern GlobalState* __global_state;
76 
78 extern GlobalState* createGlobalState();
79 
84 inline auto globalState() {
85  if ( __global_state )
86  return __global_state;
87 
88  return createGlobalState();
89 }
90 
92 inline auto hiltiGlobals() {
93  assert(context::detail::current());
94  return context::detail::current()->hilti_globals;
95 }
96 
103 template<typename T>
104 inline auto moduleGlobals(unsigned int idx) {
105  const auto& globals = hiltiGlobals();
106 
107  assert(idx < globals.size());
108 
109  return std::static_pointer_cast<T>(globals[idx]);
110 }
111 
119 template<typename T>
120 inline auto initModuleGlobals(unsigned int idx) {
121  if ( context::detail::current()->hilti_globals.size() <= idx )
122  context::detail::current()->hilti_globals.resize(idx + 1);
123 
124  context::detail::current()->hilti_globals[idx] = std::make_shared<T>();
125 }
126 
127 } // namespace hilti::rt::detail
int disable_abort_on_exceptions
Definition: global-state.h:47
std::vector< hilti::rt::detail::HiltiModule > hilti_modules
Definition: global-state.h:68
bool runtime_is_initialized
Definition: global-state.h:44
Definition: debug-logger.h:14
Definition: any.h:7
ResourceUsage resource_usage_init
Definition: global-state.h:50
std::unique_ptr< hilti::rt::Configuration > configuration
Definition: global-state.h:53
Definition: util.h:89
std::unique_ptr< hilti::rt::Context > master_context
Definition: global-state.h:59
std::unique_ptr< hilti::rt::detail::DebugLogger > debug_logger
Definition: global-state.h:56
Definition: global-state.h:34