blob: 564a6ebd23912722d031fed7e207c74dc53078a5 [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éraud25c30c42023-07-05 13:46:54 -04006#include <fmt/format.h>
Adrien Béraud612b55b2023-05-29 10:42:04 -04007
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éraud25c30c42023-07-05 13:46:54 -040012 fmt::print("The calling thread {} is not the expected thread: {}\n", getCurrentThread(), threadId_);
Morteza Namvar5f639522023-07-04 17:08:58 -040013 /*JAMI_ERR() << "The calling thread " << getCurrentThread() \
14 << " is not the expected thread: " << threadId_;*/
Adrien Béraud612b55b2023-05-29 10:42:04 -040015
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040016namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040017namespace upnp {
18
19class UpnpThreadUtil
20{
21protected:
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 Namvar5f639522023-07-04 17:08:58 -040028 //static ScheduledExecutor* getScheduler() { return &Manager::instance().scheduler(); }
29
Adrien Béraud612b55b2023-05-29 10:42:04 -040030 template<typename Callback>
31 static void runOnUpnpContextQueue(Callback&& cb)
32 {
Morteza Namvar5f639522023-07-04 17:08:58 -040033 //getScheduler()->run([cb = std::forward<Callback>(cb)]() mutable { cb(); });
34 //ioContext->post(std::move(cb));
Adrien Béraud612b55b2023-05-29 10:42:04 -040035 }
36
Morteza Namvar5f639522023-07-04 17:08:58 -040037 std::shared_ptr<asio::io_context> ioContext;
Adrien Béraud612b55b2023-05-29 10:42:04 -040038 std::thread::id threadId_;
39};
40
41} // namespace upnp
42} // namespace jami