From 4d914d5c4974f0f8677228c0cc281765d06b0ee7 Mon Sep 17 00:00:00 2001 From: Coldwings Date: Wed, 16 Aug 2023 18:58:58 +0800 Subject: [PATCH] async awaiter --- common/executor/easyawaiter.h | 43 +++++++++++ include/photon/common/executor/easyawaiter.h | 1 + include/photon/thread/awaiter.h | 1 + thread/awaiter.h | 77 ++++++++++++++++++++ 4 files changed, 122 insertions(+) create mode 100644 common/executor/easyawaiter.h create mode 120000 include/photon/common/executor/easyawaiter.h create mode 120000 include/photon/thread/awaiter.h create mode 100644 thread/awaiter.h diff --git a/common/executor/easyawaiter.h b/common/executor/easyawaiter.h new file mode 100644 index 00000000..db11dfa0 --- /dev/null +++ b/common/executor/easyawaiter.h @@ -0,0 +1,43 @@ +/* +Copyright 2022 The Photon Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#pragma once + +#include +#include +#include + +namespace photon { + +struct EasyContext {}; + +template <> +struct Awaiter { + easy_comutex_t mtx; + Awaiter() { + easy_comutex_init(&mtx); + easy_comutex_cond_lock(&mtx); + } + ~Awaiter() { easy_comutex_cond_unlock(&mtx); } + void suspend() { easy_comutex_cond_wait(&mtx); } + void resume() { + easy_comutex_cond_lock(&mtx); + easy_comutex_cond_signal(&mtx); + easy_comutex_cond_unlock(&mtx); + } +}; + +} // namespace photon diff --git a/include/photon/common/executor/easyawaiter.h b/include/photon/common/executor/easyawaiter.h new file mode 120000 index 00000000..a2e8a7c3 --- /dev/null +++ b/include/photon/common/executor/easyawaiter.h @@ -0,0 +1 @@ +../../../../common/executor/easyawaiter.h \ No newline at end of file diff --git a/include/photon/thread/awaiter.h b/include/photon/thread/awaiter.h new file mode 120000 index 00000000..39ba20de --- /dev/null +++ b/include/photon/thread/awaiter.h @@ -0,0 +1 @@ +../../../thread/awaiter.h \ No newline at end of file diff --git a/thread/awaiter.h b/thread/awaiter.h new file mode 100644 index 00000000..e419d0d1 --- /dev/null +++ b/thread/awaiter.h @@ -0,0 +1,77 @@ +/* +Copyright 2022 The Photon Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +*/ + +#pragma once + +#include +#include + +#include +#include + +namespace photon { + +// Special context that supports calling executor in photon thread +struct PhotonContext {}; + +// Context for stdthread calling executor +struct StdContext {}; + +// Special context that can automaticlly choose `PhotonContext` or `StdContext` +// by if photon initialized in current environment +struct AutoContext {}; + +template +struct Awaiter; + +template <> +struct Awaiter { + photon::semaphore sem; + Awaiter() {} + void suspend() { sem.wait(1); } + void resume() { sem.signal(1); } +}; + +template <> +struct Awaiter { + int err; + std::promise p; + std::future f; + Awaiter() : f(p.get_future()) {} + void suspend() { f.get(); } + void resume() { return p.set_value(); } +}; + +template <> +struct Awaiter { + Awaiter pctx; + Awaiter sctx; + bool is_photon = false; + Awaiter() : is_photon(photon::CURRENT) {} + void suspend() { + if (is_photon) + pctx.suspend(); + else + sctx.suspend(); + } + void resume() { + if (is_photon) + pctx.resume(); + else + sctx.resume(); + } +}; +} // namespace photon \ No newline at end of file