TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec_signal.hpp
Go to the documentation of this file.
1// Time-stamp: <Last changed 2026-02-20 16:02:26 by magnolia>
2/*----------------------------------------------------------------------
3------------------------------------------------------------------------
4Copyright (c) 2020-2026 The Emacs Cat (https://github.com/olddeuteronomy/tec).
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17------------------------------------------------------------------------
18----------------------------------------------------------------------*/
27#pragma once
28
29#include <mutex>
30#include <condition_variable>
31
32#include "tec/tec_def.hpp" // IWYU pragma: keep
33
34
35namespace tec {
36
44class Signal {
45private:
46 using Lock = std::lock_guard<std::mutex>;
47 using ULock = std::unique_lock<std::mutex>;
48
49 mutable std::mutex m_;
50 mutable std::condition_variable cv_;
51 mutable bool flag_;
52
53public:
59 : flag_(false)
60 {}
61
62 Signal(const Signal&) = delete;
63 Signal(Signal&&) = delete;
64
65 virtual ~Signal() = default;
66
72 void set() {
73 {
74 Lock lock(m_);
75 flag_ = true;
76 }
77 cv_.notify_all();
78 }
79
85 void wait() const {
86 ULock ulock(m_);
87 cv_.wait(ulock, [this] { return flag_; });
88 }
89
99 template <typename Duration>
100 bool wait_for(Duration dur) const {
101 ULock ulock(m_);
102 return cv_.wait_for(ulock, dur, [this] { return flag_; });
103 }
104
110 struct OnExit {
111 private:
112 Signal* sig_;
113
114 public:
119 explicit OnExit(Signal* sig) : sig_{sig} {}
120
124 ~OnExit() { sig_->set(); }
125 };
126}; // class Signal
127
128} // namespace tec
A thread-safe signal mechanism for inter-thread synchronization.
Definition tec_signal.hpp:44
bool wait_for(Duration dur) const
Waits for the signal to be set for a specified duration.
Definition tec_signal.hpp:100
Signal()
Constructs a signal in the unsignaled state.
Definition tec_signal.hpp:58
void set()
Sets the signal to the signaled state and notifies all waiting threads.
Definition tec_signal.hpp:72
void wait() const
Waits indefinitely until the signal is set.
Definition tec_signal.hpp:85
Helper struct to signal termination on exit.
Definition tec_signal.hpp:110
~OnExit()
Destructor that sets the termination signal.
Definition tec_signal.hpp:124
OnExit(Signal *sig)
Constructs an OnExit helper with a termination signal.
Definition tec_signal.hpp:119
Common definitions and utilities for the tec namespace.