Google OR-Tools v9.15
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
sigint.h
Go to the documentation of this file.
1// Copyright 2010-2025 Google LLC
2// Licensed under the Apache License, Version 2.0 (the "License");
3// you may not use this file except in compliance with the License.
4// You may obtain a copy of the License at
5//
6// http://www.apache.org/licenses/LICENSE-2.0
7//
8// Unless required by applicable law or agreed to in writing, software
9// distributed under the License is distributed on an "AS IS" BASIS,
10// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11// See the License for the specific language governing permissions and
12// limitations under the License.
13
14#ifndef ORTOOLS_UTIL_SIGINT_H_
15#define ORTOOLS_UTIL_SIGINT_H_
16
17#include <atomic>
18#include <functional>
19
20#include "ortools/port/os.h"
21
22#if ORTOOLS_TARGET_OS_SUPPORTS_THREADS
23
24namespace operations_research {
25
26class SigintHandler {
27 public:
28 SigintHandler() = default;
29 ~SigintHandler();
30
31 // Catches ^C and call f() the first time this happen. If ^C is pressed 3
32 // times, kill the program.
33 void Register(const std::function<void()>& f);
34
35 private:
36 std::atomic<int> num_calls_ = 0;
37
38 static void SigHandler(int s);
39 thread_local static std::function<void()> handler_;
40};
41
42class SigtermHandler {
43 public:
44 SigtermHandler() = default;
45 ~SigtermHandler();
46
47 // Catches SIGTERM and call f(). It is recommended that f() calls exit() to
48 // terminate the program.
49 void Register(const std::function<void()>& f);
50
51 private:
52 static void SigHandler(int s);
53 thread_local static std::function<void()> handler_;
54};
55
56} // namespace operations_research
57
58#endif // ORTOOLS_TARGET_OS_SUPPORTS_THREADS
59
60#endif // ORTOOLS_UTIL_SIGINT_H_
OR-Tools root namespace.