LibreOffice Module comphelper (master)  1
crashzone.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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  */
9 
10 #ifndef INCLUDED_COMPHELPER_CRASHZONE_H
11 #define INCLUDED_COMPHELPER_CRASHZONE_H
12 
13 #include <sal/config.h>
14 #include <sal/types.h>
16 
17 #include <atomic>
18 #include <csignal>
19 #include <type_traits>
20 
22 {
25  int const mnAbortAfter;
26 };
27 
34 template <typename Dummy> class CrashZone
35 {
36 // gnEnterCount and gnLeaveCount are accessed both from multiple threads (cf.
37 // WatchdogThread::execute; so need to be of atomic type) and from signal handlers (cf.
38 // VCLExceptionSignal_impl; so need to be of lock-free atomic type). sig_atomic_t is chosen as
39 // the underlying type under the assumption that it is most likely to lead to an atomic type
40 // that is actually lock-free. However, gnEnterCount and gnLeaveCount are both monotonically
41 // increasing, so will eventually overflow, so the underlying type better be unsigned, which
42 // sig_atomic_t is not guaranteed to be:
43 #if !defined ARM32 || (defined ARM32 && defined __ARM_PCS_VFP)
44  using AtomicCounter = std::atomic<std::make_unsigned_t<std::sig_atomic_t>>;
45  static_assert(AtomicCounter::is_always_lock_free);
46 #else
47  using AtomicCounter = volatile std::make_unsigned_t<std::sig_atomic_t>;
48 #endif
49 
51  static inline AtomicCounter gnEnterCount = 0;
53  static inline AtomicCounter gnLeaveCount = 0;
54 
55 public:
56  CrashZone() { enter(); }
57  ~CrashZone() { leave(); }
58  static bool isInZone() { return gnEnterCount != gnLeaveCount; }
59  static const AtomicCounter& enterCount() { return gnEnterCount; }
60  // prefer creating instances to manually calling enter()/leave()
61  static void enter() { gnEnterCount++; }
62  static void leave() { gnLeaveCount++; }
63  // these should be implemented for each specific zone if needed
64  // static void hardDisable();
65  // static const CrashWatchdogTimingsValues& getCrashWatchdogTimingsValues();
66  // static void checkDebug(int nUnchanged, const CrashWatchdogTimingsValues& aTimingValues);
67  // static const char* name();
68 };
69 
70 #endif // INCLUDED_COMPHELPER_CRASHZONE_H
71 
72 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static const AtomicCounter & enterCount()
Definition: crashzone.hxx:59
A generic class for detecting if a given crash or a lock-up came from a specific area of code (such a...
Definition: crashzone.hxx:34
int mnDisableEntries
delays to take various actions in 1/4 of a second increments.
Definition: crashzone.hxx:24
std::atomic< std::make_unsigned_t< std::sig_atomic_t >> AtomicCounter
Definition: crashzone.hxx:44
static AtomicCounter gnEnterCount
how many times have we entered a zone
Definition: crashzone.hxx:51
static bool isInZone()
Definition: crashzone.hxx:58
static void enter()
Definition: crashzone.hxx:61
static AtomicCounter gnLeaveCount
how many times have we left a new zone
Definition: crashzone.hxx:53
static void leave()
Definition: crashzone.hxx:62