Adrien BĂ©raud | 612b55b | 2023-05-29 10:42:04 -0400 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | #include <thread> |
| 4 | |
| 5 | // This macro is used to validate that a code is executed from the expected |
| 6 | // thread. It's useful to detect unexpected race on data members. |
| 7 | #define CHECK_VALID_THREAD() \ |
| 8 | if (not isValidThread()) \ |
| 9 | JAMI_ERR() << "The calling thread " << getCurrentThread() \ |
| 10 | << " is not the expected thread: " << threadId_; |
| 11 | |
| 12 | namespace jami { |
| 13 | namespace upnp { |
| 14 | |
| 15 | class UpnpThreadUtil |
| 16 | { |
| 17 | protected: |
| 18 | std::thread::id getCurrentThread() const { return std::this_thread::get_id(); } |
| 19 | |
| 20 | bool isValidThread() const { return threadId_ == getCurrentThread(); } |
| 21 | |
| 22 | // Upnp context execution queue (same as manager's scheduler) |
| 23 | // Helpers to run tasks on upnp context queue. |
| 24 | static ScheduledExecutor* getScheduler() { return &Manager::instance().scheduler(); } |
| 25 | template<typename Callback> |
| 26 | static void runOnUpnpContextQueue(Callback&& cb) |
| 27 | { |
| 28 | getScheduler()->run([cb = std::forward<Callback>(cb)]() mutable { cb(); }); |
| 29 | } |
| 30 | |
| 31 | std::thread::id threadId_; |
| 32 | }; |
| 33 | |
| 34 | } // namespace upnp |
| 35 | } // namespace jami |