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
19
namespace
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.
38
class
ControlCHandler
{
39
public
:
40
ControlCHandler
() { d_prev_handler = std::signal(SIGINT,
signalHandler
); }
41
ControlCHandler
(
const
ControlCHandler
&) =
delete
;
42
ControlCHandler
(
ControlCHandler
&&) =
delete
;
43
ControlCHandler
&
operator=
(
const
ControlCHandler
&) =
delete
;
44
ControlCHandler
&
operator=
(
ControlCHandler
&&) =
delete
;
45
~ControlCHandler
() {
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
RDKit::ControlCHandler
Definition
ControlCHandler.h:38
RDKit::ControlCHandler::ControlCHandler
ControlCHandler(ControlCHandler &&)=delete
RDKit::ControlCHandler::operator=
ControlCHandler & operator=(ControlCHandler &&)=delete
RDKit::ControlCHandler::~ControlCHandler
~ControlCHandler()
Definition
ControlCHandler.h:45
RDKit::ControlCHandler::ControlCHandler
ControlCHandler(const ControlCHandler &)=delete
RDKit::ControlCHandler::operator=
ControlCHandler & operator=(const ControlCHandler &)=delete
RDKit::ControlCHandler::ControlCHandler
ControlCHandler()
Definition
ControlCHandler.h:40
RDKit::ControlCHandler::reset
static void reset()
Definition
ControlCHandler.h:56
RDKit::ControlCHandler::getGotSignal
static bool getGotSignal()
Definition
ControlCHandler.h:49
RDKit::ControlCHandler::signalHandler
static void signalHandler(int signalNumber)
Definition
ControlCHandler.h:50
export.h
RDKit
Std stuff.
Definition
Abbreviations.h:19
RDGeneral
ControlCHandler.h
Generated on Sun Mar 9 2025 10:04:30 for RDKit by
1.12.0