8000 Implement sleepFor() and sleepUntil() coroutines (#145) by danvratil · Pull Request #147 · qcoro/qcoro · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Implement sleepFor() and sleepUntil() coroutines (#145) #147

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

Merged
merged 1 commit into from
Jan 20, 2023
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
33 changes: 33 additions & 0 deletions docs/reference/core/qtimer.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,38 @@ QCoro::Task<> MyClass::pretendWork() {
}
```

## `QCoro::sleepFor()`

A simple coroutine that will suspend for the specified time duration. Can be quite
useful especially in unit-tests.

```cpp
template<typename Rep, Period>
QCoro::Task<> QCoro::sleepFor(const std::chrono::duration<Rep, Period> &timeout);
```

## `QCoro::sleepUntil()`

A simple coroutine that will suspend until the specified point in time. Can be useful
for scheduling tasks.

```cpp
template<typename Clock, typename Duration>
QCoro::Task< QCoro::sleepUntil(const std::chrono::time_point<Clock, Duration> &when);
```

Example:

```cpp
const auto now = std::chrono::system_clock::now();
const auto tomorrow_time = std::chrono::system_clock::to_time_t(now + 86400s);
std::tm *gt = std::gmtime(&tomorrow_time);
gt.tm_hour = 0;
gt.tm_min = 0;
gt.tm_sec = 0;
const auto tomorrow_midnight = std::mktime(&gt);
co_await QCoro::sleepUntil(std::chrono::system_clock::from_time_t(tomorrow_midnight));
```

[qdoc-qtimer]: https://doc.qt.io/qt-5/qtimer.html
[qdoc-qtimer-timeout]: https://doc.qt.io/qt-5/qtimer.html#timeout
20 changes: 20 additions & 0 deletions qcoro/core/qcorotimer.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,26 @@ struct awaiter_type<QTimer> {

} // namespace QCoro::detail

namespace QCoro {

//! A coroutine that suspends for given period of time.
template<typename Rep, typename Period>
QCoro::Task<> sleepFor(const std::chrono::duration<Rep, Period> &timeout) {
QTimer timer;
timer.setSingleShot(true);
timer.start(std::chrono::duration_cast<std::chrono::milliseconds>(timeout));
co_await timer;
}

//! A coroutine that suspends until the specified time.
template<typename Clock, typename Duration>
QCoro::Task<> sleepUntil(const std::chrono::time_point<Clock, Duration> &when) {
const auto tp = when.time_since_epoch() - std::chrono::steady_clock::now().time_since_epoch();
return sleepFor(tp);
}

} // namespace QCoro

/*! \endcond */

//! Returns a coroutine-friendly wrapper for QTimer object.
Expand Down
20 changes: 20 additions & 0 deletions tests/qtimer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

#include "qcorotimer.h"

#include <chrono>

using namespace std::chrono_literals;

class QCoroTimerTest : public QCoro::TestObject<QCoroTimerTest> {
Q_OBJECT

Expand Down Expand Up @@ -68,12 +72,28 @@ class QCoroTimerTest : public QCoro::TestObject<QCoroTimerTest> {
QVERIFY(triggered);
}

QCoro::Task<> testSleepFor_coro(QCoro::TestContext) {
QElapsedTimer elapsed;
elapsed.start();
co_await QCoro::sleepFor(100ms);
QCORO_VERIFY(elapsed.elapsed() >= 75);
}

QCoro::Task<> testSleepUntil_coro(QCoro::TestContext) {
QElapsedTimer elapsed;
elapsed.start();
co_await QCoro::sleepUntil(std::chrono::steady_clock::now() + 500ms);
QCORO_VERIFY(elapsed.elapsed() >= 475);
}

private Q_SLOTS:
addTest(Triggers)
addTest(QCoroWrapperTriggers)
addTest(DoesntBlockEventLoop)
addTest(DoesntCoAwaitInactiveTimer)
addTest(DoesntCoAwaitNullTimer)
addTest(SleepFor)
addTest(SleepUntil)

addThenTest(Triggers)
};
Expand Down
0