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
15#include <atomic>
16#include <csignal>
17#include <type_traits>
18
20{
23 int const mnAbortAfter;
24};
25
32template <typename Dummy> class CrashZone
33{
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
47
49 static inline AtomicCounter gnEnterCount = 0;
51 static inline AtomicCounter gnLeaveCount = 0;
52
53public:
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();
66};
67
68#endif // INCLUDED_COMPHELPER_CRASHZONE_H
69
70/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
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:33
std::atomic< std::make_unsigned_t< std::sig_atomic_t > > AtomicCounter
Definition: crashzone.hxx:42
static bool isInZone()
Definition: crashzone.hxx:56
static void enter()
Definition: crashzone.hxx:59
static const AtomicCounter & enterCount()
Definition: crashzone.hxx:57
static void leave()
Definition: crashzone.hxx:60
static AtomicCounter gnEnterCount
how many times have we entered a zone
Definition: crashzone.hxx:49
static AtomicCounter gnLeaveCount
how many times have we left a new zone
Definition: crashzone.hxx:51
int mnDisableEntries
delays to take various actions in 1/4 of a second increments.
Definition: crashzone.hxx:22