2016-09-26 16:50:13 +00:00
|
|
|
#pragma once
|
2016-08-25 21:47:45 +00:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
|
|
|
#include <stack>
|
|
|
|
#include <mutex>
|
|
|
|
|
|
|
|
|
|
|
|
namespace DB
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/** Pool for objects that cannot be used from different threads simultaneously.
|
|
|
|
* Allows to create an object for each thread.
|
|
|
|
* Pool has unbounded size and objects are not destroyed before destruction of pool.
|
2016-08-26 17:57:19 +00:00
|
|
|
*
|
|
|
|
* Use it in cases when thread local storage is not appropriate
|
|
|
|
* (when maximum number of simultaneously used objects is less
|
|
|
|
* than number of running/sleeping threads, that has ever used object,
|
|
|
|
* and creation/destruction of objects is expensive).
|
2016-08-25 21:47:45 +00:00
|
|
|
*/
|
2017-02-01 14:00:12 +00:00
|
|
|
template <typename T>
|
|
|
|
class SimpleObjectPool
|
2016-08-25 21:47:45 +00:00
|
|
|
{
|
2017-02-01 14:00:12 +00:00
|
|
|
protected:
|
2016-08-25 21:47:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Hold all avaiable objects in stack.
|
|
|
|
std::mutex mutex;
|
|
|
|
std::stack<std::unique_ptr<T>> stack;
|
2016-08-25 21:47:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Specialized deleter for std::unique_ptr.
|
|
|
|
/// Returns underlying pointer back to stack thus reclaiming its ownership.
|
|
|
|
struct Deleter
|
|
|
|
{
|
|
|
|
SimpleObjectPool<T> * parent;
|
2016-08-25 21:47:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Deleter(SimpleObjectPool<T> * parent_ = nullptr) : parent{parent_} {}
|
2016-08-25 21:47:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
void operator()(T * owning_ptr) const
|
|
|
|
{
|
|
|
|
std::lock_guard<std::mutex> lock{parent->mutex};
|
|
|
|
parent->stack.emplace(owning_ptr);
|
|
|
|
}
|
|
|
|
};
|
2016-08-25 21:47:45 +00:00
|
|
|
|
2017-02-01 14:00:12 +00:00
|
|
|
public:
|
2017-04-01 07:20:54 +00:00
|
|
|
using Pointer = std::unique_ptr<T, Deleter>;
|
|
|
|
|
|
|
|
/// Extracts and returns a pointer from the stack if it's not empty,
|
|
|
|
/// creates a new one by calling provided f() otherwise.
|
|
|
|
template <typename Factory>
|
|
|
|
Pointer get(Factory && f)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(mutex);
|
|
|
|
|
|
|
|
if (stack.empty())
|
|
|
|
{
|
|
|
|
lock.unlock();
|
|
|
|
return { f(), this };
|
|
|
|
}
|
|
|
|
|
|
|
|
auto object = stack.top().release();
|
|
|
|
stack.pop();
|
|
|
|
|
|
|
|
return { object, this };
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Like get(), but creates object using default constructor.
|
|
|
|
Pointer getDefault()
|
|
|
|
{
|
|
|
|
return get([] { return new T; });
|
|
|
|
}
|
2017-02-01 14:00:12 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Like SimpleObjectPool, but additionally allows store different kind of objects that are identified by Key
|
|
|
|
template <typename T, typename Key>
|
|
|
|
class ObjectPoolMap
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
using Object = SimpleObjectPool<T>;
|
2016-08-25 21:47:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
/// Key -> objects
|
|
|
|
using Container = std::map<Key, std::unique_ptr<Object>>;
|
2016-08-25 21:47:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
Container container;
|
|
|
|
std::mutex mutex;
|
2016-08-25 21:47:45 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
using Pointer = typename Object::Pointer;
|
2017-02-01 14:00:12 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
template <typename Factory>
|
|
|
|
Pointer get(const Key & key, Factory && f)
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> lock(mutex);
|
2016-08-25 21:47:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
auto it = container.find(key);
|
|
|
|
if (container.end() == it)
|
|
|
|
it = container.emplace(key, std::make_unique<Object>()).first;
|
2016-08-25 21:47:45 +00:00
|
|
|
|
2017-04-01 07:20:54 +00:00
|
|
|
return it->second->get(std::forward<Factory>(f));
|
|
|
|
}
|
2016-08-25 21:47:45 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
}
|