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