bump osmium version

This commit is contained in:
Dennis Luxen
2014-12-17 11:19:08 +01:00
parent 40ff7ad999
commit 3dddd16ec7
47 changed files with 2620 additions and 2251 deletions
+9 -3
View File
@@ -40,12 +40,18 @@ namespace osmium {
namespace thread {
/**
* This function wrapper can collect move-only functions unlike
* std::function which needs copyable functions.
* Taken from the book "C++ Concurrency in Action".
*/
class function_wrapper {
struct impl_base {
virtual ~impl_base() = default;
virtual void call() = 0;
virtual ~impl_base() {
}
}; // struct impl_base
std::unique_ptr<impl_base> impl;
@@ -58,7 +64,7 @@ namespace osmium {
m_functor(std::move(functor)) {
}
void call() {
void call() override {
m_functor();
}
}; // struct impl_type
+12 -36
View File
@@ -42,9 +42,10 @@ DEALINGS IN THE SOFTWARE.
#include <type_traits>
#include <vector>
#include <osmium/thread/queue.hpp>
#include <osmium/thread/name.hpp>
#include <osmium/thread/function_wrapper.hpp>
#include <osmium/thread/queue.hpp>
#include <osmium/thread/util.hpp>
#include <osmium/util/config.hpp>
namespace osmium {
@@ -58,7 +59,10 @@ namespace osmium {
*/
class Pool {
// This class makes sure pool threads are joined when the pool is destructed
/**
* This class makes sure all pool threads will be joined when
* the pool is destructed.
*/
class thread_joiner {
std::vector<std::thread>& m_threads;
@@ -108,20 +112,15 @@ namespace osmium {
*
* In all cases the minimum number of threads in the pool is 1.
*/
explicit Pool(int num_threads) :
explicit Pool(int num_threads, size_t max_queue_size) :
m_done(false),
m_work_queue(),
m_work_queue(max_queue_size, "work"),
m_threads(),
m_joiner(m_threads),
m_num_threads(num_threads) {
if (m_num_threads == 0) {
const char* env_threads = getenv("OSMIUM_POOL_THREADS");
if (env_threads) {
m_num_threads = std::atoi(env_threads);
} else {
m_num_threads = -2;
}
m_num_threads = osmium::config::get_pool_threads();
}
if (m_num_threads <= 0) {
@@ -141,9 +140,10 @@ namespace osmium {
public:
static constexpr int default_num_threads = 0;
static constexpr size_t max_work_queue_size = 10;
static Pool& instance() {
static Pool pool(default_num_threads);
static Pool pool(default_num_threads, max_work_queue_size);
return pool;
}
@@ -173,30 +173,6 @@ namespace osmium {
}; // class Pool
/**
* Wrapper for classes that can't be copied but need to be copyable for
* putting them in the pool.
*/
template <class TWrapped>
class SharedPtrWrapper {
std::shared_ptr<TWrapped> m_task;
public:
typedef typename std::result_of<TWrapped()>::type result_type;
template <typename... TArgs>
SharedPtrWrapper(TArgs&&... args) :
m_task(std::make_shared<TWrapped>(std::forward<TArgs>(args)...)) {
}
result_type operator()() {
return m_task->operator()();
}
}; // class SharedPtrWrapper
} // namespace thread
} // namespace osmium
+63 -13
View File
@@ -33,52 +33,102 @@ DEALINGS IN THE SOFTWARE.
*/
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstddef>
#include <mutex>
#include <queue>
#include <thread>
#include <utility>
namespace osmium {
namespace thread {
constexpr std::chrono::milliseconds full_queue_sleep_duration { 10 }; // XXX
/**
* A thread-safe queue.
*/
template <typename T>
class Queue {
/// Maximum size of this queue. If the queue is full pushing to
/// the queue will block.
const size_t m_max_size;
/// Name of this queue (for debugging only).
const std::string m_name;
mutable std::mutex m_mutex;
std::queue<T> m_queue;
/// Used to signal readers when data is available in the queue.
std::condition_variable m_data_available;
#ifdef OSMIUM_DEBUG_QUEUE_SIZE
/// The largest size the queue has been so far.
size_t m_largest_size;
/// The number of times the queue was full and a thread pushing
/// to the queue was blocked.
std::atomic<int> m_full_counter;
#endif
public:
Queue() :
/**
* Construct a multithreaded queue.
*
* @param max_size Maximum number of elements in the queue. Set to
* 0 for an unlimited size.
* @param name Optional name for this queue. (Used for debugging.)
*/
Queue(size_t max_size = 0, const std::string& name = "") :
m_max_size(max_size),
m_name(name),
m_mutex(),
m_queue(),
m_data_available() {
m_data_available()
#ifdef OSMIUM_DEBUG_QUEUE_SIZE
,
m_largest_size(0),
m_full_counter(0)
#endif
{
}
~Queue() {
#ifdef OSMIUM_DEBUG_QUEUE_SIZE
std::cerr << "queue '" << m_name << "' with max_size=" << m_max_size << " had largest size " << m_largest_size << " and was full " << m_full_counter << " times\n";
#endif
}
/**
* Push an element onto the queue. If the queue has a max size, this
* call will block if the queue is full.
*/
void push(T value) {
if (m_max_size) {
while (size() >= m_max_size) {
std::this_thread::sleep_for(full_queue_sleep_duration);
#ifdef OSMIUM_DEBUG_QUEUE_SIZE
++m_full_counter;
#endif
}
}
std::lock_guard<std::mutex> lock(m_mutex);
m_queue.push(std::move(value));
#ifdef OSMIUM_DEBUG_QUEUE_SIZE
if (m_largest_size < m_queue.size()) {
m_largest_size = m_queue.size();
}
#endif
m_data_available.notify_one();
}
size_t push_and_get_size(T&& value) {
std::lock_guard<std::mutex> lock(m_mutex);
m_queue.push(std::forward<T>(value));
m_data_available.notify_one();
return m_queue.size();
}
void push(T value, int) {
push(value);
}
void wait_and_pop(T& value) {
std::unique_lock<std::mutex> lock(m_mutex);
m_data_available.wait(lock, [this] {
+87
View File
@@ -0,0 +1,87 @@
#ifndef OSMIUM_THREAD_UTIL_HPP
#define OSMIUM_THREAD_UTIL_HPP
/*
This file is part of Osmium (http://osmcode.org/libosmium).
Copyright 2013,2014 Jochen Topf <jochen@topf.org> and others (see README).
Boost Software License - Version 1.0 - August 17th, 2003
Permission is hereby granted, free of charge, to any person or organization
obtaining a copy of the software and accompanying documentation covered by
this license (the "Software") to use, reproduce, display, distribute,
execute, and transmit the Software, and to prepare derivative works of the
Software, and to permit third-parties to whom the Software is furnished to
do so, all subject to the following:
The copyright notices in the Software and this entire statement, including
the above license grant, this restriction and the following disclaimer,
must be included in all copies of the Software, in whole or in part, and
all derivative works of the Software, unless such copies or derivative
works are solely in the form of machine-executable object code generated by
a source language processor.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <chrono>
#include <future>
#ifdef __linux__
# include <sys/prctl.h>
#endif
namespace osmium {
namespace thread {
/**
* Check if the future resulted in an exception. This will re-throw
* the exception stored in the future if there was one. Otherwise it
* will just return.
*/
template <class T>
inline void check_for_exception(std::future<T>& future) {
if (future.valid() && future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
future.get();
}
}
/**
* Wait until the given future becomes ready. Will block if the future
* is not ready. Can be called more than once unless future.get().
*/
template <class T>
inline void wait_until_done(std::future<T>& future) {
if (future.valid()) {
future.get();
}
}
/**
* Set name of current thread for debugging. This only works on Linux.
*/
#ifdef __linux__
inline void set_thread_name(const char* name) {
prctl(PR_SET_NAME, name, 0, 0, 0);
}
#else
inline void set_thread_name(const char*) {
// intentionally left blank
}
#endif
} // namespace thread
} // namespace osmium
#endif // OSMIUM_THREAD_UTIL_HPP