Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / include / comphelper / crashzone.hxx
blobf15f848cdf9ea31d735118352ca8c1dd868df52d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #ifndef INCLUDED_COMPHELPER_CRASHZONE_H
11 #define INCLUDED_COMPHELPER_CRASHZONE_H
13 #include <sal/config.h>
15 #include <atomic>
16 #include <csignal>
17 #include <type_traits>
19 struct CrashWatchdogTimingsValues
21 /// delays to take various actions in 1/4 of a second increments.
22 int mnDisableEntries;
23 int const mnAbortAfter;
26 /**
27 * A generic class for detecting if a given crash or a lock-up came from a specific
28 * area of code (such as OpenGL).
29 * Use this helper to track that.
30 * The class is a template so that there can be multiple instances of static variables.
32 template <typename Dummy> class CrashZone
34 // gnEnterCount and gnLeaveCount are accessed both from multiple threads (cf.
35 // WatchdogThread::execute; so need to be of atomic type) and from signal handlers (cf.
36 // VCLExceptionSignal_impl; so need to be of lock-free atomic type). sig_atomic_t is chosen as
37 // the underlying type under the assumption that it is most likely to lead to an atomic type
38 // that is actually lock-free. However, gnEnterCount and gnLeaveCount are both monotonically
39 // increasing, so will eventually overflow, so the underlying type better be unsigned, which
40 // sig_atomic_t is not guaranteed to be:
41 #if !defined ARM32 || (defined ARM32 && defined __ARM_PCS_VFP)
42 using AtomicCounter = std::atomic<std::make_unsigned_t<std::sig_atomic_t>>;
43 static_assert(AtomicCounter::is_always_lock_free);
44 #else
45 using AtomicCounter = volatile std::make_unsigned_t<std::sig_atomic_t>;
46 #endif
48 /// how many times have we entered a zone
49 static inline AtomicCounter gnEnterCount = 0;
50 /// how many times have we left a new zone
51 static inline AtomicCounter gnLeaveCount = 0;
53 public:
54 CrashZone() { enter(); }
55 ~CrashZone() { leave(); }
56 static bool isInZone() { return gnEnterCount != gnLeaveCount; }
57 static const AtomicCounter& enterCount() { return gnEnterCount; }
58 // prefer creating instances to manually calling enter()/leave()
59 static void enter() { gnEnterCount++; }
60 static void leave() { gnLeaveCount++; }
61 // these should be implemented for each specific zone if needed
62 // static void hardDisable();
63 // static const CrashWatchdogTimingsValues& getCrashWatchdogTimingsValues();
64 // static void checkDebug(int nUnchanged, const CrashWatchdogTimingsValues& aTimingValues);
65 // static const char* name();
68 #endif // INCLUDED_COMPHELPER_CRASHZONE_H
70 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */