Files
scylladb/utils/coroutine.hh
Avi Kivity fcb8d040e8 treewide: use Software Package Data Exchange (SPDX) license identifiers
Instead of lengthy blurbs, switch to single-line, machine-readable
standardized (https://spdx.dev) license identifiers. The Linux kernel
switched long ago, so there is strong precedent.

Three cases are handled: AGPL-only, Apache-only, and dual licensed.
For the latter case, I chose (AGPL-3.0-or-later and Apache-2.0),
reasoning that our changes are extensive enough to apply our license.

The changes we applied mechanically with a script, except to
licenses/README.md.

Closes #9937
2022-01-18 12:15:18 +01:00

52 lines
1.4 KiB
C++

/*
* Copyright (C) 2018-present ScyllaDB
*/
/*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
#pragma once
#include <functional>
#include <seastar/core/future-util.hh>
#include <seastar/util/noncopyable_function.hh>
#include "seastarx.hh"
namespace utils {
// Represents a deferring operation which defers cooperatively with the caller.
//
// The operation is started and resumed by calling run(), which returns
// with stop_iteration::no whenever the operation defers and is not completed yet.
// When the operation is finally complete, run() returns with stop_iteration::yes.
// After that, run() should not be invoked any more.
//
// This allows the caller to:
// 1) execute some post-defer and pre-resume actions atomically
// 2) have control over when the operation is resumed and in which context,
// in particular the caller can cancel the operation at deferring points.
//
// One simple way to drive the operation to completion:
//
// coroutine c;
// while (c.run() == stop_iteartion::no) {}
//
class coroutine final {
public:
coroutine() = default;
coroutine(noncopyable_function<stop_iteration()> f) : _run(std::move(f)) {}
stop_iteration run() { return _run(); }
explicit operator bool() const { return bool(_run); }
private:
noncopyable_function<stop_iteration()> _run;
};
// Makes a coroutine which does nothing.
inline
coroutine make_empty_coroutine() {
return coroutine([] { return stop_iteration::yes; });
}
}