Libosmium  2.20.0
Fast and flexible C++ library for working with OpenStreetMap data
util.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_THREAD_UTIL_HPP
2#define OSMIUM_THREAD_UTIL_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2023 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <chrono>
37#include <future>
38#include <thread>
39#include <utility>
40
41#if defined(__linux__)
42# include <sys/prctl.h>
43#elif defined(__FreeBSD__)
44# include <pthread.h>
45#endif
46
47namespace osmium {
48
49 namespace thread {
50
56 template <typename T>
57 inline void check_for_exception(std::future<T>& future) {
58 if (future.valid() && future.wait_for(std::chrono::seconds(0)) == std::future_status::ready) {
59 future.get();
60 }
61 }
62
67 template <typename T>
68 inline void wait_until_done(std::future<T>& future) {
69 if (future.valid()) {
70 future.get();
71 }
72 }
73
77#if defined(__linux__)
78 inline void set_thread_name(const char* name) noexcept {
79 prctl(PR_SET_NAME, name, 0, 0, 0);
80 }
81#elif defined(__FreeBSD__)
82 inline void set_thread_name(const char* name) noexcept {
83 pthread_setname_np(pthread_self(), name);
84 }
85#else
86 inline void set_thread_name(const char*) noexcept {
87 // intentionally left blank
88 }
89#endif
90
92
93 std::thread m_thread;
94
95 public:
96
97 thread_handler() = default;
98
99 template <typename TFunction, typename... TArgs>
100 explicit thread_handler(TFunction&& f, TArgs&&... args) :
101 m_thread(std::forward<TFunction>(f), std::forward<TArgs>(args)...) {
102 }
103
106
107 thread_handler(thread_handler&&) noexcept = default;
108 thread_handler& operator=(thread_handler&&) noexcept = default;
109
111 if (m_thread.joinable()) {
112 m_thread.join();
113 }
114 }
115
116 }; // class thread_handler
117
118 } // namespace thread
119
120} // namespace osmium
121
122#endif // OSMIUM_THREAD_UTIL_HPP
Definition: util.hpp:91
thread_handler(const thread_handler &)=delete
thread_handler & operator=(const thread_handler &)=delete
std::thread m_thread
Definition: util.hpp:93
thread_handler(TFunction &&f, TArgs &&... args)
Definition: util.hpp:100
thread_handler(thread_handler &&) noexcept=default
void set_thread_name(const char *name) noexcept
Definition: util.hpp:78
void check_for_exception(std::future< T > &future)
Definition: util.hpp:57
void wait_until_done(std::future< T > &future)
Definition: util.hpp:68
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: location.hpp:555