Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Qt scheduler #292

Merged
merged 9 commits into from
Nov 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CMakePresets.json
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,9 @@
{
"name": "ci-tests",
"configuration": "Release",
"environment": {
"QT_QPA_PLATFORM":"offscreen"
},
"output": {
"outputOnFailure": true
},
Expand Down
3 changes: 2 additions & 1 deletion cmake/dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ endif()

# ==================== QT ==========================
if (RPP_BUILD_QT_CODE AND (RPP_BUILD_TESTS OR RPP_BUILD_EXAMPLES))
find_package(Qt6 COMPONENTS Widgets)
find_package(Qt6 COMPONENTS Widgets QUIET)
if (Qt6_FOUND)
SET(RPP_QT_TARGET Qt6)
else()
message("-- RPP: Can't find Qt6, searching for Qt5...")
find_package(Qt5 REQUIRED COMPONENTS Widgets)
SET(RPP_QT_TARGET Qt5)
endif()
Expand Down
1 change: 0 additions & 1 deletion src/rpp/rpp/schedulers/immediate_scheduler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@

#include <chrono>
#include <concepts>
#include <thread>

namespace rpp::schedulers
{
Expand Down
10 changes: 9 additions & 1 deletion src/rppqt/rppqt/fwd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,12 @@
* \ingroup rppqt
*/

#include <rppqt/sources/fwd.hpp>
#include <rppqt/sources/fwd.hpp>

/**
* \defgroup schedulers Schedulers
* \brief Scheduler is the way to introduce multi-threading in your application via RPP
* \see https://reactivex.io/documentation/scheduler.html
* \ingroup rpp
*/
#include <rppqt/schedulers/fwd.hpp>
3 changes: 2 additions & 1 deletion src/rppqt/rppqt/rppqt.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@
#pragma once

#include <rppqt/fwd.hpp>
#include <rppqt/sources.hpp>
#include <rppqt/sources.hpp>
#include <rppqt/schedulers.hpp>
20 changes: 20 additions & 0 deletions src/rppqt/rppqt/schedulers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// ReactivePlusPlus library
//
// Copyright Aleksey Loginov 2022 - present.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/victimsnino/ReactivePlusPlus
//

#pragma once

/**
* \defgroup qt_schedulers QT Schedulers
* \brief Scheduler is the way to introduce multi-threading in your application via RPP
* \see https://reactivex.io/documentation/scheduler.html
* \ingroup rppqt
*/

#include <rppqt/schedulers/main_thread_scheduler.hpp>
16 changes: 16 additions & 0 deletions src/rppqt/rppqt/schedulers/fwd.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// ReactivePlusPlus library
//
// Copyright Aleksey Loginov 2022 - present.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/victimsnino/ReactivePlusPlus
//

#pragma once

namespace rppqt::schedulers
{
class main_thread_scheduler;
} // namespace rppqt::schedulers
74 changes: 74 additions & 0 deletions src/rppqt/rppqt/schedulers/main_thread_scheduler.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// ReactivePlusPlus library
//
// Copyright Aleksey Loginov 2022 - present.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/victimsnino/ReactivePlusPlus
//

#pragma once

#include <rppqt/schedulers/fwd.hpp> // own forwarding
#include <rpp/schedulers/details/worker.hpp> // worker
#include <rpp/subscriptions/subscription_base.hpp> // lifetime
#include <rppqt/utils/exceptions.hpp>

#include <chrono>
#include <concepts>

#include <QCoreApplication>
#include <QTimer>

namespace rppqt::schedulers
{
/**
* \brief Schedule provided schedulables to main GUI QT thread (where QApplication placed)
* \ingroup qt_schedulers
*/
class main_thread_scheduler final : public rpp::schedulers::details::scheduler_tag
{
private:
class worker_strategy;
using main_thread_schedulable = rpp::schedulers::schedulable_wrapper<worker_strategy>;

class worker_strategy
{
public:
worker_strategy(const rpp::subscription_base& sub)
: m_sub{sub} {}

bool is_subscribed() const { return m_sub.is_subscribed(); }

void defer_at(rpp::schedulers::time_point time_point, rpp::schedulers::constraint::schedulable_fn auto&& fn) const
{
defer_at(time_point, main_thread_schedulable{*this, time_point, std::forward<decltype(fn)>(fn)});
}

void defer_at(rpp::schedulers::time_point time_point, main_thread_schedulable&& fn) const
{
if (!m_sub.is_subscribed())
return;

const auto application = QCoreApplication::instance();
if (!application)
throw utils::no_active_qapplication{
"Pointer to application is null. Create QApplication before using main_thread_scheduler!"};

const auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(now() - time_point).count();
QTimer::singleShot(duration, application, std::move(fn));
}

static rpp::schedulers::time_point now() { return rpp::schedulers::clock_type::now(); }
private:
rpp::subscription_base m_sub;
};

public:
static auto create_worker(const rpp::subscription_base& sub = {})
{
return rpp::schedulers::worker<worker_strategy>{sub};
}
};
} // namespace rppqt::schedulers
21 changes: 21 additions & 0 deletions src/rppqt/rppqt/utils/exceptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// ReactivePlusPlus library
//
// Copyright Aleksey Loginov 2022 - present.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/victimsnino/ReactivePlusPlus
//

#pragma once

#include <stdexcept>

namespace rppqt::utils
{
struct no_active_qapplication : std::runtime_error
{
using std::runtime_error::runtime_error;
};
}
66 changes: 66 additions & 0 deletions src/tests/rppqt/test_main_thread_scheduler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// ReactivePlusPlus library
//
// Copyright Aleksey Loginov 2022 - present.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt)
//
// Project home: https://github.com/victimsnino/ReactivePlusPlus

#include <catch2/catch_test_macros.hpp>
#include <rpp/subscriptions/composite_subscription.hpp>

#include <rppqt/schedulers/main_thread_scheduler.hpp>

#include <future>
#include <QApplication>

SCENARIO("main_thread_scheduler schedules actions to main thread", "[schedulers]")
{
GIVEN("qapplication and scheduler")
{
int argc{};
QCoreApplication application{argc, nullptr};
WHEN("submitting action to main scheduler from another thread")
{
std::promise<std::thread::id> execution_thread{};
std::thread{[&]
{
rppqt::schedulers::main_thread_scheduler{}.create_worker().schedule([&]()->rpp::schedulers::optional_duration
{
execution_thread.set_value(std::this_thread::get_id());
return {};
});
}}.join();
QTimer::singleShot(10, &application, [&]{application.exit();});
application.exec();
THEN("thread of exectuion of schedulable should be same as thread of application")
{
auto future = execution_thread.get_future();
REQUIRE(future.wait_for(std::chrono::seconds{3}) == std::future_status::ready);
CHECK(future.get() == std::this_thread::get_id());
}
}
WHEN("submitting action to main scheduler from another thread but with unsubscribed subscription after schedule")
{
std::promise<std::thread::id> execution_thread{};
std::thread{[&]
{
rpp::composite_subscription sub{};
rppqt::schedulers::main_thread_scheduler{}.create_worker(sub).schedule([&]()->rpp::schedulers::optional_duration
{
execution_thread.set_value(std::this_thread::get_id());
return {};
});
sub.unsubscribe();
}}.join();
QTimer::singleShot(10, &application, [&]{application.exit();});
application.exec();
THEN("no schedule execution")
{
auto future = execution_thread.get_future();
REQUIRE(future.wait_for(std::chrono::seconds{1}) == std::future_status::timeout);
}
}
}
}