blob: 10d454a4f0ed443e3ad02c44ff2b646fc1754ba8 [file] [log] [blame]
Adrien BĂ©raud612b55b2023-05-29 10:42:04 -04001#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
12namespace jami {
13namespace upnp {
14
15class UpnpThreadUtil
16{
17protected:
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