SMACC2
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
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 (boost::signals2::connection conn)
 

Private Attributes

int count_
 
std::mutex mutex_
 
std::condition_variable cv_
 
std::vector< boost::signals2::connection > connections_
 
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 35 of file callback_counter_semaphore.cpp.

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

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

◆ addConnection()

void smacc2::CallbackCounterSemaphore::addConnection ( boost::signals2::connection  conn)

Definition at line 91 of file callback_counter_semaphore.cpp.

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

References connections_, finalized, mutex_, and name_.

◆ finalize()

void smacc2::CallbackCounterSemaphore::finalize ( )

Definition at line 70 of file callback_counter_semaphore.cpp.

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

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

◆ release()

void smacc2::CallbackCounterSemaphore::release ( )

Definition at line 59 of file callback_counter_semaphore.cpp.

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

References count_, cv_, mutex_, and name_.

Member Data Documentation

◆ connections_

std::vector<boost::signals2::connection> 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: