aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/coroutine/engine/impl.h
diff options
context:
space:
mode:
authorAnton Samokhvalov <pg83@yandex.ru>2022-02-10 16:45:17 +0300
committerDaniil Cherednik <dcherednik@yandex-team.ru>2022-02-10 16:45:17 +0300
commitd3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch)
treedd4bd3ca0f36b817e96812825ffaf10d645803f2 /library/cpp/coroutine/engine/impl.h
parent72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff)
downloadydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/coroutine/engine/impl.h')
-rw-r--r--library/cpp/coroutine/engine/impl.h216
1 files changed, 108 insertions, 108 deletions
diff --git a/library/cpp/coroutine/engine/impl.h b/library/cpp/coroutine/engine/impl.h
index d904235ed3..283a96ecf1 100644
--- a/library/cpp/coroutine/engine/impl.h
+++ b/library/cpp/coroutine/engine/impl.h
@@ -1,30 +1,30 @@
#pragma once
-
+
#include "callbacks.h"
#include "cont_poller.h"
#include "iostatus.h"
-#include "poller.h"
+#include "poller.h"
#include "stack/stack_common.h"
#include "trampoline.h"
#include "custom_time.h"
-
+
#include <library/cpp/containers/intrusive_rb_tree/rb_tree.h>
-#include <util/system/error.h>
-#include <util/generic/ptr.h>
-#include <util/generic/intrlist.h>
-#include <util/datetime/base.h>
+#include <util/system/error.h>
+#include <util/generic/ptr.h>
+#include <util/generic/intrlist.h>
+#include <util/datetime/base.h>
#include <util/generic/maybe.h>
#include <util/generic/function.h>
-
+
#define EWAKEDUP 34567
-class TCont;
-struct TContRep;
-class TContExecutor;
-class TContPollEvent;
-
+class TCont;
+struct TContRep;
+class TContExecutor;
+class TContPollEvent;
+
namespace NCoro::NStack {
class IAllocator;
}
@@ -32,18 +32,18 @@ namespace NCoro::NStack {
class TCont : private TIntrusiveListItem<TCont> {
struct TJoinWait: public TIntrusiveListItem<TJoinWait> {
TJoinWait(TCont& c) noexcept;
-
+
void Wake() noexcept;
-
+
public:
TCont& Cont_;
};
-
+
friend class TContExecutor;
friend class TIntrusiveListItem<TCont>;
friend class NCoro::TEventWaitQueue;
friend class NCoro::TTrampoline;
-
+
private:
TCont(
NCoro::NStack::IAllocator& allocator,
@@ -52,63 +52,63 @@ private:
NCoro::TTrampoline::TFunc func,
const char* name
) noexcept;
-
+
public:
TContExecutor* Executor() noexcept {
return &Executor_;
- }
-
+ }
+
const TContExecutor* Executor() const noexcept {
return &Executor_;
- }
-
+ }
+
const char* Name() const noexcept {
- return Name_;
- }
-
+ return Name_;
+ }
+
void PrintMe(IOutputStream& out) const noexcept;
-
+
void Yield() noexcept;
-
+
void ReScheduleAndSwitch() noexcept;
-
- /// @return ETIMEDOUT on success
+
+ /// @return ETIMEDOUT on success
int SleepD(TInstant deadline) noexcept;
-
+
int SleepT(TDuration timeout) noexcept {
- return SleepD(timeout.ToDeadLine());
- }
-
+ return SleepD(timeout.ToDeadLine());
+ }
+
int SleepI() noexcept {
- return SleepD(TInstant::Max());
- }
-
+ return SleepD(TInstant::Max());
+ }
+
bool IAmRunning() const noexcept;
void Cancel() noexcept;
bool Cancelled() const noexcept {
- return Cancelled_;
- }
-
+ return Cancelled_;
+ }
+
bool Scheduled() const noexcept {
return Scheduled_;
}
bool Join(TCont* c, TInstant deadLine = TInstant::Max()) noexcept;
-
+
void ReSchedule() noexcept;
-
+
void Switch() noexcept;
void SwitchTo(TExceptionSafeContext* ctx) {
Trampoline_.SwitchTo(ctx);
}
-private:
+private:
void Terminate();
-private:
+private:
TContExecutor& Executor_;
// TODO(velavokr): allow name storage owning (for generated names backed by TString)
@@ -119,17 +119,17 @@ private:
TIntrusiveList<TJoinWait> Waiters_;
bool Cancelled_ = false;
bool Scheduled_ = false;
-};
-
+};
+
TCont* RunningCont();
-
-
-template <class Functor>
-static void ContHelperFunc(TCont* cont, void* arg) {
- (*((Functor*)(arg)))(cont);
-}
-
-template <typename T, void (T::*M)(TCont*)>
+
+
+template <class Functor>
+static void ContHelperFunc(TCont* cont, void* arg) {
+ (*((Functor*)(arg)))(cont);
+}
+
+template <typename T, void (T::*M)(TCont*)>
static void ContHelperMemberFunc(TCont* c, void* arg) {
((reinterpret_cast<T*>(arg))->*M)(c);
}
@@ -145,11 +145,11 @@ public:
/// Central coroutine class.
/// Note, coroutines are single-threaded, and all methods must be called from the single thread
-class TContExecutor {
- friend class TCont;
+class TContExecutor {
+ friend class TCont;
using TContList = TIntrusiveList<TCont>;
-
-public:
+
+public:
TContExecutor(
uint32_t defaultStackSize,
THolder<IPollerFace> poller = IPollerFace::Default(),
@@ -159,41 +159,41 @@ public:
TMaybe<NCoro::NStack::TPoolAllocatorSettings> poolSettings = Nothing(),
NCoro::ITime* time = nullptr
);
-
+
~TContExecutor();
-
+
// if we already have a coroutine to run
void Execute() noexcept;
-
+
void Execute(TContFunc func, void* arg = nullptr) noexcept;
-
- template <class Functor>
+
+ template <class Functor>
void Execute(Functor& f) noexcept {
- Execute((TContFunc)ContHelperFunc<Functor>, (void*)&f);
- }
-
- template <typename T, void (T::*M)(TCont*)>
+ Execute((TContFunc)ContHelperFunc<Functor>, (void*)&f);
+ }
+
+ template <typename T, void (T::*M)(TCont*)>
void Execute(T* obj) noexcept {
- Execute(ContHelperMemberFunc<T, M>, obj);
- }
+ Execute(ContHelperMemberFunc<T, M>, obj);
+ }
- template <class Functor>
+ template <class Functor>
TCont* Create(
Functor& f,
const char* name,
TMaybe<ui32> customStackSize = Nothing()
) noexcept {
return Create((TContFunc)ContHelperFunc<Functor>, (void*)&f, name, customStackSize);
- }
-
- template <typename T, void (T::*M)(TCont*)>
+ }
+
+ template <typename T, void (T::*M)(TCont*)>
TCont* Create(
T* obj,
const char* name,
TMaybe<ui32> customStackSize = Nothing()
) noexcept {
return Create(ContHelperMemberFunc<T, M>, obj, name, customStackSize);
- }
+ }
TCont* Create(
TContFunc func,
@@ -201,7 +201,7 @@ public:
const char* name,
TMaybe<ui32> customStackSize = Nothing()
) noexcept;
-
+
TCont* CreateOwned(
NCoro::TTrampoline::TFunc func,
const char* name,
@@ -209,17 +209,17 @@ public:
) noexcept;
NCoro::TContPoller* Poller() noexcept {
- return &Poller_;
- }
-
+ return &Poller_;
+ }
+
TCont* Running() noexcept {
- return Current_;
- }
-
+ return Current_;
+ }
+
const TCont* Running() const noexcept {
- return Current_;
- }
-
+ return Current_;
+ }
+
size_t TotalReadyConts() const noexcept {
return Ready_.Size() + TotalScheduledConts();
}
@@ -240,54 +240,54 @@ public:
// TODO(velavokr): rename, it is just CancelAll actually
void Abort() noexcept;
-
+
void SetFailOnError(bool fail) noexcept {
- FailOnError_ = fail;
- }
-
+ FailOnError_ = fail;
+ }
+
bool FailOnError() const noexcept {
- return FailOnError_;
- }
-
+ return FailOnError_;
+ }
+
void RegisterInWaitQueue(NCoro::TContPollEvent* event) {
WaitQueue_.Register(event);
}
void ScheduleIoWait(TFdEvent* event) {
RegisterInWaitQueue(event);
- Poller_.Schedule(event);
- }
-
+ Poller_.Schedule(event);
+ }
+
void ScheduleIoWait(TTimerEvent* event) noexcept {
RegisterInWaitQueue(event);
- }
-
+ }
+
void ScheduleUserEvent(IUserEvent* event) {
UserEvents_.PushBack(event);
}
void Pause();
TInstant Now();
-private:
+private:
void Release(TCont* cont) noexcept;
-
+
void Exit(TCont* cont) noexcept;
-
+
void RunScheduler() noexcept;
-
+
void ScheduleToDelete(TCont* cont) noexcept;
-
+
void ScheduleExecution(TCont* cont) noexcept;
-
+
void ScheduleExecutionNow(TCont* cont) noexcept;
void DeleteScheduled() noexcept;
-
- void WaitForIO();
-
+
+ void WaitForIO();
+
void Poll(TInstant deadline);
-private:
+private:
NCoro::IScheduleCallback* const ScheduleCallback_ = nullptr;
NCoro::IEnterPollerCallback* const EnterPollerCallback_ = nullptr;
const uint32_t DefaultStackSize_;
@@ -295,8 +295,8 @@ private:
TExceptionSafeContext SchedContext_;
- TContList ToDelete_;
- TContList Ready_;
+ TContList ToDelete_;
+ TContList Ready_;
TContList ReadyNext_;
NCoro::TEventWaitQueue WaitQueue_;
NCoro::TContPoller Poller_;
@@ -310,4 +310,4 @@ private:
bool FailOnError_ = false;
bool Paused_ = false;
NCoro::ITime* Time_ = nullptr;
-};
+};