SMACC
Loading...
Searching...
No Matches
callback_counter_semaphore.cpp
Go to the documentation of this file.
1/*****************************************************************************************************************
2 * ReelRobotix Inc. - Software License Agreement Copyright (c) 2018
3 * Authors: Pablo Inigo Blasco, Brett Aldrich
4 *
5 ******************************************************************************************************************/
6#include <iostream>
7#include <boost/signals2.hpp>
8#include <thread>
9#include <condition_variable>
10#include <mutex>
11#include <boost/signals2.hpp>
12#include <ros/ros.h>
14
15namespace smacc
16{
17 CallbackCounterSemaphore::CallbackCounterSemaphore(std::string name, int count) : count_(count), name_(name) {}
18
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 }
35
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 }
43
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 }
60
61 void CallbackCounterSemaphore::addConnection(boost::signals2::connection conn)
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 }
73
74}
void addConnection(boost::signals2::connection conn)
std::vector< boost::signals2::connection > connections_
CallbackCounterSemaphore(std::string name, int count=0)