threading.h
Go to the documentation of this file.
1 #ifndef H_CAN_THREADING_BASE
2 #define H_CAN_THREADING_BASE
3 
5 #include <boost/thread/thread.hpp>
6 #include <boost/bind.hpp>
7 
8 namespace can{
9 
10 
12  boost::mutex mutex_;
13  boost::condition_variable cond_;
16  void updateState(const can::State &s){
17  boost::mutex::scoped_lock lock(mutex_);
18  state_ = s;
19  lock.unlock();
20  cond_.notify_all();
21  }
22 public:
23  template<typename InterfaceType> StateWaiter(InterfaceType *interface){
24  state_ = interface->getState();
25  state_listener_ = interface->createStateListener(can::StateInterface::StateDelegate(this, &StateWaiter::updateState));
26  }
27  template<typename DurationType> bool wait(const can::State::DriverState &s, const DurationType &duration){
28  boost::mutex::scoped_lock cond_lock(mutex_);
29  boost::system_time abs_time = boost::get_system_time() + duration;
30  while(s != state_.driver_state)
31  {
32  if(!cond_.timed_wait(cond_lock,abs_time))
33  {
34  return false;
35  }
36  }
37  return true;
38  }
39 };
40 
41 template<typename WrappedInterface> class ThreadedInterface : public WrappedInterface{
42  boost::shared_ptr<boost::thread> thread_;
43  void run_thread(){
44  WrappedInterface::run();
45  }
46 public:
47  virtual bool init(const std::string &device, bool loopback) {
48  if(!thread_ && WrappedInterface::init(device, loopback)){
49  StateWaiter waiter(this);
50  thread_.reset(new boost::thread(&ThreadedInterface::run_thread, this));
51  return waiter.wait(can::State::ready, boost::posix_time::seconds(1));
52  }
53  return WrappedInterface::getState().isReady();
54  }
55  virtual void shutdown(){
56  WrappedInterface::shutdown();
57  if(thread_){
58  thread_->interrupt();
59  thread_->join();
60  thread_.reset();
61  }
62  }
63  void join(){
64  if(thread_){
65  thread_->join();
66  }
67  }
68  virtual ~ThreadedInterface() {}
69  ThreadedInterface(): WrappedInterface() {}
70  template<typename T1> ThreadedInterface(const T1 &t1): WrappedInterface(t1) {}
71  template<typename T1, typename T2> ThreadedInterface(const T1 &t1, const T2 &t2): WrappedInterface(t1, t2) {}
72 
73 };
74 
75 
76 } // namespace can
77 #endif
boost::shared_ptr< boost::thread > thread_
Definition: threading.h:42
virtual bool init(const std::string &device, bool loopback)
Definition: threading.h:47
Definition: asio_base.h:11
boost::mutex mutex_
Definition: threading.h:12
bool wait(const can::State::DriverState &s, const DurationType &duration)
Definition: threading.h:27
boost::condition_variable cond_
Definition: threading.h:13
StateWaiter(InterfaceType *interface)
Definition: threading.h:23
ThreadedInterface(const T1 &t1)
Definition: threading.h:70
StateListener::ListenerConstSharedPtr StateListenerConstSharedPtr
Definition: interface.h:111
enum can::State::DriverState driver_state
virtual ~ThreadedInterface()
Definition: threading.h:68
ThreadedInterface(const T1 &t1, const T2 &t2)
Definition: threading.h:71
can::StateInterface::StateListenerConstSharedPtr state_listener_
Definition: threading.h:14
can::State state_
Definition: threading.h:15
void updateState(const can::State &s)
Definition: threading.h:16
virtual void shutdown()
Definition: threading.h:55


socketcan_interface
Author(s): Mathias Lüdtke
autogenerated on Fri May 14 2021 02:59:39