]> git.proxmox.com Git - ceph.git/blob - ceph/src/seastar/include/seastar/core/std-coroutine.hh
import 15.2.0 Octopus source
[ceph.git] / ceph / src / seastar / include / seastar / core / std-coroutine.hh
1 /*
2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
6 *
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
16 * under the License.
17 */
18 /*
19 * Copyright (C) 2019 ScyllaDB Ltd.
20 */
21
22 #pragma once
23
24 #if !SEASTAR_COROUTINES_TS
25 #error Coroutines TS support disabled.
26 #endif
27
28 #if __has_include(<experimental/coroutine>)
29 #include <experimental/coroutine>
30 #else
31
32 // We are not exactly allowed to defined anything in the std namespace, but this
33 // makes coroutines work with libstdc++. All of this is experimental anyway.
34
35 namespace std::experimental {
36
37 template<typename Promise>
38 class coroutine_handle {
39 void* _pointer = nullptr;
40 public:
41 coroutine_handle() = default;
42
43 coroutine_handle &operator=(nullptr_t) noexcept {
44 _pointer = nullptr;
45 return *this;
46 }
47
48 explicit operator bool() const noexcept { return _pointer; }
49
50 static coroutine_handle from_address(void* ptr) noexcept {
51 coroutine_handle hndl;
52 hndl._pointer =ptr;
53 return hndl;
54 }
55 void* address() const noexcept { return _pointer; }
56
57 static coroutine_handle from_promise(Promise& promise) noexcept {
58 coroutine_handle hndl;
59 hndl._pointer = __builtin_coro_promise(&promise, alignof(Promise), true);
60 return hndl;
61 }
62 Promise& promise() const noexcept {
63 return *reinterpret_cast<Promise*>(__builtin_coro_promise(_pointer, alignof(Promise), false));
64 }
65
66 void operator()() noexcept { resume(); }
67
68 void resume() const noexcept { __builtin_coro_resume(_pointer); }
69 void destroy() const noexcept { __builtin_coro_destroy(_pointer); }
70 bool done() const noexcept { return __builtin_coro_done(_pointer); }
71 };
72
73 struct suspend_never {
74 constexpr bool await_ready() const noexcept { return true; }
75 template<typename T>
76 constexpr void await_suspend(coroutine_handle<T>) noexcept { }
77 constexpr void await_resume() noexcept { }
78 };
79
80 struct suspend_always {
81 constexpr bool await_ready() const noexcept { return false; }
82 template<typename T>
83 constexpr void await_suspend(coroutine_handle<T>) noexcept { }
84 constexpr void await_resume() noexcept { }
85 };
86
87 template<typename T, typename... Args>
88 class coroutine_traits { };
89
90 }
91
92 #endif