blob: 6c7f97b935f4fb4f26f56940cf034e97e596b966 [file] [log] [blame]
Adrien Béraud612b55b2023-05-29 10:42:04 -04001#pragma once
2
3#include <thread>
Morteza Namvar5f639522023-07-04 17:08:58 -04004#include <memory>
5#include <asio/io_context.hpp>
Adrien Béraud612b55b2023-05-29 10:42:04 -04006
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 Namvar5f639522023-07-04 17:08:58 -040011 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éraud612b55b2023-05-29 10:42:04 -040014
15namespace jami {
16namespace upnp {
17
18class UpnpThreadUtil
19{
20protected:
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 Namvar5f639522023-07-04 17:08:58 -040027 //static ScheduledExecutor* getScheduler() { return &Manager::instance().scheduler(); }
28
Adrien Béraud612b55b2023-05-29 10:42:04 -040029 template<typename Callback>
30 static void runOnUpnpContextQueue(Callback&& cb)
31 {
Morteza Namvar5f639522023-07-04 17:08:58 -040032 //getScheduler()->run([cb = std::forward<Callback>(cb)]() mutable { cb(); });
33 //ioContext->post(std::move(cb));
Adrien Béraud612b55b2023-05-29 10:42:04 -040034 }
35
Morteza Namvar5f639522023-07-04 17:08:58 -040036 std::shared_ptr<asio::io_context> ioContext;
Adrien Béraud612b55b2023-05-29 10:42:04 -040037 std::thread::id threadId_;
38};
39
40} // namespace upnp
41} // namespace jami