/* /~` _ _ _|_. _ _ |_ | _ \_,(_)| | | || ||_|(_||_)|(/_ https://github.com/Naios/continuable v4.0.0 Copyright(c) 2015 - 2019 Denis Blank Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files(the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions : The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. **/ #ifndef CONTINUABLE_DETAIL_TRANSFORMS_WAIT_HPP_INCLUDED #define CONTINUABLE_DETAIL_TRANSFORMS_WAIT_HPP_INCLUDED #include #include #include #include #include #include #include #include #include #include #include #if defined(CONTINUABLE_HAS_EXCEPTIONS) # include #endif namespace cti { namespace detail { namespace transforms { template struct sync_trait; template struct sync_trait> { using result_t = result; }; using lock_t = std::unique_lock; using condition_variable_t = std::condition_variable; template ::result_t> Result wait_relaxed(continuable_base&& continuable) { // Do an immediate unpack if the continuable is ready if (continuable.is_ready()) { return std::move(continuable).unpack(); } std::mutex cv_mutex; condition_variable_t cv; std::atomic_bool ready{false}; Result sync_result; std::move(continuable) .next([&](auto&&... args) { sync_result = Result::from(std::forward(args)...); ready.store(true, std::memory_order_release); cv.notify_all(); }) .done(); if (!ready.load(std::memory_order_acquire)) { lock_t lock(cv_mutex); cv.wait(lock, [&] { return ready.load(std::memory_order_acquire); }); } return sync_result; } /// Transforms the continuation to sync execution and unpacks the result the if /// possible template auto wait_and_unpack(continuable_base&& continuable) { auto sync_result = wait_relaxed(std::move(continuable)); #if defined(CONTINUABLE_HAS_EXCEPTIONS) if (sync_result.is_value()) { return std::move(sync_result).get_value(); } else { assert(sync_result.is_exception()); std::rethrow_exception(sync_result.get_exception()); } #else return sync_result; #endif // CONTINUABLE_HAS_EXCEPTIONS } template struct wait_frame { std::mutex cv_mutex; std::mutex rw_mutex; condition_variable_t cv; std::atomic_bool ready{false}; Result sync_result; }; template ::result_t> Result wait_unsafe(continuable_base&& continuable, Waiter&& waiter) { // Do an immediate unpack if the continuable is ready if (continuable.is_ready()) { return std::move(continuable).unpack(); } using frame_t = wait_frame; auto frame = std::make_shared(); std::move(continuable) .next([frame = std::weak_ptr(frame)](auto&&... args) { if (auto locked = frame.lock()) { { std::lock_guard rw_lock(locked->rw_mutex); locked->sync_result = Result::from( std::forward(args)...); } locked->ready.store(true, std::memory_order_release); locked->cv.notify_all(); } }) .done(); if (!frame->ready.load(std::memory_order_acquire)) { lock_t lock(frame->cv_mutex); std::forward(waiter)(frame->cv, lock, [&] { return frame->ready.load(std::memory_order_acquire); }); } return ([&] { std::lock_guard rw_lock(frame->rw_mutex); Result cached = std::move(frame->sync_result); return cached; })(); } } // namespace transforms } // namespace detail } // namespace cti #endif // CONTINUABLE_DETAIL_TRANSFORMS_WAIT_HPP_INCLUDED