File: threads_manager.hpp

package info (click to toggle)
dnf5 5.4.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,960 kB
  • sloc: cpp: 94,312; python: 3,370; xml: 1,073; ruby: 600; sql: 250; ansic: 232; sh: 104; perl: 62; makefile: 30
file content (227 lines) | stat: -rw-r--r-- 9,208 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
// Copyright Contributors to the DNF5 project.
// Copyright Contributors to the libdnf project.
// SPDX-License-Identifier: GPL-2.0-or-later
//
// This file is part of libdnf: https://github.com/rpm-software-management/libdnf/
//
// Libdnf is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 2 of the License, or
// (at your option) any later version.
//
// Libdnf is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with libdnf.  If not, see <https://www.gnu.org/licenses/>.

#ifndef DNF5DAEMON_SERVER_THREADS_MANAGER_HPP
#define DNF5DAEMON_SERVER_THREADS_MANAGER_HPP

#include "dbus.hpp"
#include "utils.hpp"

#include <fmt/format.h>
#include <locale.h>
#include <sdbus-c++/sdbus-c++.h>

#include <atomic>
#include <iostream>
#include <mutex>
#include <optional>
#include <string>
#include <thread>
#include <vector>

class ThreadsManager {
public:
    ThreadsManager();
    virtual ~ThreadsManager();
    void register_thread(std::thread && thread);
    void mark_thread_finished(std::thread::id thread_id);
    void current_thread_finished() { mark_thread_finished(std::this_thread::get_id()); };
    void join_threads(const bool only_finished);
    void finish();

    template <class S, bool UseLibdnf5Mutex = true>
    void handle_method(
        S & service,
        sdbus::MethodReply (S::*method)(sdbus::MethodCall &),
        sdbus::MethodCall & call,
        std::optional<std::string> thread_locale = std::nullopt) {
        auto worker = std::thread(
            [this](
                S & service,
                sdbus::MethodReply (S::*method)(sdbus::MethodCall &),
                sdbus::MethodCall call,
                std::optional<std::string> thread_locale = std::nullopt) {
                locale_t new_locale{nullptr};
                locale_t orig_locale{nullptr};
                if (thread_locale) {
                    orig_locale = set_thread_locale(thread_locale.value(), new_locale);
                }

                sdbus::MethodReply reply;
                try {
                    // Use template parameter to control libdnf5 mutex usage per method
                    // UseLibdnf5Mutex=true (default): Serialize D-Bus method calls that use libdnf5
                    // UseLibdnf5Mutex=false: Skip mutex for methods that don't need synchronization
                    std::optional<std::lock_guard<std::mutex>> libdnf5_lock;
                    if constexpr (UseLibdnf5Mutex) {
                        libdnf5_lock.emplace(service.get_session().get_libdnf5_mutex());
                    }
                    reply = (service.*method)(call);
                } catch (const sdbus::Error & ex) {
                    reply = call.createErrorReply(ex);
                } catch (const std::exception & ex) {
                    reply = call.createErrorReply(sdbus::Error(dnfdaemon::ERROR, ex.what()));
                } catch (...) {
                    reply = call.createErrorReply(sdbus::Error(dnfdaemon::ERROR, "Unknown exception caught"));
                }
                bool success = false;
                std::string error_msg;
                try {
                    reply.send();
                    success = true;
                } catch (const std::exception & e) {
                    error_msg = e.what();
                } catch (...) {
                    error_msg = "Unknown exception caught";
                }
                if (!success) {
                    std::cerr << fmt::format(
                                     "Error sending D-Bus reply to {}:{}() call: {}",
                                     call.getInterfaceName(),
                                     call.getMemberName(),
                                     error_msg)
                              << std::endl;
                }

                if (thread_locale) {
                    uselocale(orig_locale);
                    freelocale(new_locale);
                }
                current_thread_finished();
            },
            std::ref(service),
            method,
            call,
            thread_locale);
        register_thread(std::move(worker));
    }

    template <class S, bool UseLibdnf5Mutex = true>
    void handle_method_fd(
        S & service,
        void (S::*method)(sdbus::MethodCall &, const std::string &),
        sdbus::MethodCall & call,
        std::optional<std::string> thread_locale = std::nullopt) {
        auto worker = std::thread(
            [this](
                S & service,
                void (S::*method)(sdbus::MethodCall &, const std::string &),
                sdbus::MethodCall call,
                std::optional<std::string> thread_locale = std::nullopt) {
                static unsigned int counter{0};
                locale_t new_locale{nullptr};
                locale_t orig_locale{nullptr};
                if (thread_locale) {
                    orig_locale = set_thread_locale(thread_locale.value(), new_locale);
                }

                sdbus::MethodReply reply = call.createReply();
                // generate unique transfer id based on client bus name and counter
                const std::string transfer_id = fmt::format("{}-{}", call.getSender(), ++counter);
                reply << transfer_id;

                std::string error_msg;
                try {
                    reply.send();
                } catch (const std::exception & e) {
                    error_msg = e.what();
                } catch (...) {
                    error_msg = "Unknown exception caught";
                }

                if (error_msg.empty()) {
                    try {
                        // Use template parameter to control libdnf5 mutex usage per method
                        // UseLibdnf5Mutex=true (default): Serialize D-Bus method calls that use libdnf5
                        // UseLibdnf5Mutex=false: Skip mutex for methods that don't need synchronization
                        std::optional<std::lock_guard<std::mutex>> libdnf5_lock;
                        if constexpr (UseLibdnf5Mutex) {
                            libdnf5_lock.emplace(service.get_session().get_libdnf5_mutex());
                        }
                        (service.*method)(call, transfer_id);
                    } catch (...) {
                        // TODO(mblaha): log the error
                    }
                } else {
                    std::cerr << fmt::format(
                                     "Error sending D-Bus reply to {}:{}() call: {}",
                                     call.getInterfaceName(),
                                     call.getMemberName(),
                                     error_msg)
                              << std::endl;
                }

                if (thread_locale) {
                    uselocale(orig_locale);
                    freelocale(new_locale);
                }

                current_thread_finished();
            },
            std::ref(service),
            method,
            call,
            thread_locale);
        register_thread(std::move(worker));
    }

    template <class S>
    void handle_signal(S & service, void (S::*method)(sdbus::Signal &), sdbus::Signal & signal) {
        auto worker = std::thread(
            [this](S & service, void (S::*method)(sdbus::Signal &), sdbus::Signal signal) {
                bool success = false;
                std::string error_msg;
                try {
                    (service.*method)(signal);
                    success = true;
                } catch (const std::exception & ex) {
                    error_msg = ex.what();
                } catch (...) {
                    error_msg = "Unknown exception caught";
                }
                if (!success) {
                    std::cerr << fmt::format(
                                     "Error handling signal {}:{}: {}",
                                     signal.getInterfaceName(),
                                     signal.getMemberName(),
                                     error_msg)
                              << std::endl;
                }
                current_thread_finished();
            },
            std::ref(service),
            method,
            signal);
        register_thread(std::move(worker));
    }

private:
    std::mutex running_threads_mutex;
    // flag whether to break the finished threads collector infinite loop
    std::atomic<bool> finish_collector{false};
    // thread that joins finished worker threads
    std::thread running_threads_collector;
    // vector of started worker threads
    std::vector<std::thread> running_threads{};
    // vector of finished threads id
    std::vector<std::thread::id> finished_threads{};
    static locale_t set_thread_locale(const std::string & thread_locale, locale_t & new_locale);
};

#endif