hpp-pinocchio  4.9.1
Wrapping of the kinematic/dynamic chain Pinocchio for HPP.
pool.hh
Go to the documentation of this file.
1 //
2 // Copyright (c) 2018 CNRS
3 // Author: Joseph Mirabel
4 //
5 //
6 // This file is part of hpp-pinocchio
7 // hpp-pinocchio is free software: you can redistribute it
8 // and/or modify it under the terms of the GNU Lesser General Public
9 // License as published by the Free Software Foundation, either version
10 // 3 of the License, or (at your option) any later version.
11 //
12 // hpp-pinocchio is distributed in the hope that it will be
13 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
14 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 // General Lesser Public License for more details. You should have
16 // received a copy of the GNU Lesser General Public License along with
17 // hpp-pinocchio If not, see
18 // <http://www.gnu.org/licenses/>.
19 
20 #ifndef HPP_PINOCCHIO_POOL_HH
21 #define HPP_PINOCCHIO_POOL_HH
22 
23 # include <vector>
24 
25 # include <boost/thread/mutex.hpp>
26 # include <boost/thread/condition_variable.hpp>
27 
28 # include <hpp/pinocchio/config.hh>
29 
30 namespace hpp {
31  namespace pinocchio {
32 
47  template <typename T>
48  class HPP_PINOCCHIO_DLLAPI Pool
49  {
50  public:
53  T* acquire ()
54  {
55  boost::mutex::scoped_lock lock (mutex_);
56  condVariable_.wait (lock, OneIsAvailable(*this));
57  T* ret = NULL;
58  std::swap (ret, Ts_[lastFree_]);
59  lastFree_++;
60  return ret;
61  }
62 
66  void release (T* t)
67  {
68  boost::mutex::scoped_lock lock (mutex_);
69  // At least one T* is not free.
70  assert (lastFree_ > 0);
71  assert (Ts_[lastFree_-1] == NULL);
72  Ts_[lastFree_-1] = t;
73  lastFree_--;
74  condVariable_.notify_one ();
75  }
76 
78  bool available () const
79  {
80  return OneIsAvailable (*this) ();
81  }
82 
83  std::size_t size () const
84  {
85  return Ts_.size();
86  }
87 
89  void clear ()
90  {
91  boost::mutex::scoped_lock lock (mutex_);
92  if (lastFree_ > 0)
93  throw std::logic_error ("Cannot free pool when some objects are in use.");
94  for (std::size_t i = 0; i < size(); ++i) delete Ts_[i];
95  Ts_.resize(0);
96  }
97 
100  void push_back (T* t)
101  {
102  boost::mutex::scoped_lock lock (mutex_);
103  Ts_.push_back(t);
104  }
105 
108  template <class InputIterator>
109  void push_back (InputIterator first, InputIterator last)
110  {
111  boost::mutex::scoped_lock lock (mutex_);
112  Ts_.insert(Ts_.end(), first, last);
113  }
114 
117  ~Pool ()
118  {
119  clear();
120  }
121 
123  Pool () : lastFree_ (0) {}
124 
125  private:
126  struct OneIsAvailable
127  {
128  const Pool& p;
129  OneIsAvailable (const Pool& pool) : p (pool) {}
130  bool operator() () { return p.lastFree_ < p.size(); }
131  };
132 
133  boost::mutex mutex_;
134  boost::condition_variable condVariable_;
135  std::vector<T*> Ts_;
136  std::size_t lastFree_;
137  }; // class Pool
138  } // namespace pinocchio
139 } // namespace hpp
140 
141 #endif // HPP_PINOCCHIO_DEVICE_HH
~Pool()
Definition: pool.hh:117
void release(T *t)
Definition: pool.hh:66
Utility functions.
Pool of objects.
Definition: pool.hh:48
void push_back(T *t)
Definition: pool.hh:100
bool available() const
Returns true is at least one object is not locked.
Definition: pool.hh:78
T * acquire()
Definition: pool.hh:53
Pool()
Constructor.
Definition: pool.hh:123
void clear()
Deletes all internal objects.
Definition: pool.hh:89
Transform3f t
FCL_REAL size() const
std::size_t size() const
Definition: pool.hh:83
void push_back(InputIterator first, InputIterator last)
Definition: pool.hh:109