LLDB mainline
Classes | Public Types | Public Member Functions | Static Public Attributes | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
lldb_private::Alarm Class Reference

enables scheduling a callback function after a specified timeout. More...

#include <Alarm.h>

Classes

struct  Entry
 Alarm entry. More...
 

Public Types

using Handle = uint64_t
 
using Callback = std::function< void()>
 
using TimePoint = llvm::sys::TimePoint<>
 
using Duration = std::chrono::milliseconds
 

Public Member Functions

 Alarm (Duration timeout, bool run_callback_on_exit=false)
 
 ~Alarm ()
 
Handle Create (Callback callback)
 Create an alarm for the given callback.
 
bool Restart (Handle handle)
 Restart the alarm for the given Handle.
 
bool Cancel (Handle handle)
 Cancel the alarm for the given Handle.
 

Static Public Attributes

static constexpr Handle INVALID_HANDLE = 0
 

Private Member Functions

TimePoint GetNextExpiration () const
 Helper to compute the next time the alarm thread needs to wake up.
 
void StartAlarmThread ()
 Helper functions to start, stop and check the status of the alarm thread.
 
void StopAlarmThread ()
 
bool AlarmThreadRunning ()
 

Static Private Member Functions

static Handle GetNextUniqueHandle ()
 Return an unique, monotonically increasing handle.
 

Private Attributes

std::vector< Entrym_entries
 List of alarm entries.
 
Duration m_timeout
 Timeout between when an alarm is created and when it fires.
 
std::mutex m_alarm_mutex
 Synchronize access between the alarm thread and the main thread.
 
std::condition_variable m_alarm_cv
 Condition variable used to wake up the alarm thread.
 
bool m_recompute_next_alarm = false
 Flag to signal the alarm thread that something changed and we need to recompute the next alarm.
 
bool m_exit = false
 Flag to signal the alarm thread to exit.
 
bool m_run_callbacks_on_exit = false
 Flag to signal we should run all callbacks on exit.
 
HostThread m_alarm_thread
 The alarm thread.
 
lldb::thread_result_t AlarmThread ()
 

Detailed Description

enables scheduling a callback function after a specified timeout.

Creating an alarm for a callback returns a Handle that can be used to restart or cancel the alarm.

Definition at line 24 of file Alarm.h.

Member Typedef Documentation

◆ Callback

using lldb_private::Alarm::Callback = std::function<void()>

Definition at line 27 of file Alarm.h.

◆ Duration

using lldb_private::Alarm::Duration = std::chrono::milliseconds

Definition at line 29 of file Alarm.h.

◆ Handle

using lldb_private::Alarm::Handle = uint64_t

Definition at line 26 of file Alarm.h.

◆ TimePoint

using lldb_private::Alarm::TimePoint = llvm::sys::TimePoint<>

Definition at line 28 of file Alarm.h.

Constructor & Destructor Documentation

◆ Alarm()

Alarm::Alarm ( Duration  timeout,
bool  run_callback_on_exit = false 
)

Definition at line 17 of file Alarm.cpp.

References StartAlarmThread().

◆ ~Alarm()

Alarm::~Alarm ( )

Definition at line 22 of file Alarm.cpp.

References StopAlarmThread().

Member Function Documentation

◆ AlarmThread()

lldb::thread_result_t Alarm::AlarmThread ( )
private

◆ AlarmThreadRunning()

bool Alarm::AlarmThreadRunning ( )
private

Definition at line 137 of file Alarm.cpp.

References lldb_private::HostThread::IsJoinable(), and m_alarm_thread.

Referenced by Cancel(), Create(), and Restart().

◆ Cancel()

bool Alarm::Cancel ( Handle  handle)

Cancel the alarm for the given Handle.

The alarm and its handle will be removed.

Returns
True if the alarm was successfully canceled and the Handle removed. False if there is no alarm for the given Handle or the alarm already expired.

Definition at line 82 of file Alarm.cpp.

References AlarmThreadRunning(), m_alarm_mutex, and m_entries.

Referenced by lldb_private::ProgressManager::Increment().

◆ Create()

Alarm::Handle Alarm::Create ( Callback  callback)

Create an alarm for the given callback.

The alarm will expire and the callback will be called after the timeout.

Returns
Handle which can be used to restart or cancel the alarm.

Definition at line 24 of file Alarm.cpp.

References AlarmThreadRunning(), GetNextExpiration(), INVALID_HANDLE, m_alarm_cv, m_alarm_mutex, m_entries, and m_recompute_next_alarm.

Referenced by lldb_private::ProgressManager::Decrement().

◆ GetNextExpiration()

Alarm::TimePoint Alarm::GetNextExpiration ( ) const
private

Helper to compute the next time the alarm thread needs to wake up.

Definition at line 215 of file Alarm.cpp.

References m_timeout.

Referenced by Create(), and Restart().

◆ GetNextUniqueHandle()

Alarm::Handle Alarm::GetNextUniqueHandle ( )
staticprivate

Return an unique, monotonically increasing handle.

Definition at line 219 of file Alarm.cpp.

◆ Restart()

bool Alarm::Restart ( Handle  handle)

Restart the alarm for the given Handle.

The alarm will expire and the callback will be called after the timeout.

Returns
True if the alarm was successfully restarted. False if there is no alarm for the given Handle or the alarm already expired.

Definition at line 51 of file Alarm.cpp.

References AlarmThreadRunning(), GetNextExpiration(), m_alarm_cv, m_alarm_mutex, m_entries, and m_recompute_next_alarm.

◆ StartAlarmThread()

void Alarm::StartAlarmThread ( )
private

Helper functions to start, stop and check the status of the alarm thread.

Definition at line 112 of file Alarm.cpp.

References AlarmThread(), lldb_private::GetLog(), lldb_private::Host, lldb_private::HostThread::IsJoinable(), lldb_private::ThreadLauncher::LaunchThread(), LLDB_LOG_ERROR, and m_alarm_thread.

Referenced by Alarm().

◆ StopAlarmThread()

void Alarm::StopAlarmThread ( )
private

Member Data Documentation

◆ INVALID_HANDLE

constexpr Handle lldb_private::Alarm::INVALID_HANDLE = 0
staticconstexpr

◆ m_alarm_cv

std::condition_variable lldb_private::Alarm::m_alarm_cv
private

Condition variable used to wake up the alarm thread.

Definition at line 100 of file Alarm.h.

Referenced by AlarmThread(), Create(), Restart(), and StopAlarmThread().

◆ m_alarm_mutex

std::mutex lldb_private::Alarm::m_alarm_mutex
private

Synchronize access between the alarm thread and the main thread.

Definition at line 97 of file Alarm.h.

Referenced by AlarmThread(), Cancel(), Create(), Restart(), and StopAlarmThread().

◆ m_alarm_thread

HostThread lldb_private::Alarm::m_alarm_thread
private

The alarm thread.

Definition at line 92 of file Alarm.h.

Referenced by AlarmThreadRunning(), StartAlarmThread(), and StopAlarmThread().

◆ m_entries

std::vector<Entry> lldb_private::Alarm::m_entries
private

List of alarm entries.

Definition at line 85 of file Alarm.h.

Referenced by AlarmThread(), Cancel(), Create(), and Restart().

◆ m_exit

bool lldb_private::Alarm::m_exit = false
private

Flag to signal the alarm thread to exit.

Definition at line 107 of file Alarm.h.

Referenced by AlarmThread(), and StopAlarmThread().

◆ m_recompute_next_alarm

bool lldb_private::Alarm::m_recompute_next_alarm = false
private

Flag to signal the alarm thread that something changed and we need to recompute the next alarm.

Definition at line 104 of file Alarm.h.

Referenced by AlarmThread(), Create(), and Restart().

◆ m_run_callbacks_on_exit

bool lldb_private::Alarm::m_run_callbacks_on_exit = false
private

Flag to signal we should run all callbacks on exit.

Definition at line 110 of file Alarm.h.

Referenced by AlarmThread().

◆ m_timeout

Duration lldb_private::Alarm::m_timeout
private

Timeout between when an alarm is created and when it fires.

Definition at line 88 of file Alarm.h.

Referenced by GetNextExpiration().


The documentation for this class was generated from the following files: