Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <thread> |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 4 | #include <memory> |
| 5 | #include <asio/io_context.hpp> |
Adrien Béraud | 25c30c4 | 2023-07-05 13:46:54 -0400 | [diff] [blame] | 6 | #include <fmt/format.h> |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 7 | |
| 8 | // This macro is used to validate that a code is executed from the expected |
| 9 | // thread. It's useful to detect unexpected race on data members. |
| 10 | #define CHECK_VALID_THREAD() \ |
| 11 | if (not isValidThread()) \ |
Adrien Béraud | 25c30c4 | 2023-07-05 13:46:54 -0400 | [diff] [blame] | 12 | 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] | 13 | /*JAMI_ERR() << "The calling thread " << getCurrentThread() \ |
| 14 | << " is not the expected thread: " << threadId_;*/ |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 15 | |
Adrien Béraud | 1ae60aa | 2023-07-07 09:55:09 -0400 | [diff] [blame] | 16 | namespace dhtnet { |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 17 | namespace upnp { |
| 18 | |
| 19 | class UpnpThreadUtil |
| 20 | { |
| 21 | protected: |
| 22 | std::thread::id getCurrentThread() const { return std::this_thread::get_id(); } |
| 23 | |
| 24 | bool isValidThread() const { return threadId_ == getCurrentThread(); } |
| 25 | |
| 26 | // Upnp context execution queue (same as manager's scheduler) |
| 27 | // Helpers to run tasks on upnp context queue. |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 28 | //static ScheduledExecutor* getScheduler() { return &Manager::instance().scheduler(); } |
| 29 | |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 30 | template<typename Callback> |
| 31 | static void runOnUpnpContextQueue(Callback&& cb) |
| 32 | { |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 33 | //getScheduler()->run([cb = std::forward<Callback>(cb)]() mutable { cb(); }); |
| 34 | //ioContext->post(std::move(cb)); |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 35 | } |
| 36 | |
Morteza Namvar | 5f63952 | 2023-07-04 17:08:58 -0400 | [diff] [blame] | 37 | std::shared_ptr<asio::io_context> ioContext; |
Adrien Béraud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 38 | std::thread::id threadId_; |
| 39 | }; |
| 40 | |
| 41 | } // namespace upnp |
| 42 | } // namespace jami |