SMACC2
Loading...
Searching...
No Matches
smacc2::CallbackCounterSemaphore Class Reference

#include <callback_counter_semaphore.hpp>

Collaboration diagram for smacc2::CallbackCounterSemaphore:
Collaboration graph

Public Member Functions

 CallbackCounterSemaphore (std::string name, int count=0)
 
bool acquire ()
 
void release ()
 
void finalize ()
 
void addConnection (smacc2::SmaccSignalConnection conn)
 

Private Attributes

int count_
 
std::mutex mutex_
 
std::condition_variable cv_
 
std::vector< smacc2::SmaccSignalConnectionconnections_
 
bool finalized = false
 
std::string name_
 

Detailed Description

Definition at line 31 of file callback_counter_semaphore.hpp.

Constructor & Destructor Documentation

◆ CallbackCounterSemaphore()

smacc2::CallbackCounterSemaphore::CallbackCounterSemaphore ( std::string name,
int count = 0 )

Member Function Documentation

◆ acquire()

bool smacc2::CallbackCounterSemaphore::acquire ( )

Definition at line 34 of file callback_counter_semaphore.cpp.

35{
36 std::unique_lock<std::mutex> lock(mutex_);
37 RCLCPP_DEBUG(
38 rclcpp::get_logger(name_), "[CallbackCounterSemaphore] acquire callback %s %ld", name_.c_str(),
39 (long)this);
40
41 if (finalized)
42 {
43 RCLCPP_DEBUG(
44 rclcpp::get_logger(name_), "[CallbackCounterSemaphore] callback rejected %s %ld",
45 name_.c_str(), (long)this);
46 return false;
47 }
48
49 ++count_;
50 cv_.notify_one();
51
52 RCLCPP_DEBUG(
53 rclcpp::get_logger(name_), "[CallbackCounterSemaphore] callback accepted %s %ld", name_.c_str(),
54 (long)this);
55 return true;
56}

References count_, cv_, finalized, mutex_, and name_.

◆ addConnection()

void smacc2::CallbackCounterSemaphore::addConnection ( smacc2::SmaccSignalConnection conn)

Definition at line 90 of file callback_counter_semaphore.cpp.

91{
92 std::unique_lock<std::mutex> lock(mutex_);
93
94 if (finalized)
95 {
96 RCLCPP_DEBUG(
97 rclcpp::get_logger(name_),
98 "[CallbackCounterSemaphore] ignoring adding callback, already finalized %s %ld",
99 name_.c_str(), (long)this);
100 return;
101 }
102
103 connections_.push_back(conn);
104}
std::vector< smacc2::SmaccSignalConnection > connections_

References connections_, finalized, mutex_, and name_.

◆ finalize()

void smacc2::CallbackCounterSemaphore::finalize ( )

Definition at line 69 of file callback_counter_semaphore.cpp.

70{
71 std::unique_lock<std::mutex> lock(mutex_);
72
73 while (count_ > 0)
74 {
75 cv_.wait(lock);
76 }
77 finalized = true;
78
79 for (auto conn : connections_)
80 {
81 conn.disconnect();
82 }
83
84 connections_.clear();
85 RCLCPP_DEBUG(
86 rclcpp::get_logger(name_), "[CallbackCounterSemaphore] callbacks finalized %s %ld",
87 name_.c_str(), (long)this);
88}

References connections_, count_, cv_, finalized, mutex_, and name_.

◆ release()

void smacc2::CallbackCounterSemaphore::release ( )

Definition at line 58 of file callback_counter_semaphore.cpp.

59{
60 std::unique_lock<std::mutex> lock(mutex_);
61 --count_;
62 cv_.notify_one();
63
64 RCLCPP_DEBUG(
65 rclcpp::get_logger(name_), "[CallbackCounterSemaphore] callback finished %s %ld", name_.c_str(),
66 (long)this);
67}

References count_, cv_, mutex_, and name_.

Member Data Documentation

◆ connections_

std::vector<smacc2::SmaccSignalConnection> smacc2::CallbackCounterSemaphore::connections_
private

Definition at line 47 of file callback_counter_semaphore.hpp.

Referenced by addConnection(), and finalize().

◆ count_

int smacc2::CallbackCounterSemaphore::count_
private

Definition at line 44 of file callback_counter_semaphore.hpp.

Referenced by acquire(), finalize(), and release().

◆ cv_

std::condition_variable smacc2::CallbackCounterSemaphore::cv_
private

Definition at line 46 of file callback_counter_semaphore.hpp.

Referenced by acquire(), finalize(), and release().

◆ finalized

bool smacc2::CallbackCounterSemaphore::finalized = false
private

Definition at line 48 of file callback_counter_semaphore.hpp.

Referenced by acquire(), addConnection(), and finalize().

◆ mutex_

std::mutex smacc2::CallbackCounterSemaphore::mutex_
private

Definition at line 45 of file callback_counter_semaphore.hpp.

Referenced by acquire(), addConnection(), finalize(), and release().

◆ name_

std::string smacc2::CallbackCounterSemaphore::name_
private

Definition at line 49 of file callback_counter_semaphore.hpp.

Referenced by acquire(), addConnection(), finalize(), and release().


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