|
| 1 | +/// @file ThreadCrashHdl.h |
| 2 | +/// @brief Thead handling and Crash Report |
| 3 | +/// @see For thread-local locales see |
| 4 | +/// https://stackoverflow.com/a/17173977 |
| 5 | +/// @see For Crash Reporter see |
| 6 | +/// https://developer.x-plane.com/code-sample/crash-handling/ |
| 7 | +/// @details Sets standard settings for worker threads like locale and |
| 8 | +/// crashh reporting. |
| 9 | +/// Installs our own crash reporter (since X-Plane seems to filter |
| 10 | +/// out crashes in plugins and doesn't write a dump any longer |
| 11 | +/// in such cases). |
| 12 | +/// @author Birger Hoppe |
| 13 | +/// @copyright (c) 2024 Birger Hoppe |
| 14 | +/// @copyright Permission is hereby granted, free of charge, to any person obtaining a |
| 15 | +/// copy of this software and associated documentation files (the "Software"), |
| 16 | +/// to deal in the Software without restriction, including without limitation |
| 17 | +/// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 18 | +/// and/or sell copies of the Software, and to permit persons to whom the |
| 19 | +/// Software is furnished to do so, subject to the following conditions:\n |
| 20 | +/// The above copyright notice and this permission notice shall be included in |
| 21 | +/// all copies or substantial portions of the Software.\n |
| 22 | +/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 23 | +/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 24 | +/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 25 | +/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 26 | +/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 27 | +/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 28 | +/// THE SOFTWARE. |
| 29 | + |
| 30 | +#ifndef ThreadCrashHdl_h |
| 31 | +#define ThreadCrashHdl_h |
| 32 | + |
| 33 | +// |
| 34 | +// MARK: Crash Handler |
| 35 | +// |
| 36 | + |
| 37 | +/// @brief Registers the global crash handler |
| 38 | +/// @details Should be called from XPluginStart() |
| 39 | +void CrashHandlerRegister(); |
| 40 | +/// @brief Unregisters the global crash handler |
| 41 | +/// @details You need to call this in XPluginStop() so we can clean up after ourselves |
| 42 | +void CrashHandlerUnregister(); |
| 43 | + |
| 44 | +/// @brief Registers the calling thread with the crash handler |
| 45 | +/// @details We use this to figure out if a crashed thread belongs to us |
| 46 | +/// when we later try to figure out if we caused a crash |
| 47 | +void CrashHandlerRegisterThread (const char* sThrName); |
| 48 | +/// @brief Unregisters the calling thread from the crash handler |
| 49 | +/// @details MUST be called at the end of thread that was registered |
| 50 | +/// via CrashHandlerRegister() |
| 51 | +void CrashHandlerUnregisterThread(); |
| 52 | + |
| 53 | + |
| 54 | + |
| 55 | +// |
| 56 | +// MARK: Thread Settings |
| 57 | +// |
| 58 | + |
| 59 | +/// Begin a thread and set a thread-local locale |
| 60 | +/// @details In the communication with servers we must use internal standards, |
| 61 | +/// ie. C locale, so that for example the decimal point is `.` |
| 62 | +/// Hence we set a thread-local locale in all threads as they deal with communication. |
| 63 | +/// See https://stackoverflow.com/a/17173977 |
| 64 | +class ThreadSettings { |
| 65 | +protected: |
| 66 | +#if IBM |
| 67 | +#define LC_ALL_MASK LC_ALL |
| 68 | +#else |
| 69 | + locale_t threadLocale = locale_t(0); |
| 70 | + locale_t prevLocale = locale_t(0); |
| 71 | +#endif |
| 72 | +public: |
| 73 | + /// @brief Defines thread's name and sets the thread's locale |
| 74 | + /// @param sThreadName Thread's name, max 16 chars |
| 75 | + /// @param localeMask One of the LC_*_MASK constants. If `0` then locale is not changed. |
| 76 | + /// @param sLocaleName New locale to set |
| 77 | + ThreadSettings (const char* sThreadName, |
| 78 | + int localeMask = 0, |
| 79 | + const char* sLocaleName = "C"); |
| 80 | + /// Restores and cleans up locale |
| 81 | + ~ThreadSettings(); |
| 82 | +}; |
| 83 | + |
| 84 | +#endif /* ThreadCrashHdl_h */ |
0 commit comments