ReactivePlusPlus
One more implementation of ReactiveX approach in C++ with care about performance and templates in mind
 
Loading...
Searching...
No Matches
utils.hpp
1// ReactivePlusPlus library
2//
3// Copyright Aleksey Loginov 2022 - present.
4// Distributed under the Boost Software License, Version 1.0.
5// (See accompanying file LICENSE_1_0.txt or copy at
6// https://www.boost.org/LICENSE_1_0.txt)
7//
8// Project home: https://github.com/victimsnino/ReactivePlusPlus
9
10#pragma once
11
12#include <rpp/schedulers/constraints.hpp>
13#include <rpp/subscriptions/subscription_base.hpp>
14
15#include <thread>
16
17namespace rpp::schedulers::details
18{
19// keep old_timepoint to easily understand if we need to sleep (due to sleep is expensive enough even if time in the "past")
20inline thread_local time_point s_last_sleep_timepoint{};
21
26bool immediate_scheduling_while_condition(time_point& time_point,
27 constraint::schedulable_fn auto&& schedulable,
28 const subscription_base& sub,
29 const std::predicate auto& condition)
30{
31 while (condition())
32 {
33 if (!sub.is_subscribed())
34 return false;
35
36 if (s_last_sleep_timepoint < time_point)
37 {
38 std::this_thread::sleep_until(time_point);
39 s_last_sleep_timepoint = time_point;
40
41 if (!sub.is_subscribed())
42 return false;
43 }
44
45
46 if (const auto duration = schedulable())
47 time_point = time_point + duration.value();
48 else
49 return false;
50 }
51
52 return true;
53}
54}