blob: 22b25028461ebad1baafbb303865f39e1173a771 [file] [log] [blame]
Adrien Béraudcb753622023-07-17 22:32:49 -04001/*
2 * Copyright (C) 2004-2023 Savoir-faire Linux Inc.
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
Adrien Béraud612b55b2023-05-29 10:42:04 -040017#pragma once
18
19#include <thread>
Morteza Namvar5f639522023-07-04 17:08:58 -040020#include <memory>
21#include <asio/io_context.hpp>
Adrien Béraud25c30c42023-07-05 13:46:54 -040022#include <fmt/format.h>
Adrien Béraud612b55b2023-05-29 10:42:04 -040023
24// This macro is used to validate that a code is executed from the expected
25// thread. It's useful to detect unexpected race on data members.
26#define CHECK_VALID_THREAD() \
27 if (not isValidThread()) \
Adrien Béraud25c30c42023-07-05 13:46:54 -040028 fmt::print("The calling thread {} is not the expected thread: {}\n", getCurrentThread(), threadId_);
Morteza Namvar5f639522023-07-04 17:08:58 -040029 /*JAMI_ERR() << "The calling thread " << getCurrentThread() \
30 << " is not the expected thread: " << threadId_;*/
Adrien Béraud612b55b2023-05-29 10:42:04 -040031
Adrien Béraud1ae60aa2023-07-07 09:55:09 -040032namespace dhtnet {
Adrien Béraud612b55b2023-05-29 10:42:04 -040033namespace upnp {
34
35class UpnpThreadUtil
36{
37protected:
38 std::thread::id getCurrentThread() const { return std::this_thread::get_id(); }
39
40 bool isValidThread() const { return threadId_ == getCurrentThread(); }
41
42 // Upnp context execution queue (same as manager's scheduler)
43 // Helpers to run tasks on upnp context queue.
Morteza Namvar5f639522023-07-04 17:08:58 -040044 //static ScheduledExecutor* getScheduler() { return &Manager::instance().scheduler(); }
Sébastien Blin464bdff2023-07-19 08:02:53 -040045
Adrien Béraud612b55b2023-05-29 10:42:04 -040046 template<typename Callback>
47 static void runOnUpnpContextQueue(Callback&& cb)
48 {
Morteza Namvar5f639522023-07-04 17:08:58 -040049 //getScheduler()->run([cb = std::forward<Callback>(cb)]() mutable { cb(); });
50 //ioContext->post(std::move(cb));
Adrien Béraud612b55b2023-05-29 10:42:04 -040051 }
52
Morteza Namvar5f639522023-07-04 17:08:58 -040053 std::shared_ptr<asio::io_context> ioContext;
Adrien Béraud612b55b2023-05-29 10:42:04 -040054 std::thread::id threadId_;
55};
56
57} // namespace upnp
Sébastien Blin464bdff2023-07-19 08:02:53 -040058} // namespace dhtnet