Libosmium 2.22.0 Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
util.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_THREAD_UTIL_HPP
2#define OSMIUM_THREAD_UTIL_HPP
3
4/*
5
6 This file is part of Osmium (https://osmcode.org/libosmium).
7
8 Copyright 2013-2025 Jochen Topf <jochen@topf.org> and others (see README).
9
10 Boost Software License - Version 1.0 - August 17th, 2003
11
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS 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
47 namespace 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
91 class thread_handler {
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
104 thread_handler(const thread_handler&) = delete;
105 thread_handler& operator=(const thread_handler&) = delete;
106
107 thread_handler(thread_handler&&) noexcept = default;
108 thread_handler& operator=(thread_handler&&) noexcept = default;
109
110 ~thread_handler() {
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
osmium::thread::thread_handler
Definition util.hpp:91
osmium::thread::thread_handler::thread_handler
thread_handler(const thread_handler &)=delete
osmium::thread::thread_handler::thread_handler
thread_handler()=default
osmium::thread::thread_handler::operator=
thread_handler & operator=(const thread_handler &)=delete
osmium::thread::thread_handler::m_thread
std::thread m_thread
Definition util.hpp:93
osmium::thread::thread_handler::thread_handler
thread_handler(TFunction &&f, TArgs &&... args)
Definition util.hpp:100
osmium::thread::thread_handler::thread_handler
thread_handler(thread_handler &&) noexcept=default
osmium::thread::set_thread_name
void set_thread_name(const char *name) noexcept
Definition util.hpp:78
osmium::thread::check_for_exception
void check_for_exception(std::future< T > &future)
Definition util.hpp:57
osmium::thread::wait_until_done
void wait_until_done(std::future< T > &future)
Definition util.hpp:68
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
std
Definition location.hpp:555