SMACC
Loading...
Searching...
No Matches
Public Member Functions | Private Attributes | List of all members
smacc::CallbackCounterSemaphore Class Reference

#include <callback_counter_semaphore.h>

Collaboration diagram for smacc::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 18 of file callback_counter_semaphore.h.

Constructor & Destructor Documentation

◆ CallbackCounterSemaphore()

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

Member Function Documentation

◆ acquire()

bool smacc::CallbackCounterSemaphore::acquire ( )

Definition at line 19 of file callback_counter_semaphore.cpp.

19 {
20 std::unique_lock<std::mutex> lock(mutex_);
21 ROS_DEBUG("[CallbackCounterSemaphore] acquire callback %s %ld",name_.c_str(), (long)this);
22
23 if(finalized)
24 {
25 ROS_DEBUG("[CallbackCounterSemaphore] callback rejected %s %ld",name_.c_str(), (long)this);
26 return false;
27 }
28
29 ++count_;
30 cv_.notify_one();
31
32 ROS_DEBUG("[CallbackCounterSemaphore] callback accepted %s %ld",name_.c_str(), (long)this);
33 return true;
34 }

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

◆ addConnection()

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

Definition at line 61 of file callback_counter_semaphore.cpp.

62 {
63 std::unique_lock<std::mutex> lock(mutex_);
64
65 if(finalized)
66 {
67 ROS_DEBUG("[CallbackCounterSemaphore] ignoring adding callback, already finalized %s %ld",name_.c_str(), (long)this);
68 return;
69 }
70
71 connections_.push_back(conn);
72 }
std::vector< boost::signals2::connection > connections_

References connections_, finalized, mutex_, and name_.

◆ finalize()

void smacc::CallbackCounterSemaphore::finalize ( )

Definition at line 44 of file callback_counter_semaphore.cpp.

44 {
45 std::unique_lock<std::mutex> lock(mutex_);
46
47 while (count_ > 0) {
48 cv_.wait(lock);
49 }
50 finalized = true;
51
52 for(auto conn: connections_)
53 {
54 conn.disconnect();
55 }
56
57 connections_.clear();
58 ROS_DEBUG("[CallbackCounterSemaphore] callbacks finalized %s %ld",name_.c_str(), (long)this);
59 }

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

◆ release()

void smacc::CallbackCounterSemaphore::release ( )

Definition at line 36 of file callback_counter_semaphore.cpp.

36 {
37 std::unique_lock<std::mutex> lock(mutex_);
38 --count_;
39 cv_.notify_one();
40
41 ROS_DEBUG("[CallbackCounterSemaphore] callback finished %s %ld",name_.c_str(), (long)this);
42 }

References count_, cv_, mutex_, and name_.

Member Data Documentation

◆ connections_

std::vector<boost::signals2::connection> smacc::CallbackCounterSemaphore::connections_
private

Definition at line 33 of file callback_counter_semaphore.h.

Referenced by addConnection(), and finalize().

◆ count_

int smacc::CallbackCounterSemaphore::count_
private

Definition at line 30 of file callback_counter_semaphore.h.

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

◆ cv_

std::condition_variable smacc::CallbackCounterSemaphore::cv_
private

Definition at line 32 of file callback_counter_semaphore.h.

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

◆ finalized

bool smacc::CallbackCounterSemaphore::finalized = false
private

Definition at line 34 of file callback_counter_semaphore.h.

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

◆ mutex_

std::mutex smacc::CallbackCounterSemaphore::mutex_
private

Definition at line 31 of file callback_counter_semaphore.h.

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

◆ name_

std::string smacc::CallbackCounterSemaphore::name_
private

Definition at line 35 of file callback_counter_semaphore.h.

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


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