18 : m_timeout(timeout), m_run_callbacks_on_exit(run_callback_on_exit) {
40 m_entries.emplace_back(callback, expiration);
67 [handle](
Entry &entry) { return entry.handle == handle; });
72 it->expiration = expiration;
93 [handle](
Entry &entry) { return entry.handle == handle; });
109 : handle(
Alarm::GetNextUniqueHandle()), callback(std::move(callback)),
110 expiration(std::move(expiration)) {}
115 "lldb.debugger.alarm-thread", [
this] {
return AlarmThread(); },
121 "failed to launch host thread: {0}");
141 std::optional<TimePoint> next_alarm;
157 llvm::SmallVector<Callback, 1> callbacks;
161 if (!
m_alarm_cv.wait_until(alarm_lock, *next_alarm, predicate)) {
170 const TimePoint now = std::chrono::system_clock::now();
173 if (it->expiration <= now) {
174 callbacks.emplace_back(std::move(it->callback));
193 callbacks.emplace_back(std::move(entry.callback));
201 if (!next_alarm || entry.expiration < *next_alarm)
202 next_alarm = entry.expiration;
209 for (
Callback &callback : callbacks)
216 return std::chrono::system_clock::now() +
m_timeout;
220 static std::atomic<Handle> g_next_handle = 1;
221 return g_next_handle++;
#define LLDB_LOG_ERROR(log, error,...)
enables scheduling a callback function after a specified timeout.
Alarm(Duration timeout, bool run_callback_on_exit=false)
bool m_exit
Flag to signal the alarm thread to exit.
std::function< void()> Callback
llvm::sys::TimePoint<> TimePoint
TimePoint GetNextExpiration() const
Helper to compute the next time the alarm thread needs to wake up.
lldb::thread_result_t AlarmThread()
bool AlarmThreadRunning()
static constexpr Handle INVALID_HANDLE
Duration m_timeout
Timeout between when an alarm is created and when it fires.
void StartAlarmThread()
Helper functions to start, stop and check the status of the alarm thread.
std::vector< Entry > m_entries
List of alarm entries.
bool m_run_callbacks_on_exit
Flag to signal we should run all callbacks on exit.
std::condition_variable m_alarm_cv
Condition variable used to wake up the alarm thread.
static Handle GetNextUniqueHandle()
Return an unique, monotonically increasing handle.
bool Cancel(Handle handle)
Cancel the alarm for the given Handle.
bool Restart(Handle handle)
Restart the alarm for the given Handle.
bool m_recompute_next_alarm
Flag to signal the alarm thread that something changed and we need to recompute the next alarm.
std::mutex m_alarm_mutex
Synchronize access between the alarm thread and the main thread.
Handle Create(Callback callback)
Create an alarm for the given callback.
std::chrono::milliseconds Duration
HostThread m_alarm_thread
The alarm thread.
Status Join(lldb::thread_result_t *result)
static llvm::Expected< HostThread > LaunchThread(llvm::StringRef name, std::function< lldb::thread_result_t()> thread_function, size_t min_stack_byte_size=0)
A class that represents a running process on the host machine.
Log * GetLog(Cat mask)
Retrieve the Log object for the channel associated with the given log enum.
Entry(Type t=Type::Invalid, const char *s=nullptr, const char *f=nullptr)