1 #ifndef OSMIUM_IO_DETAIL_WRITE_THREAD_HPP 2 #define OSMIUM_IO_DETAIL_WRITE_THREAD_HPP 3 4 /* 5 6 This file is part of Osmium (https://osmcode.org/libosmium). 7 8 Copyright 2013-2021 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 <osmium/io/compression.hpp> 37 #include <osmium/io/detail/queue_util.hpp> 38 #include <osmium/thread/util.hpp> 39 40 #include <exception> 41 #include <future> 42 #include <memory> 43 #include <string> 44 #include <utility> 45 46 namespace osmium { 47 48 namespace io { 49 50 namespace detail { 51 52 /** 53 * This codes runs in its own thread, getting data from the given 54 * queue, (optionally) compressing it, and writing it to the output 55 * file. 56 */ 57 class WriteThread { 58 59 queue_wrapper<std::string> m_queue; 60 std::unique_ptr<osmium::io::Compressor> m_compressor; 61 std::promise<std::size_t> m_promise; 62 std::atomic_bool* m_notification; 63 64 public: 65 WriteThread(future_string_queue_type & input_queue,std::unique_ptr<osmium::io::Compressor> && compressor,std::promise<std::size_t> && promise,std::atomic_bool * notification)66 WriteThread(future_string_queue_type& input_queue, 67 std::unique_ptr<osmium::io::Compressor>&& compressor, 68 std::promise<std::size_t>&& promise, 69 std::atomic_bool* notification) : 70 m_queue(input_queue), 71 m_compressor(std::move(compressor)), 72 m_promise(std::move(promise)), 73 m_notification(notification) { 74 } 75 76 WriteThread(const WriteThread&) = delete; 77 WriteThread& operator=(const WriteThread&) = delete; 78 79 WriteThread(WriteThread&&) = delete; 80 WriteThread& operator=(WriteThread&&) = delete; 81 82 ~WriteThread() noexcept = default; 83 operator ()()84 void operator()() { 85 osmium::thread::set_thread_name("_osmium_write"); 86 87 try { 88 while (true) { 89 const std::string data{m_queue.pop()}; 90 if (at_end_of_data(data)) { 91 break; 92 } 93 m_compressor->write(data); 94 } 95 m_compressor->close(); 96 m_promise.set_value(m_compressor->file_size()); 97 } catch (...) { 98 m_notification->store(true); 99 m_promise.set_exception(std::current_exception()); 100 m_queue.drain(); 101 } 102 } 103 104 }; // class WriteThread 105 106 } // namespace detail 107 108 } // namespace io 109 110 } // namespace osmium 111 112 #endif // OSMIUM_IO_DETAIL_WRITE_THREAD_HPP 113