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.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
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
19 * Copyright (C) 2019 ScyllaDB Ltd.
24 #if !SEASTAR_COROUTINES_TS
25 #error Coroutines TS support disabled.
28 #if __has_include(<experimental/coroutine>)
29 #include <experimental/coroutine>
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.
35 namespace std::experimental {
37 template<typename Promise>
38 class coroutine_handle {
39 void* _pointer = nullptr;
41 coroutine_handle() = default;
43 coroutine_handle &operator=(nullptr_t) noexcept {
48 explicit operator bool() const noexcept { return _pointer; }
50 static coroutine_handle from_address(void* ptr) noexcept {
51 coroutine_handle hndl;
55 void* address() const noexcept { return _pointer; }
57 static coroutine_handle from_promise(Promise& promise) noexcept {
58 coroutine_handle hndl;
59 hndl._pointer = __builtin_coro_promise(&promise, alignof(Promise), true);
62 Promise& promise() const noexcept {
63 return *reinterpret_cast<Promise*>(__builtin_coro_promise(_pointer, alignof(Promise), false));
66 void operator()() noexcept { resume(); }
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); }
73 struct suspend_never {
74 constexpr bool await_ready() const noexcept { return true; }
76 constexpr void await_suspend(coroutine_handle<T>) noexcept { }
77 constexpr void await_resume() noexcept { }
80 struct suspend_always {
81 constexpr bool await_ready() const noexcept { return false; }
83 constexpr void await_suspend(coroutine_handle<T>) noexcept { }
84 constexpr void await_resume() noexcept { }
87 template<typename T, typename... Args>
88 class coroutine_traits { };