Adrien Béraud | cb75362 | 2023-07-17 22:32:49 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2023 Savoir-faire Linux Inc. |
| 3 | * |
| 4 | * This program is free software: you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation, either version 3 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 16 | */ |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 17 | #pragma once |
| 18 | |
| 19 | #include <thread> |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <asio/io_context.hpp> |
Adrien Béraud | 25c30c4 | 2023-07-05 13:46:54 -0400 | [diff] [blame] | 22 | #include <fmt/format.h> |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 23 | |
| 24 | // This macro is used to validate that a code is executed from the expected |
| 25 | // thread. It's useful to detect unexpected race on data members. |
| 26 | #define CHECK_VALID_THREAD() \ |
| 27 | if (not isValidThread()) \ |
Adrien Béraud | 25c30c4 | 2023-07-05 13:46:54 -0400 | [diff] [blame] | 28 | fmt::print("The calling thread {} is not the expected thread: {}\n", getCurrentThread(), threadId_); |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 29 | /*JAMI_ERR() << "The calling thread " << getCurrentThread() \ |
| 30 | << " is not the expected thread: " << threadId_;*/ |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 31 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 32 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 33 | namespace upnp { |
| 34 | |
| 35 | class UpnpThreadUtil |
| 36 | { |
| 37 | protected: |
| 38 | std::thread::id getCurrentThread() const { return std::this_thread::get_id(); } |
| 39 | |
| 40 | bool isValidThread() const { return threadId_ == getCurrentThread(); } |
| 41 | |
| 42 | // Upnp context execution queue (same as manager's scheduler) |
| 43 | // Helpers to run tasks on upnp context queue. |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 44 | //static ScheduledExecutor* getScheduler() { return &Manager::instance().scheduler(); } |
| 45 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 46 | template<typename Callback> |
| 47 | static void runOnUpnpContextQueue(Callback&& cb) |
| 48 | { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 49 | //getScheduler()->run([cb = std::forward<Callback>(cb)]() mutable { cb(); }); |
| 50 | //ioContext->post(std::move(cb)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 51 | } |
| 52 | |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 53 | std::shared_ptr<asio::io_context> ioContext; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 54 | std::thread::id threadId_; |
| 55 | }; |
| 56 | |
| 57 | } // namespace upnp |
| 58 | } // namespace jami |