MT Core (C++)
Core library for replacing C++ standard in project usage
Loading...
Searching...
No Matches
thread/mtcore_thread/core.hpp
Go to the documentation of this file.
1/*
2
3Copyright 2025 Matthew Tolman
4
5Licensed under the Apache License, Version 2.0 (the "License");
6you may not use this file except in compliance with the License.
7You may obtain a copy of the License at
8
9 http://www.apache.org/licenses/LICENSE-2.0
10
11Unless required by applicable law or agreed to in writing, software
12distributed under the License is distributed on an "AS IS" BASIS,
13WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14See the License for the specific language governing permissions and
15limitations under the License.
16
17*/
18
19#ifndef MTCORE_THREAD_CORE_HPP
20#define MTCORE_THREAD_CORE_HPP
21
22#include <mutex>
23
24namespace mtcore::thread::impl {
25 extern std::mutex stackTraceMux;
26}
27
28#define MTCORE_THREAD_IMPL_NAME1(X, Y, S, N, SEP) X##SEP##Y##SEP##S##SEP##N
29#define MTCORE_THREAD_IMPL_NAME(X, Y, S, N) MTCORE_THREAD_IMPL_NAME1(X, Y, S, N, _)
30#define MTCORE_THREAD_SHORT_NAME(X, Y) MTCORE_THREAD_IMPL_NAME1(X, Y, S, N, _)
31
32
33#ifndef MTCORE_NO_STACKTRACE
34// we redefine these to be more thread friendly, otherwise our stack traces get interleaved and it's a mess to untangle
35// them
36#ifdef mt_print_cur_stacktrace
37#undef mt_print_cur_stacktrace
38#endif
39
40#ifdef mt_print_stacktrace
41#undef mt_print_stacktrace
42#endif
43
44#define mt_print_cur_stacktrace(...) \
45 { \
46 auto MTCORE_THREAD_SHORT_NAME(stack_lock, __LINE__) = std::unique_lock{mtcore::thread::impl::stackTraceMux}; \
47 (void) fprintf(stderr, __VA_ARGS__); \
48 (void) fflush(stderr); \
49 cpptrace::stacktrace::current(1).print(std::cerr); \
50 }
51
52#define mt_print_stacktrace(TRACE, ...) \
53 { \
54 auto MTCORE_THREAD_SHORT_NAME(stack_lock, __LINE__) = std::unique_lock{mtcore::thread::impl::stackTraceMux}; \
55 (void) fprintf(stderr, __VA_ARGS__); \
56 (void) fflush(stderr); \
57 (TRACE).print(std::cerr); \
58 }
59
60#include "mtcore.hpp"
61#endif
62
63#endif // MTCORE_THREAD_CORE_HPP