hpp-pinocchio  6.0.0
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 
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are
9 // met:
10 //
11 // 1. Redistributions of source code must retain the above copyright
12 // notice, this list of conditions and the following disclaimer.
13 //
14 // 2. Redistributions in binary form must reproduce the above copyright
15 // notice, this list of conditions and the following disclaimer in the
16 // documentation and/or other materials provided with the distribution.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
29 // DAMAGE.
30 
31 #ifndef HPP_PINOCCHIO_POOL_HH
32 #define HPP_PINOCCHIO_POOL_HH
33 
34 #include <boost/thread/condition_variable.hpp>
35 #include <boost/thread/mutex.hpp>
36 #include <hpp/pinocchio/config.hh>
37 #include <vector>
38 
39 namespace hpp {
40 namespace pinocchio {
41 
56 template <typename T>
58  public:
61  T* acquire() {
62  boost::mutex::scoped_lock lock(mutex_);
63  condVariable_.wait(lock, OneIsAvailable(*this));
64  T* ret = NULL;
65  std::swap(ret, Ts_[lastFree_]);
66  lastFree_++;
67  return ret;
68  }
69 
73  void release(T* t) {
74  boost::mutex::scoped_lock lock(mutex_);
75  // At least one T* is not free.
76  assert(lastFree_ > 0);
77  assert(Ts_[lastFree_ - 1] == NULL);
78  Ts_[lastFree_ - 1] = t;
79  lastFree_--;
80  condVariable_.notify_one();
81  }
82 
84  bool available() const { return OneIsAvailable(*this)(); }
85 
86  std::size_t size() const { return Ts_.size(); }
87 
89  void clear() {
90  boost::mutex::scoped_lock lock(mutex_);
91  if (lastFree_ > 0)
92  throw std::logic_error("Cannot free pool when some objects are in use.");
93  for (std::size_t i = 0; i < size(); ++i) delete Ts_[i];
94  Ts_.resize(0);
95  }
96 
99  void push_back(T* t) {
100  boost::mutex::scoped_lock lock(mutex_);
101  Ts_.push_back(t);
102  }
103 
106  template <class InputIterator>
107  void push_back(InputIterator first, InputIterator last) {
108  boost::mutex::scoped_lock lock(mutex_);
109  Ts_.insert(Ts_.end(), first, last);
110  }
111 
114  ~Pool() { clear(); }
115 
117  Pool() : lastFree_(0) {}
118 
119  private:
120  struct OneIsAvailable {
121  const Pool& p;
122  OneIsAvailable(const Pool& pool) : p(pool) {}
123  bool operator()() { return p.lastFree_ < p.size(); }
124  };
125 
126  boost::mutex mutex_;
127  boost::condition_variable condVariable_;
128  std::vector<T*> Ts_;
129  std::size_t lastFree_;
130 }; // class Pool
131 } // namespace pinocchio
132 } // namespace hpp
133 
134 #endif // HPP_PINOCCHIO_DEVICE_HH
Pool of objects.
Definition: pool.hh:57
std::size_t size() const
Definition: pool.hh:86
void push_back(InputIterator first, InputIterator last)
Definition: pool.hh:107
void push_back(T *t)
Definition: pool.hh:99
~Pool()
Definition: pool.hh:114
void clear()
Deletes all internal objects.
Definition: pool.hh:89
void release(T *t)
Definition: pool.hh:73
T * acquire()
Definition: pool.hh:61
bool available() const
Returns true is at least one object is not locked.
Definition: pool.hh:84
Pool()
Constructor.
Definition: pool.hh:117
#define HPP_PINOCCHIO_DLLAPI
Definition: config.hh:88
Utility functions.
Definition: body.hh:39
Definition: collision-object.hh:40