RDKit
Open-source cheminformatics and machine learning.
Loading...
Searching...
No Matches
ControlCHandler.h
Go to the documentation of this file.
1//
2// Copyright (C) David Cosgrove 2025.
3//
4// @@ All Rights Reserved @@
5// This file is part of the RDKit.
6// The contents are covered by the terms of the BSD license
7// which is included in the file license.txt, found at the root
8// of the RDKit source tree.
9//
10
11#ifndef CONTROLCHANDLER_H
12#define CONTROLCHANDLER_H
13
14#include <atomic>
15#include <csignal>
16
17#include <RDGeneral/export.h>
18
19namespace RDKit {
20//! This class catches a control-C/SIGINT and sets the flag d_gotSignal
21//! if one is received. It is intended to be used inside a long
22//! C++ calculation called from Python which intercepts the signal
23//! handler. The C++ code must check the value of d_gotSignal
24//! periodically and act accordingly. The destructor resets
25//! the signal handler and flag for next use, which is essential
26//! because it's a static variable.
27//! Example usage, inside a boost::python wrapper:
28//! ResultsObject results;
29//! {
30//! NOGIL gil;
31//! results = someFunction();
32//! }
33//! if (results.getCancelled()) {
34//! throw_runtime_error("someFunction cancelled");
35//! }
36//! It's important that the exception is thrown once the GIL has been
37//! released, otherwise a crash is inevitable at some future point.
39 public:
40 ControlCHandler() { d_prev_handler = std::signal(SIGINT, signalHandler); }
46 std::signal(SIGINT, d_prev_handler);
47 d_gotSignal = false;
48 }
49 static bool getGotSignal() { return d_gotSignal; }
50 static void signalHandler(int signalNumber) {
51 if (signalNumber == SIGINT) {
52 d_gotSignal = true;
53 std::signal(SIGINT, d_prev_handler);
54 }
55 }
56 static void reset() {
57 d_gotSignal = false;
58 std::signal(SIGINT, signalHandler);
59 }
60
61 private:
62 inline static bool d_gotSignal{false};
63 inline static void (*d_prev_handler)(int);
64};
65} // namespace RDKit
66#endif // CONTROLCHANDLER_H
ControlCHandler(ControlCHandler &&)=delete
ControlCHandler & operator=(ControlCHandler &&)=delete
ControlCHandler(const ControlCHandler &)=delete
ControlCHandler & operator=(const ControlCHandler &)=delete
static void signalHandler(int signalNumber)
Std stuff.
bool rdvalue_is(const RDValue_cast_t)