Merge commit '879f7eb04200d7d2c28af565229bf6e3d54274fd' into retry/libosmium

This commit is contained in:
karenzshea
2016-10-03 13:08:59 -04:00
208 changed files with 11590 additions and 4051 deletions
+13 -14
View File
@@ -34,9 +34,7 @@ DEALINGS IN THE SOFTWARE.
*/
#include <algorithm>
#include <atomic>
#include <cstddef>
#include <cstdlib>
#include <future>
#include <thread>
#include <type_traits>
@@ -66,7 +64,7 @@ namespace osmium {
}
if (num_threads < 0) {
num_threads += hardware_concurrency;
num_threads += int(hardware_concurrency);
}
if (num_threads < 1) {
@@ -78,6 +76,11 @@ namespace osmium {
return num_threads;
}
inline size_t get_work_queue_size() noexcept {
const size_t n = osmium::config::get_max_queue_size("WORK", 10);
return n > 2 ? n : 2;
}
} // namespace detail
/**
@@ -118,13 +121,11 @@ namespace osmium {
osmium::thread::set_thread_name("_osmium_worker");
while (true) {
function_wrapper task;
m_work_queue.wait_and_pop_with_timeout(task);
if (task) {
if (task()) {
// The called tasks returns true only when the
// worker thread should shut down.
return;
}
m_work_queue.wait_and_pop(task);
if (task && task()) {
// The called tasks returns true only when the
// worker thread should shut down.
return;
}
}
}
@@ -160,10 +161,9 @@ 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, max_work_queue_size);
static Pool pool(default_num_threads, detail::get_work_queue_size());
return pool;
}
@@ -176,7 +176,6 @@ namespace osmium {
~Pool() {
shutdown_all_workers();
m_work_queue.shutdown();
}
size_t queue_size() const {
@@ -190,7 +189,7 @@ namespace osmium {
template <typename TFunction>
std::future<typename std::result_of<TFunction()>::type> submit(TFunction&& func) {
typedef typename std::result_of<TFunction()>::type result_type;
using result_type = typename std::result_of<TFunction()>::type;
std::packaged_task<result_type()> task(std::forward<TFunction>(func));
std::future<result_type> future_result(task.get_future());
+36 -28
View File
@@ -33,7 +33,6 @@ DEALINGS IN THE SOFTWARE.
*/
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <cstddef>
@@ -41,7 +40,12 @@ DEALINGS IN THE SOFTWARE.
#include <queue>
#include <string>
#include <thread>
#include <utility> // IWYU pragma: keep (for std::move)
#include <utility> // IWYU pragma: keep
#ifdef OSMIUM_DEBUG_QUEUE_SIZE
# include <atomic>
# include <iostream>
#endif
namespace osmium {
@@ -69,8 +73,6 @@ namespace osmium {
/// Used to signal readers when data is available in the queue.
std::condition_variable m_data_available;
std::atomic<bool> m_done;
#ifdef OSMIUM_DEBUG_QUEUE_SIZE
/// The largest size the queue has been so far.
size_t m_largest_size;
@@ -81,6 +83,16 @@ namespace osmium {
/// The number of times the queue was full and a thread pushing
/// to the queue was blocked.
std::atomic<int> m_full_counter;
/**
* The number of times wait_and_pop(with_timeout)() was called
* on the queue.
*/
std::atomic<int> m_pop_counter;
/// The number of times the queue was full and a thread pushing
/// to the queue was blocked.
std::atomic<int> m_empty_counter;
#endif
public:
@@ -97,21 +109,21 @@ namespace osmium {
m_name(name),
m_mutex(),
m_queue(),
m_data_available(),
m_done(false)
m_data_available()
#ifdef OSMIUM_DEBUG_QUEUE_SIZE
,
m_largest_size(0),
m_push_counter(0),
m_full_counter(0)
m_full_counter(0),
m_pop_counter(0),
m_empty_counter(0)
#endif
{
}
~Queue() {
shutdown();
#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 in " << m_push_counter << " push() calls\n";
std::cerr << "queue '" << m_name << "' with max_size=" << m_max_size << " had largest size " << m_largest_size << " and was full " << m_full_counter << " times in " << m_push_counter << " push() calls and was empty " << m_empty_counter << " times in " << m_pop_counter << " pop() calls\n";
#endif
}
@@ -141,15 +153,18 @@ namespace osmium {
m_data_available.notify_one();
}
void shutdown() {
m_done = true;
m_data_available.notify_all();
}
void wait_and_pop(T& value) {
#ifdef OSMIUM_DEBUG_QUEUE_SIZE
++m_pop_counter;
#endif
std::unique_lock<std::mutex> lock(m_mutex);
#ifdef OSMIUM_DEBUG_QUEUE_SIZE
if (m_queue.empty()) {
++m_empty_counter;
}
#endif
m_data_available.wait(lock, [this] {
return !m_queue.empty() || m_done;
return !m_queue.empty();
});
if (!m_queue.empty()) {
value = std::move(m_queue.front());
@@ -157,22 +172,15 @@ namespace osmium {
}
}
void wait_and_pop_with_timeout(T& value) {
std::unique_lock<std::mutex> lock(m_mutex);
if (!m_data_available.wait_for(lock, std::chrono::seconds(1), [this] {
return !m_queue.empty() || m_done;
})) {
return;
}
if (!m_queue.empty()) {
value = std::move(m_queue.front());
m_queue.pop();
}
}
bool try_pop(T& value) {
#ifdef OSMIUM_DEBUG_QUEUE_SIZE
++m_pop_counter;
#endif
std::lock_guard<std::mutex> lock(m_mutex);
if (m_queue.empty()) {
#ifdef OSMIUM_DEBUG_QUEUE_SIZE
++m_empty_counter;
#endif
return false;
}
value = std::move(m_queue.front());
@@ -1,159 +0,0 @@
#ifndef OSMIUM_THREAD_SORTED_QUEUE_HPP
#define OSMIUM_THREAD_SORTED_QUEUE_HPP
/*
This file is part of Osmium (http://osmcode.org/libosmium).
Copyright 2013-2016 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 <condition_variable>
#include <cstddef>
#include <deque>
#include <mutex>
namespace osmium {
namespace thread {
/**
* This implements a sorted queue. It is a bit like a priority
* queue. We have n worker threads pushing items into the queue
* and one thread pulling them out again "in order". The order
* is defined by the monotonically increasing "num" parameter
* to the push() method. The wait_and_pop() and try_pop() methods
* will only give out the next numbered item. This way several
* workers can work in their own time on different pieces of
* some incoming data, but it all gets serialized properly again
* after the workers have done their work.
*/
template <typename T>
class SortedQueue {
typedef typename std::deque<T>::size_type size_type;
mutable std::mutex m_mutex;
std::deque<T> m_queue;
std::condition_variable m_data_available;
size_type m_offset;
// this method expects that we already have the lock
bool empty_intern() const {
return m_queue.front() == T();
}
public:
SortedQueue() :
m_mutex(),
m_queue(1),
m_data_available(),
m_offset(0) {
}
/**
* Push an item into the queue.
*
* @param value The item to push into the queue.
* @param num Number to describe ordering for the items.
* It must increase monotonically.
*/
void push(T value, size_type num) {
std::lock_guard<std::mutex> lock(m_mutex);
num -= m_offset;
if (m_queue.size() <= num + 1) {
m_queue.resize(num + 2);
}
m_queue[num] = std::move(value);
m_data_available.notify_one();
}
/**
* Wait until the next item becomes available and make it
* available through value.
*/
void wait_and_pop(T& value) {
std::unique_lock<std::mutex> lock(m_mutex);
m_data_available.wait(lock, [this] {
return !empty_intern();
});
value = std::move(m_queue.front());
m_queue.pop_front();
++m_offset;
}
/**
* Get next item if it is available and return true. Or
* return false otherwise.
*/
bool try_pop(T& value) {
std::lock_guard<std::mutex> lock(m_mutex);
if (empty_intern()) {
return false;
}
value = std::move(m_queue.front());
m_queue.pop_front();
++m_offset;
return true;
}
/**
* The queue is empty. This means try_pop() would fail if called.
* It does not mean that there is nothing on the queue. Because
* the queue is sorted, it could mean that the next item in the
* queue is not available, but other items are.
*/
bool empty() const {
std::lock_guard<std::mutex> lock(m_mutex);
return empty_intern();
}
/**
* Returns the number of items in the queue, regardless of whether
* they can be accessed. If this is =0 it
* implies empty()==true, but not the other way around.
*/
size_t size() const {
std::lock_guard<std::mutex> lock(m_mutex);
return m_queue.size();
}
}; // class SortedQueue
} // namespace thread
} // namespace osmium
#endif // OSMIUM_THREAD_SORTED_QUEUE_HPP
+2
View File
@@ -35,6 +35,8 @@ DEALINGS IN THE SOFTWARE.
#include <chrono>
#include <future>
#include <thread>
#include <utility>
#ifdef __linux__
# include <sys/prctl.h>