HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
initialize.hpp
1// Copyright Take Vos 2023.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7#include "terminate.hpp"
8#include "console_win32.hpp"
9#include "debugger_intf.hpp"
10#include <atomic>
11#include <chrono>
12#include <thread>
13#include <cstdint>
14#include <cstddef>
15
20hi_export_module(hikogui.utility.initialize);
21
22hi_export namespace hi {
23inline namespace v1 {
24namespace detail {
25
26enum class initialize_state_type {
27 uninitialized,
28 running,
29 finished,
30};
31
32inline thread_local uint16_t initialize_thread_id_dummy;
33
37[[nodiscard]] inline uintptr_t initialize_thread_id() noexcept
38{
39 // The following is guaranteed:
40 // - 0: An address can never be a nullptr.
41 // - 1: initialize_thread_id_dummy is a uint16_t which is aligned to a multiple of two.
42 return std::bit_cast<uintptr_t>(std::addressof(initialize_thread_id_dummy));
43}
44
45inline std::atomic<uintptr_t> initialize_state = 0;
46
47}
48
56inline void initialize() noexcept
57{
58 using namespace std::literals;
59
60 uintptr_t expected = 0;
61 if (detail::initialize_state.compare_exchange_strong(expected, detail::initialize_thread_id(), std::memory_order::acquire)) {
62 // Make sure stdin, stdout, stderr are attached to a console and that
63 // std::print() works properly.
65
66 // Install the terminate handle to make pretty error messages for
67 // end users.
69
70 // Install the handler for break-points and other traps.
71 // Which will optionally start the just-in-time debugger, or call
72 // std::terminate() with an appropriate error.
74
75 // Mark initialization as "finished".
76 detail::initialize_state.store(1, std::memory_order::release);
77
78 } else if (expected == detail::initialize_thread_id()) {
79 set_debug_message("hi::initialize() re-enterred from same thread.");
81
82 } else {
83 // We can not use std::mutex before main().
84 // Wait until initialization on the other thread is finished.
85 while (detail::initialize_state.load(std::memory_order::acquire) != 1) {
87 }
88 }
89}
90
91} // namespace v1
92}
Utilities for throwing exceptions and terminating the application.
The HikoGUI namespace.
Definition array_generic.hpp:20
std::terminate_handler old_terminate_handler
The old terminate handler.
Definition terminate.hpp:58
void enable_debugger() noexcept
Enable the JIT debugger to be attached.
Definition debugger_generic_impl.hpp:17
void start_console() noexcept
Start the console.
Definition console_win32.hpp:21
void initialize() noexcept
Initialize base functionality of HikoGUI.
Definition initialize.hpp:56
constexpr Out load(In const *src) noexcept
Unaligned Load of a numeric value from an array.
Definition endian.hpp:80
void terminate_handler() noexcept
The HikoGUI terminate handler.
Definition terminate.hpp:66
DOXYGEN BUG.
Definition algorithm_misc.hpp:20
T addressof(T... args)
T set_terminate(T... args)
T sleep_for(T... args)
T terminate(T... args)