iceoryx_hoofs  2.0.2
signal_watcher.hpp
1 // Copyright (c) 2021 by Apex.AI Inc. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 // SPDX-License-Identifier: Apache-2.0
16 #ifndef IOX_HOOFS_POSIX_WRAPPER_SIGNAL_WATCHER_HPP
17 #define IOX_HOOFS_POSIX_WRAPPER_SIGNAL_WATCHER_HPP
18 
19 #include "iceoryx_hoofs/posix_wrapper/semaphore.hpp"
20 #include "iceoryx_hoofs/posix_wrapper/signal_handler.hpp"
21 
22 #include <atomic>
23 
24 namespace iox
25 {
26 namespace posix
27 {
48 {
49  public:
50  SignalWatcher(const SignalWatcher&) = delete;
51  SignalWatcher(SignalWatcher&&) = delete;
52  ~SignalWatcher() = default;
53 
54  SignalWatcher& operator=(const SignalWatcher&) = delete;
55  SignalWatcher& operator=(SignalWatcher&&) = delete;
56 
58  static SignalWatcher& getInstance() noexcept;
59 
61  void waitForSignal() const noexcept;
62 
64  bool wasSignalTriggered() const noexcept;
65 
66  protected:
67  SignalWatcher() noexcept;
68 
69  private:
70  friend void internalSignalHandler(int) noexcept;
71  mutable std::atomic<uint64_t> m_numberOfWaiters{0U};
72  mutable Semaphore m_semaphore;
73 
74  std::atomic_bool m_hasSignalOccurred{false};
75  SignalGuard m_sigTermGuard;
76  SignalGuard m_sigIntGuard;
77 };
78 
80 void waitForTerminationRequest() noexcept;
81 
83 bool hasTerminationRequested() noexcept;
84 } // namespace posix
85 } // namespace iox
86 
87 #endif
Posix semaphore C++ Wrapping class.
Definition: semaphore.hpp:82
The SignalWatcher waits for SIGINT and SIGTERM. One can wait until the signal has occurred or ask the...
Definition: signal_watcher.hpp:48
bool wasSignalTriggered() const noexcept
Returns true when SIGTERM or SIGINT has occurred, otherwise false.
static SignalWatcher & getInstance() noexcept
Returns the singleton instance of the SignalWatcher.
void waitForSignal() const noexcept
Blocks until either SIGTERM or SIGINT has occurred.
building block to easily create free function for logging in a library context
Definition: lockfree_queue.hpp:29