Diag-Client-Lib
Public Types | Public Member Functions | Private Member Functions | Private Attributes | List of all members
utility::sync_timer::SyncTimer< ClockType > Class Template Referencefinal

Timer class for timeout monitoring. More...

#include <sync_timer.h>

Public Types

enum class  TimerState : std::uint8_t { kIdle = 0 , kCancelRequested , kTimeout }
 Definition of different timer state during timeout monitoring. More...
 
using Clock = ClockType
 Type alias for the clock type. More...
 
using TimePoint = std::chrono::time_point< Clock >
 Type alias for the clock time point. More...
 

Public Member Functions

 SyncTimer ()
 Construct an instance of SyncTimer. More...
 
 ~SyncTimer ()
 Destruct an instance of SyncTimer. More...
 
template<typename TimeoutCallback , typename CancelCallback >
void WaitForTimeout (TimeoutCallback &&timeout_func, CancelCallback &&cancellation_func, std::chrono::milliseconds const timeout)
 Helper function to wait for response with timeout monitoring. More...
 
auto IsTimerActive ()
 Function to query if timer is running. More...
 
void CancelWait ()
 Function to cancel the synchronous wait. More...
 

Private Member Functions

auto Start (std::chrono::milliseconds const timeout) noexcept -> TimerState
 Function to start the timeout monitoring. More...
 
void Stop () noexcept
 Function to stop the current timeout monitoring. More...
 

Private Attributes

std::condition_variable cond_var_
 The conditional variable needed for synchronizing between start and stop of running timer. More...
 
std::mutex mutex_lock_
 The mutex for the conditional variable. More...
 
bool exit_request_
 The flag to terminate the wait. More...
 
bool start_running_
 The flag to stop the current running timer. More...
 

Detailed Description

template<typename ClockType>
class utility::sync_timer::SyncTimer< ClockType >

Timer class for timeout monitoring.

Template Parameters
ClockTypeThe type of clock to be used for time monitoring

Definition at line 24 of file sync_timer.h.

Member Typedef Documentation

◆ Clock

template<typename ClockType >
using utility::sync_timer::SyncTimer< ClockType >::Clock = ClockType

Type alias for the clock type.

Definition at line 29 of file sync_timer.h.

◆ TimePoint

template<typename ClockType >
using utility::sync_timer::SyncTimer< ClockType >::TimePoint = std::chrono::time_point<Clock>

Type alias for the clock time point.

Definition at line 34 of file sync_timer.h.

Member Enumeration Documentation

◆ TimerState

template<typename ClockType >
enum utility::sync_timer::SyncTimer::TimerState : std::uint8_t
strong

Definition of different timer state during timeout monitoring.

Enumerator
kIdle 
kCancelRequested 
kTimeout 

Definition at line 39 of file sync_timer.h.

39 : std::uint8_t { kIdle = 0, kCancelRequested, kTimeout };

Constructor & Destructor Documentation

◆ SyncTimer()

template<typename ClockType >
utility::sync_timer::SyncTimer< ClockType >::SyncTimer ( )
inline

Construct an instance of SyncTimer.

Definition at line 44 of file sync_timer.h.

44 : cond_var_{}, mutex_lock_{}, exit_request_{false}, start_running_{false} {}
std::mutex mutex_lock_
The mutex for the conditional variable.
Definition: sync_timer.h:148
bool start_running_
The flag to stop the current running timer.
Definition: sync_timer.h:158
std::condition_variable cond_var_
The conditional variable needed for synchronizing between start and stop of running timer.
Definition: sync_timer.h:143
bool exit_request_
The flag to terminate the wait.
Definition: sync_timer.h:153

◆ ~SyncTimer()

template<typename ClockType >
utility::sync_timer::SyncTimer< ClockType >::~SyncTimer ( )
inline

Member Function Documentation

◆ CancelWait()

template<typename ClockType >
void utility::sync_timer::SyncTimer< ClockType >::CancelWait ( )
inline

Function to cancel the synchronous wait.

Definition at line 90 of file sync_timer.h.

90 { Stop(); }
void Stop() noexcept
Function to stop the current timeout monitoring.
Definition: sync_timer.h:133

References utility::sync_timer::SyncTimer< ClockType >::Stop().

Referenced by doip_client::channel::tcp_channel::DiagnosticMessageHandler::DiagnosticMessageHandlerImpl::Stop(), and doip_client::channel::tcp_channel::RoutingActivationHandler::RoutingActivationHandlerImpl::Stop().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ IsTimerActive()

template<typename ClockType >
auto utility::sync_timer::SyncTimer< ClockType >::IsTimerActive ( )
inline

◆ Start()

template<typename ClockType >
auto utility::sync_timer::SyncTimer< ClockType >::Start ( std::chrono::milliseconds const  timeout) -> TimerState
inlineprivatenoexcept

Function to start the timeout monitoring.

Parameters
[in]timeoutThe timeout value in milliseconds after which timeout happens
Returns
TimerState "kTimeout" in case of timeout or "kCancelRequested" when timeout monitoring was cancelled

Definition at line 100 of file sync_timer.h.

100  {
101  TimerState timer_state{TimerState::kIdle};
102  std::unique_lock<std::mutex> lck(mutex_lock_);
103  start_running_ = true;
104  TimePoint const expiry_time_point{Clock::now() + timeout};
105  if (cond_var_.wait_until(lck, expiry_time_point, [this, &expiry_time_point, &timer_state]() {
106  bool do_exit_wait{false};
107  // check if exit was requested
108  if (!exit_request_) {
109  TimePoint const current_time{Clock::now()};
110  // check for expiry
111  if (current_time >= expiry_time_point) {
112  // timeout
113  timer_state = TimerState::kTimeout;
114  do_exit_wait = true;
115  } else {
116  // check for cancellation request
117  if (!start_running_) {
118  timer_state = TimerState::kCancelRequested;
119  do_exit_wait = true;
120  } // else - spurious wake-up, do nothing
121  }
122  } else {
123  do_exit_wait = true;
124  }
125  return do_exit_wait;
126  })) {}
127  return timer_state;
128  }
TimerState
Definition of different timer state during timeout monitoring.
Definition: sync_timer.h:39
std::chrono::time_point< Clock > TimePoint
Type alias for the clock time point.
Definition: sync_timer.h:34

References utility::sync_timer::SyncTimer< ClockType >::cond_var_, utility::sync_timer::SyncTimer< ClockType >::exit_request_, utility::sync_timer::SyncTimer< ClockType >::kCancelRequested, utility::sync_timer::SyncTimer< ClockType >::kIdle, utility::sync_timer::SyncTimer< ClockType >::kTimeout, utility::sync_timer::SyncTimer< ClockType >::mutex_lock_, and utility::sync_timer::SyncTimer< ClockType >::start_running_.

Referenced by utility::sync_timer::SyncTimer< ClockType >::WaitForTimeout().

Here is the caller graph for this function:

◆ Stop()

template<typename ClockType >
void utility::sync_timer::SyncTimer< ClockType >::Stop ( )
inlineprivatenoexcept

Function to stop the current timeout monitoring.

Definition at line 133 of file sync_timer.h.

133  {
134  std::lock_guard<std::mutex> const lck(mutex_lock_);
135  start_running_ = false;
136  cond_var_.notify_all();
137  }

Referenced by utility::sync_timer::SyncTimer< ClockType >::CancelWait().

Here is the caller graph for this function:

◆ WaitForTimeout()

template<typename ClockType >
template<typename TimeoutCallback , typename CancelCallback >
void utility::sync_timer::SyncTimer< ClockType >::WaitForTimeout ( TimeoutCallback &&  timeout_func,
CancelCallback &&  cancellation_func,
std::chrono::milliseconds const  timeout 
)
inline

Helper function to wait for response with timeout monitoring.

Template Parameters
TimeoutCallbackThe callback functor type for timeout notification
CancelCallbackThe callback functor type for cancellation notification
Parameters
[in]timeout_funcThe functor to be called when timeout occurs
[in]cancel_funcThe functor to be called when expected event occurs within timeout
[in]timeoutThe timeout in milliseconds

Definition at line 70 of file sync_timer.h.

71  {
72  if (Start(timeout) == TimerState::kTimeout) {
73  timeout_func();
74  } else {
75  cancellation_func();
76  }
77  }
auto Start(std::chrono::milliseconds const timeout) noexcept -> TimerState
Function to start the timeout monitoring.
Definition: sync_timer.h:100

References utility::sync_timer::SyncTimer< ClockType >::kTimeout, and utility::sync_timer::SyncTimer< ClockType >::Start().

Here is the call graph for this function:

Member Data Documentation

◆ cond_var_

template<typename ClockType >
std::condition_variable utility::sync_timer::SyncTimer< ClockType >::cond_var_
private

The conditional variable needed for synchronizing between start and stop of running timer.

Definition at line 143 of file sync_timer.h.

Referenced by utility::sync_timer::SyncTimer< ClockType >::Start(), and utility::sync_timer::SyncTimer< ClockType >::~SyncTimer().

◆ exit_request_

template<typename ClockType >
bool utility::sync_timer::SyncTimer< ClockType >::exit_request_
private

The flag to terminate the wait.

Definition at line 153 of file sync_timer.h.

Referenced by utility::sync_timer::SyncTimer< ClockType >::Start(), and utility::sync_timer::SyncTimer< ClockType >::~SyncTimer().

◆ mutex_lock_

template<typename ClockType >
std::mutex utility::sync_timer::SyncTimer< ClockType >::mutex_lock_
private

◆ start_running_

template<typename ClockType >
bool utility::sync_timer::SyncTimer< ClockType >::start_running_
private

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