2010-05-11 19:42:16 +00:00
|
|
|
// -*- C++ -*-
|
2021-11-17 21:25:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
2010-05-11 19:42:16 +00:00
|
|
|
//
|
2019-01-19 10:56:40 +00:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2010-05-11 19:42:16 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef _LIBCPP_FUTURE
|
|
|
|
#define _LIBCPP_FUTURE
|
|
|
|
|
|
|
|
/*
|
|
|
|
future synopsis
|
|
|
|
|
|
|
|
namespace std
|
|
|
|
{
|
|
|
|
|
|
|
|
enum class future_errc
|
|
|
|
{
|
2013-09-14 18:20:10 +00:00
|
|
|
future_already_retrieved = 1,
|
2010-05-11 19:42:16 +00:00
|
|
|
promise_already_satisfied,
|
2013-09-14 18:20:10 +00:00
|
|
|
no_state,
|
|
|
|
broken_promise
|
2010-05-11 19:42:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum class launch
|
|
|
|
{
|
2010-11-23 18:33:54 +00:00
|
|
|
async = 1,
|
|
|
|
deferred = 2,
|
|
|
|
any = async | deferred
|
2010-05-11 19:42:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
enum class future_status
|
|
|
|
{
|
|
|
|
ready,
|
|
|
|
timeout,
|
|
|
|
deferred
|
|
|
|
};
|
|
|
|
|
|
|
|
template <> struct is_error_code_enum<future_errc> : public true_type { };
|
2012-07-21 17:46:55 +00:00
|
|
|
error_code make_error_code(future_errc e) noexcept;
|
|
|
|
error_condition make_error_condition(future_errc e) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
const error_category& future_category() noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2023-09-26 16:54:16 +00:00
|
|
|
class future_error : public logic_error {
|
2010-05-11 19:42:16 +00:00
|
|
|
public:
|
2023-09-26 16:54:16 +00:00
|
|
|
explicit future_error(future_errc e); // since C++17
|
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
const error_code& code() const noexcept;
|
|
|
|
const char* what() const noexcept;
|
2023-09-26 16:54:16 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
error_code ec_; // exposition only
|
2010-05-11 19:42:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class R>
|
|
|
|
class promise
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
promise();
|
|
|
|
template <class Allocator>
|
|
|
|
promise(allocator_arg_t, const Allocator& a);
|
2012-07-21 17:46:55 +00:00
|
|
|
promise(promise&& rhs) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
promise(const promise& rhs) = delete;
|
|
|
|
~promise();
|
|
|
|
|
|
|
|
// assignment
|
2012-07-21 17:46:55 +00:00
|
|
|
promise& operator=(promise&& rhs) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
promise& operator=(const promise& rhs) = delete;
|
2012-07-21 17:46:55 +00:00
|
|
|
void swap(promise& other) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
// retrieving the result
|
|
|
|
future<R> get_future();
|
|
|
|
|
|
|
|
// setting the result
|
|
|
|
void set_value(const R& r);
|
|
|
|
void set_value(R&& r);
|
|
|
|
void set_exception(exception_ptr p);
|
|
|
|
|
|
|
|
// setting the result with deferred notification
|
|
|
|
void set_value_at_thread_exit(const R& r);
|
|
|
|
void set_value_at_thread_exit(R&& r);
|
|
|
|
void set_exception_at_thread_exit(exception_ptr p);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class R>
|
|
|
|
class promise<R&>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
promise();
|
|
|
|
template <class Allocator>
|
|
|
|
promise(allocator_arg_t, const Allocator& a);
|
2012-07-21 17:46:55 +00:00
|
|
|
promise(promise&& rhs) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
promise(const promise& rhs) = delete;
|
|
|
|
~promise();
|
|
|
|
|
|
|
|
// assignment
|
2012-07-21 17:46:55 +00:00
|
|
|
promise& operator=(promise&& rhs) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
promise& operator=(const promise& rhs) = delete;
|
2012-07-21 17:46:55 +00:00
|
|
|
void swap(promise& other) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
// retrieving the result
|
2010-08-27 20:10:19 +00:00
|
|
|
future<R&> get_future();
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
// setting the result
|
|
|
|
void set_value(R& r);
|
|
|
|
void set_exception(exception_ptr p);
|
|
|
|
|
|
|
|
// setting the result with deferred notification
|
|
|
|
void set_value_at_thread_exit(R&);
|
|
|
|
void set_exception_at_thread_exit(exception_ptr p);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class promise<void>
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
promise();
|
|
|
|
template <class Allocator>
|
|
|
|
promise(allocator_arg_t, const Allocator& a);
|
2012-07-21 17:46:55 +00:00
|
|
|
promise(promise&& rhs) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
promise(const promise& rhs) = delete;
|
|
|
|
~promise();
|
|
|
|
|
|
|
|
// assignment
|
2012-07-21 17:46:55 +00:00
|
|
|
promise& operator=(promise&& rhs) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
promise& operator=(const promise& rhs) = delete;
|
2012-07-21 17:46:55 +00:00
|
|
|
void swap(promise& other) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
// retrieving the result
|
2010-08-27 20:10:19 +00:00
|
|
|
future<void> get_future();
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
// setting the result
|
|
|
|
void set_value();
|
|
|
|
void set_exception(exception_ptr p);
|
|
|
|
|
|
|
|
// setting the result with deferred notification
|
|
|
|
void set_value_at_thread_exit();
|
|
|
|
void set_exception_at_thread_exit(exception_ptr p);
|
|
|
|
};
|
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
template <class R> void swap(promise<R>& x, promise<R>& y) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
template <class R, class Alloc>
|
|
|
|
struct uses_allocator<promise<R>, Alloc> : public true_type {};
|
|
|
|
|
|
|
|
template <class R>
|
|
|
|
class future
|
|
|
|
{
|
|
|
|
public:
|
2012-07-21 17:46:55 +00:00
|
|
|
future() noexcept;
|
|
|
|
future(future&&) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
future(const future& rhs) = delete;
|
|
|
|
~future();
|
|
|
|
future& operator=(const future& rhs) = delete;
|
2012-07-21 17:46:55 +00:00
|
|
|
future& operator=(future&&) noexcept;
|
2017-01-25 20:14:03 +00:00
|
|
|
shared_future<R> share() noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
// retrieving the value
|
|
|
|
R get();
|
|
|
|
|
|
|
|
// functions to check state
|
2012-07-21 17:46:55 +00:00
|
|
|
bool valid() const noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
void wait() const;
|
|
|
|
template <class Rep, class Period>
|
|
|
|
future_status
|
|
|
|
wait_for(const chrono::duration<Rep, Period>& rel_time) const;
|
|
|
|
template <class Clock, class Duration>
|
|
|
|
future_status
|
|
|
|
wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class R>
|
|
|
|
class future<R&>
|
|
|
|
{
|
|
|
|
public:
|
2012-07-21 17:46:55 +00:00
|
|
|
future() noexcept;
|
|
|
|
future(future&&) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
future(const future& rhs) = delete;
|
|
|
|
~future();
|
|
|
|
future& operator=(const future& rhs) = delete;
|
2012-07-21 17:46:55 +00:00
|
|
|
future& operator=(future&&) noexcept;
|
2017-01-24 23:28:25 +00:00
|
|
|
shared_future<R&> share() noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
// retrieving the value
|
|
|
|
R& get();
|
|
|
|
|
|
|
|
// functions to check state
|
2012-07-21 17:46:55 +00:00
|
|
|
bool valid() const noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
void wait() const;
|
|
|
|
template <class Rep, class Period>
|
|
|
|
future_status
|
|
|
|
wait_for(const chrono::duration<Rep, Period>& rel_time) const;
|
|
|
|
template <class Clock, class Duration>
|
|
|
|
future_status
|
|
|
|
wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class future<void>
|
|
|
|
{
|
|
|
|
public:
|
2012-07-21 17:46:55 +00:00
|
|
|
future() noexcept;
|
|
|
|
future(future&&) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
future(const future& rhs) = delete;
|
|
|
|
~future();
|
|
|
|
future& operator=(const future& rhs) = delete;
|
2012-07-21 17:46:55 +00:00
|
|
|
future& operator=(future&&) noexcept;
|
2017-01-24 23:28:25 +00:00
|
|
|
shared_future<void> share() noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
// retrieving the value
|
|
|
|
void get();
|
|
|
|
|
|
|
|
// functions to check state
|
2012-07-21 17:46:55 +00:00
|
|
|
bool valid() const noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
void wait() const;
|
|
|
|
template <class Rep, class Period>
|
|
|
|
future_status
|
|
|
|
wait_for(const chrono::duration<Rep, Period>& rel_time) const;
|
|
|
|
template <class Clock, class Duration>
|
|
|
|
future_status
|
|
|
|
wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class R>
|
|
|
|
class shared_future
|
|
|
|
{
|
|
|
|
public:
|
2012-07-21 17:46:55 +00:00
|
|
|
shared_future() noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
shared_future(const shared_future& rhs);
|
2012-07-21 17:46:55 +00:00
|
|
|
shared_future(future<R>&&) noexcept;
|
|
|
|
shared_future(shared_future&& rhs) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
~shared_future();
|
|
|
|
shared_future& operator=(const shared_future& rhs);
|
2012-07-21 17:46:55 +00:00
|
|
|
shared_future& operator=(shared_future&& rhs) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
// retrieving the value
|
|
|
|
const R& get() const;
|
|
|
|
|
|
|
|
// functions to check state
|
2012-07-21 17:46:55 +00:00
|
|
|
bool valid() const noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
void wait() const;
|
|
|
|
template <class Rep, class Period>
|
|
|
|
future_status
|
|
|
|
wait_for(const chrono::duration<Rep, Period>& rel_time) const;
|
|
|
|
template <class Clock, class Duration>
|
|
|
|
future_status
|
|
|
|
wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class R>
|
|
|
|
class shared_future<R&>
|
|
|
|
{
|
|
|
|
public:
|
2012-07-21 17:46:55 +00:00
|
|
|
shared_future() noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
shared_future(const shared_future& rhs);
|
2012-07-21 17:46:55 +00:00
|
|
|
shared_future(future<R&>&&) noexcept;
|
|
|
|
shared_future(shared_future&& rhs) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
~shared_future();
|
|
|
|
shared_future& operator=(const shared_future& rhs);
|
2012-07-21 17:46:55 +00:00
|
|
|
shared_future& operator=(shared_future&& rhs) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
// retrieving the value
|
|
|
|
R& get() const;
|
|
|
|
|
|
|
|
// functions to check state
|
2012-07-21 17:46:55 +00:00
|
|
|
bool valid() const noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
void wait() const;
|
|
|
|
template <class Rep, class Period>
|
|
|
|
future_status
|
|
|
|
wait_for(const chrono::duration<Rep, Period>& rel_time) const;
|
|
|
|
template <class Clock, class Duration>
|
|
|
|
future_status
|
|
|
|
wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
class shared_future<void>
|
|
|
|
{
|
|
|
|
public:
|
2012-07-21 17:46:55 +00:00
|
|
|
shared_future() noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
shared_future(const shared_future& rhs);
|
2012-07-21 17:46:55 +00:00
|
|
|
shared_future(future<void>&&) noexcept;
|
|
|
|
shared_future(shared_future&& rhs) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
~shared_future();
|
|
|
|
shared_future& operator=(const shared_future& rhs);
|
2012-07-21 17:46:55 +00:00
|
|
|
shared_future& operator=(shared_future&& rhs) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
// retrieving the value
|
|
|
|
void get() const;
|
|
|
|
|
|
|
|
// functions to check state
|
2012-07-21 17:46:55 +00:00
|
|
|
bool valid() const noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
void wait() const;
|
|
|
|
template <class Rep, class Period>
|
|
|
|
future_status
|
|
|
|
wait_for(const chrono::duration<Rep, Period>& rel_time) const;
|
|
|
|
template <class Clock, class Duration>
|
|
|
|
future_status
|
|
|
|
wait_until(const chrono::time_point<Clock, Duration>& abs_time) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class F, class... Args>
|
2013-09-21 18:17:23 +00:00
|
|
|
future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
|
2010-05-11 19:42:16 +00:00
|
|
|
async(F&& f, Args&&... args);
|
|
|
|
|
|
|
|
template <class F, class... Args>
|
2013-09-21 18:17:23 +00:00
|
|
|
future<typename result_of<typename decay<F>::type(typename decay<Args>::type...)>::type>
|
2010-05-11 19:42:16 +00:00
|
|
|
async(launch policy, F&& f, Args&&... args);
|
|
|
|
|
|
|
|
template <class> class packaged_task; // undefined
|
|
|
|
|
|
|
|
template <class R, class... ArgTypes>
|
|
|
|
class packaged_task<R(ArgTypes...)>
|
|
|
|
{
|
|
|
|
public:
|
2016-06-01 21:05:53 +00:00
|
|
|
typedef R result_type; // extension
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
// construction and destruction
|
2012-07-21 17:46:55 +00:00
|
|
|
packaged_task() noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
template <class F>
|
|
|
|
explicit packaged_task(F&& f);
|
2017-11-27 20:47:54 +00:00
|
|
|
template <class F, class Allocator>
|
|
|
|
packaged_task(allocator_arg_t, const Allocator& a, F&& f);
|
2010-05-11 19:42:16 +00:00
|
|
|
~packaged_task();
|
|
|
|
|
|
|
|
// no copy
|
2012-07-21 19:34:12 +00:00
|
|
|
packaged_task(const packaged_task&) = delete;
|
|
|
|
packaged_task& operator=(const packaged_task&) = delete;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
// move support
|
2012-07-21 17:46:55 +00:00
|
|
|
packaged_task(packaged_task&& other) noexcept;
|
|
|
|
packaged_task& operator=(packaged_task&& other) noexcept;
|
|
|
|
void swap(packaged_task& other) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
bool valid() const noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
|
|
|
// result retrieval
|
|
|
|
future<R> get_future();
|
|
|
|
|
|
|
|
// execution
|
|
|
|
void operator()(ArgTypes... );
|
|
|
|
void make_ready_at_thread_exit(ArgTypes...);
|
|
|
|
|
|
|
|
void reset();
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class R>
|
2012-07-21 17:46:55 +00:00
|
|
|
void swap(packaged_task<R(ArgTypes...)&, packaged_task<R(ArgTypes...)>&) noexcept;
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2017-11-27 20:47:54 +00:00
|
|
|
template <class R, class Alloc> struct uses_allocator<packaged_task<R>, Alloc>;
|
|
|
|
|
2010-05-11 19:42:16 +00:00
|
|
|
} // std
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
2024-01-16 18:13:40 +00:00
|
|
|
#include <__config>
|
|
|
|
|
|
|
|
#ifdef _LIBCPP_HAS_NO_THREADS
|
|
|
|
# error "<future> is not supported since libc++ has been configured without support for threads."
|
|
|
|
#endif
|
|
|
|
|
2022-03-25 16:55:36 +00:00
|
|
|
#include <__assert> // all public C++ headers provide the assertion handler
|
2020-11-04 20:01:25 +00:00
|
|
|
#include <__availability>
|
2022-02-23 22:05:22 +00:00
|
|
|
#include <__chrono/duration.h>
|
|
|
|
#include <__chrono/time_point.h>
|
2023-03-01 19:49:22 +00:00
|
|
|
#include <__exception/exception_ptr.h>
|
2023-05-31 09:49:56 +00:00
|
|
|
#include <__memory/addressof.h>
|
2023-04-08 15:29:31 +00:00
|
|
|
#include <__memory/allocator.h>
|
2021-07-01 13:25:35 +00:00
|
|
|
#include <__memory/allocator_arg_t.h>
|
2022-09-22 19:53:13 +00:00
|
|
|
#include <__memory/allocator_destructor.h>
|
2023-04-08 15:29:31 +00:00
|
|
|
#include <__memory/allocator_traits.h>
|
|
|
|
#include <__memory/compressed_pair.h>
|
|
|
|
#include <__memory/pointer_traits.h>
|
2023-02-12 11:32:36 +00:00
|
|
|
#include <__memory/shared_ptr.h>
|
|
|
|
#include <__memory/unique_ptr.h>
|
2021-07-01 13:25:35 +00:00
|
|
|
#include <__memory/uses_allocator.h>
|
2023-04-08 15:29:31 +00:00
|
|
|
#include <__system_error/error_category.h>
|
|
|
|
#include <__system_error/error_code.h>
|
|
|
|
#include <__system_error/error_condition.h>
|
2023-02-12 11:32:36 +00:00
|
|
|
#include <__type_traits/aligned_storage.h>
|
2022-10-01 19:25:13 +00:00
|
|
|
#include <__type_traits/strip_signature.h>
|
2021-12-14 00:21:38 +00:00
|
|
|
#include <__utility/auto_cast.h>
|
2021-06-05 02:47:47 +00:00
|
|
|
#include <__utility/forward.h>
|
2022-03-05 18:17:07 +00:00
|
|
|
#include <__utility/move.h>
|
2010-09-03 21:46:37 +00:00
|
|
|
#include <mutex>
|
2022-09-04 22:01:15 +00:00
|
|
|
#include <new>
|
2023-04-08 15:29:31 +00:00
|
|
|
#include <stdexcept>
|
2010-08-27 20:10:19 +00:00
|
|
|
#include <thread>
|
2021-12-22 17:14:14 +00:00
|
|
|
#include <version>
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2011-10-17 20:05:10 +00:00
|
|
|
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
2022-02-02 01:16:40 +00:00
|
|
|
# pragma GCC system_header
|
2011-10-17 20:05:10 +00:00
|
|
|
#endif
|
2010-05-11 19:42:16 +00:00
|
|
|
|
[🍒][libc++] Fix missing and incorrect push/pop macros (#79204) (#79497)
We recently noticed that the unwrap_iter.h file was pushing macros, but
it was pushing them again instead of popping them at the end of the
file. This led to libc++ basically swallowing any custom definition of
these macros in user code:
#define min HELLO
#include <algorithm>
// min is not HELLO anymore, it's not defined
While investigating this issue, I noticed that our push/pop pragmas were
actually entirely wrong too. Indeed, instead of pushing macros like
`move`, we'd push `move(int, int)` in the pragma, which is not a valid
macro name. As a result, we would not actually push macros like `move`
-- instead we'd simply undefine them. This led to the following code not
working:
#define move HELLO
#include <algorithm>
// move is not HELLO anymore
Fixing the pragma push/pop incantations led to a cascade of issues
because we use identifiers like `move` in a large number of places, and
all of these headers would now need to do the push/pop dance.
This patch fixes all these issues. First, it adds a check that we don't
swallow important names like min, max, move or refresh as explained
above. This is done by augmenting the existing
system_reserved_names.gen.py test to also check that the macros are what
we expect after including each header.
Second, it fixes the push/pop pragmas to work properly and adds missing
pragmas to all the files I could detect a failure in via the newly added
test.
rdar://121365472
(cherry picked from commit 7b4622514d232ce5f7110dd8b20d90e81127c467)
2024-02-02 01:51:34 +00:00
|
|
|
_LIBCPP_PUSH_MACROS
|
|
|
|
#include <__undef_macros>
|
|
|
|
|
2010-05-11 19:42:16 +00:00
|
|
|
_LIBCPP_BEGIN_NAMESPACE_STD
|
|
|
|
|
|
|
|
// enum class future_errc
|
2011-12-02 19:36:40 +00:00
|
|
|
_LIBCPP_DECLARE_STRONG_ENUM(future_errc){
|
2013-09-14 18:20:10 +00:00
|
|
|
future_already_retrieved = 1, promise_already_satisfied, no_state, broken_promise};
|
2011-12-02 19:36:40 +00:00
|
|
|
_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_errc)
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2010-09-22 14:16:26 +00:00
|
|
|
template <>
|
2017-01-04 23:56:00 +00:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc> : public true_type {};
|
2010-08-25 17:32:05 +00:00
|
|
|
|
2022-02-11 18:34:33 +00:00
|
|
|
#ifdef _LIBCPP_CXX03_LANG
|
2011-12-02 19:36:40 +00:00
|
|
|
template <>
|
2017-01-04 23:56:00 +00:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS is_error_code_enum<future_errc::__lx> : public true_type {};
|
2011-12-02 19:36:40 +00:00
|
|
|
#endif
|
|
|
|
|
2010-05-11 19:42:16 +00:00
|
|
|
// enum class launch
|
2011-12-02 19:36:40 +00:00
|
|
|
_LIBCPP_DECLARE_STRONG_ENUM(launch){async = 1, deferred = 2, any = async | deferred};
|
|
|
|
_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(launch)
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2022-02-11 18:34:33 +00:00
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
2013-06-29 18:38:17 +00:00
|
|
|
|
|
|
|
typedef underlying_type<launch>::type __launch_underlying_type;
|
|
|
|
|
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator&(launch __x, launch __y) {
|
|
|
|
return static_cast<launch>(static_cast<__launch_underlying_type>(__x) & static_cast<__launch_underlying_type>(__y));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator|(launch __x, launch __y) {
|
|
|
|
return static_cast<launch>(static_cast<__launch_underlying_type>(__x) | static_cast<__launch_underlying_type>(__y));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator^(launch __x, launch __y) {
|
|
|
|
return static_cast<launch>(static_cast<__launch_underlying_type>(__x) ^ static_cast<__launch_underlying_type>(__y));
|
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR launch operator~(launch __x) {
|
2013-07-02 18:01:41 +00:00
|
|
|
return static_cast<launch>(~static_cast<__launch_underlying_type>(__x) & 3);
|
2013-06-29 18:38:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_HIDE_FROM_ABI launch& operator&=(launch& __x, launch __y) {
|
|
|
|
__x = __x & __y;
|
|
|
|
return __x;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_HIDE_FROM_ABI launch& operator|=(launch& __x, launch __y) {
|
|
|
|
__x = __x | __y;
|
|
|
|
return __x;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline _LIBCPP_HIDE_FROM_ABI launch& operator^=(launch& __x, launch __y) {
|
|
|
|
__x = __x ^ __y;
|
|
|
|
return __x;
|
|
|
|
}
|
|
|
|
|
2022-02-11 18:34:33 +00:00
|
|
|
#endif // !_LIBCPP_CXX03_LANG
|
2013-06-29 18:38:17 +00:00
|
|
|
|
2010-05-11 19:42:16 +00:00
|
|
|
// enum class future_status
|
2011-12-02 19:36:40 +00:00
|
|
|
_LIBCPP_DECLARE_STRONG_ENUM(future_status){ready, timeout, deferred};
|
|
|
|
_LIBCPP_DECLARE_STRONG_ENUM_EPILOG(future_status)
|
2010-05-11 19:42:16 +00:00
|
|
|
|
2023-06-14 17:17:50 +00:00
|
|
|
_LIBCPP_EXPORTED_FROM_ABI const error_category& future_category() _NOEXCEPT;
|
2010-08-25 17:32:05 +00:00
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(future_errc __e) _NOEXCEPT {
|
2010-08-25 17:32:05 +00:00
|
|
|
return error_code(static_cast<int>(__e), future_category());
|
|
|
|
}
|
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI error_condition make_error_condition(future_errc __e) _NOEXCEPT {
|
2010-08-25 17:32:05 +00:00
|
|
|
return error_condition(static_cast<int>(__e), future_category());
|
|
|
|
}
|
|
|
|
|
2023-11-01 22:56:43 +00:00
|
|
|
_LIBCPP_NORETURN inline _LIBCPP_HIDE_FROM_ABI void __throw_future_error(future_errc __ev);
|
2023-09-26 16:54:16 +00:00
|
|
|
|
2023-11-01 22:56:43 +00:00
|
|
|
class _LIBCPP_EXPORTED_FROM_ABI future_error : public logic_error {
|
2010-08-25 17:32:05 +00:00
|
|
|
error_code __ec_;
|
2023-09-26 16:54:16 +00:00
|
|
|
|
|
|
|
future_error(error_code);
|
|
|
|
friend void __throw_future_error(future_errc);
|
|
|
|
template <class>
|
|
|
|
friend class promise;
|
|
|
|
|
2010-08-25 17:32:05 +00:00
|
|
|
public:
|
2023-09-26 16:54:16 +00:00
|
|
|
#if _LIBCPP_STD_VER >= 17
|
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit future_error(future_errc __ec) : future_error(std::make_error_code(__ec)) {}
|
|
|
|
#endif
|
2021-04-01 06:29:55 +00:00
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI const error_code& code() const _NOEXCEPT { return __ec_; }
|
2011-07-07 21:03:52 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI future_error(const future_error&) _NOEXCEPT = default;
|
2022-08-24 00:14:29 +00:00
|
|
|
~future_error() _NOEXCEPT override;
|
2010-08-25 17:32:05 +00:00
|
|
|
};
|
|
|
|
|
2023-09-26 16:54:16 +00:00
|
|
|
// Declared above std::future_error
|
2022-07-08 16:17:26 +00:00
|
|
|
void __throw_future_error(future_errc __ev) {
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2022-07-08 16:17:26 +00:00
|
|
|
throw future_error(make_error_code(__ev));
|
2015-09-03 15:11:32 +00:00
|
|
|
#else
|
2023-01-08 13:07:44 +00:00
|
|
|
(void)__ev;
|
2023-03-16 14:05:49 +00:00
|
|
|
_LIBCPP_VERBOSE_ABORT("future_error was thrown in -fno-exceptions mode");
|
2015-09-03 15:11:32 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2023-11-01 22:56:43 +00:00
|
|
|
class _LIBCPP_EXPORTED_FROM_ABI __assoc_sub_state : public __shared_count {
|
2010-08-27 20:10:19 +00:00
|
|
|
protected:
|
|
|
|
exception_ptr __exception_;
|
|
|
|
mutable mutex __mut_;
|
|
|
|
mutable condition_variable __cv_;
|
|
|
|
unsigned __state_;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2022-08-24 00:14:29 +00:00
|
|
|
void __on_zero_shared() _NOEXCEPT override;
|
2010-08-30 18:46:21 +00:00
|
|
|
void __sub_wait(unique_lock<mutex>& __lk);
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
public:
|
2018-08-24 14:00:59 +00:00
|
|
|
enum { __constructed = 1, __future_attached = 2, ready = 4, deferred = 8 };
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI __assoc_sub_state() : __state_(0) {}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI bool __has_value() const { return (__state_ & __constructed) || (__exception_ != nullptr); }
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void __attach_future() {
|
|
|
|
lock_guard<mutex> __lk(__mut_);
|
2018-08-24 14:00:59 +00:00
|
|
|
bool __has_future_attached = (__state_ & __future_attached) != 0;
|
|
|
|
if (__has_future_attached)
|
|
|
|
__throw_future_error(future_errc::future_already_retrieved);
|
|
|
|
this->__add_shared();
|
2013-01-14 20:01:24 +00:00
|
|
|
__state_ |= __future_attached;
|
2023-12-18 19:01:33 +00:00
|
|
|
}
|
2010-08-27 20:10:19 +00:00
|
|
|
|
2015-11-07 01:22:13 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void __set_deferred() { __state_ |= deferred; }
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
void __make_ready();
|
|
|
|
_LIBCPP_HIDE_FROM_ABI bool __is_ready() const { return (__state_ & ready) != 0; }
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2017-03-02 03:22:18 +00:00
|
|
|
void set_value();
|
2010-08-27 20:10:19 +00:00
|
|
|
void set_value_at_thread_exit();
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
void set_exception(exception_ptr __p);
|
2023-01-23 09:27:14 +00:00
|
|
|
void set_exception_at_thread_exit(exception_ptr __p);
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-09-03 18:39:25 +00:00
|
|
|
void copy();
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-30 18:46:21 +00:00
|
|
|
void wait();
|
2010-08-27 20:10:19 +00:00
|
|
|
template <class _Rep, class _Period>
|
2010-08-28 21:01:06 +00:00
|
|
|
future_status _LIBCPP_HIDE_FROM_ABI wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const;
|
2010-08-27 20:10:19 +00:00
|
|
|
template <class _Clock, class _Duration>
|
2017-03-02 03:22:18 +00:00
|
|
|
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS future_status
|
2010-08-27 20:10:19 +00:00
|
|
|
wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-30 18:46:21 +00:00
|
|
|
virtual void __execute();
|
2010-08-27 20:10:19 +00:00
|
|
|
};
|
|
|
|
|
2010-08-28 21:01:06 +00:00
|
|
|
template <class _Clock, class _Duration>
|
|
|
|
future_status __assoc_sub_state::wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
|
|
|
|
unique_lock<mutex> __lk(__mut_);
|
2010-08-30 18:46:21 +00:00
|
|
|
if (__state_ & deferred)
|
|
|
|
return future_status::deferred;
|
|
|
|
while (!(__state_ & ready) && _Clock::now() < __abs_time)
|
2010-08-28 21:01:06 +00:00
|
|
|
__cv_.wait_until(__lk, __abs_time);
|
|
|
|
if (__state_ & ready)
|
|
|
|
return future_status::ready;
|
|
|
|
return future_status::timeout;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class _Rep, class _Period>
|
|
|
|
inline future_status __assoc_sub_state::wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
|
2010-11-20 19:16:30 +00:00
|
|
|
return wait_until(chrono::steady_clock::now() + __rel_time);
|
2010-08-28 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2023-11-01 22:56:43 +00:00
|
|
|
class _LIBCPP_HIDDEN __assoc_state : public __assoc_sub_state {
|
2010-08-27 20:10:19 +00:00
|
|
|
typedef __assoc_sub_state base;
|
2022-12-11 01:10:31 +00:00
|
|
|
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
|
2023-10-31 14:24:44 +00:00
|
|
|
typedef typename aligned_storage<sizeof(_Rp), _LIBCPP_ALIGNOF(_Rp)>::type _Up;
|
2022-12-11 01:10:31 +00:00
|
|
|
_LIBCPP_SUPPRESS_DEPRECATED_POP
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
protected:
|
2011-11-29 18:15:50 +00:00
|
|
|
_Up __value_;
|
2010-08-27 20:10:19 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
|
2010-08-27 20:10:19 +00:00
|
|
|
|
2023-12-18 19:01:33 +00:00
|
|
|
public:
|
2010-08-27 20:10:19 +00:00
|
|
|
template <class _Arg>
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void set_value(_Arg&& __arg);
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
template <class _Arg>
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Arg&& __arg);
|
2010-08-27 20:10:19 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _Rp move();
|
|
|
|
_LIBCPP_HIDE_FROM_ABI __add_lvalue_reference_t<_Rp> copy();
|
2010-08-27 20:10:19 +00:00
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
void __assoc_state<_Rp>::__on_zero_shared() _NOEXCEPT {
|
2010-08-27 20:10:19 +00:00
|
|
|
if (this->__state_ & base::__constructed)
|
2011-11-29 18:15:50 +00:00
|
|
|
reinterpret_cast<_Rp*>(&__value_)->~_Rp();
|
2010-08-27 20:10:19 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2010-08-27 20:10:19 +00:00
|
|
|
template <class _Arg>
|
2011-11-29 18:15:50 +00:00
|
|
|
void __assoc_state<_Rp>::set_value(_Arg&& __arg) {
|
2010-08-27 20:10:19 +00:00
|
|
|
unique_lock<mutex> __lk(this->__mut_);
|
|
|
|
if (this->__has_value())
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::promise_already_satisfied);
|
2020-12-12 01:30:28 +00:00
|
|
|
::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
|
2010-08-27 20:10:19 +00:00
|
|
|
this->__state_ |= base::__constructed | base::ready;
|
|
|
|
__cv_.notify_all();
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2010-08-27 20:10:19 +00:00
|
|
|
template <class _Arg>
|
2011-11-29 18:15:50 +00:00
|
|
|
void __assoc_state<_Rp>::set_value_at_thread_exit(_Arg&& __arg) {
|
2010-08-27 20:10:19 +00:00
|
|
|
unique_lock<mutex> __lk(this->__mut_);
|
|
|
|
if (this->__has_value())
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::promise_already_satisfied);
|
2020-12-12 01:30:28 +00:00
|
|
|
::new ((void*)&__value_) _Rp(std::forward<_Arg>(__arg));
|
2010-08-27 20:10:19 +00:00
|
|
|
this->__state_ |= base::__constructed;
|
2010-10-14 19:18:04 +00:00
|
|
|
__thread_local_data()->__make_ready_at_thread_exit(this);
|
2010-08-27 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
_Rp __assoc_state<_Rp>::move() {
|
2010-08-27 20:10:19 +00:00
|
|
|
unique_lock<mutex> __lk(this->__mut_);
|
2010-08-30 18:46:21 +00:00
|
|
|
this->__sub_wait(__lk);
|
2010-08-27 20:10:19 +00:00
|
|
|
if (this->__exception_ != nullptr)
|
[libc++] Add custom clang-tidy checks
Reviewed By: #libc, ldionne
Spies: jwakely, beanz, smeenai, cfe-commits, tschuett, avogelsgesang, Mordante, sstefan1, libcxx-commits, ldionne, mgorny, arichardson, miyuki
Differential Revision: https://reviews.llvm.org/D131963
2022-08-13 20:33:12 +00:00
|
|
|
std::rethrow_exception(this->__exception_);
|
2011-11-29 18:15:50 +00:00
|
|
|
return std::move(*reinterpret_cast<_Rp*>(&__value_));
|
2010-08-27 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2022-09-05 22:33:34 +00:00
|
|
|
__add_lvalue_reference_t<_Rp> __assoc_state<_Rp>::copy() {
|
2010-08-27 20:10:19 +00:00
|
|
|
unique_lock<mutex> __lk(this->__mut_);
|
2010-08-30 18:46:21 +00:00
|
|
|
this->__sub_wait(__lk);
|
2010-08-27 20:10:19 +00:00
|
|
|
if (this->__exception_ != nullptr)
|
[libc++] Add custom clang-tidy checks
Reviewed By: #libc, ldionne
Spies: jwakely, beanz, smeenai, cfe-commits, tschuett, avogelsgesang, Mordante, sstefan1, libcxx-commits, ldionne, mgorny, arichardson, miyuki
Differential Revision: https://reviews.llvm.org/D131963
2022-08-13 20:33:12 +00:00
|
|
|
std::rethrow_exception(this->__exception_);
|
2011-11-29 18:15:50 +00:00
|
|
|
return *reinterpret_cast<_Rp*>(&__value_);
|
2010-08-27 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2023-11-01 22:56:43 +00:00
|
|
|
class __assoc_state<_Rp&> : public __assoc_sub_state {
|
2010-08-28 21:01:06 +00:00
|
|
|
typedef __assoc_sub_state base;
|
2011-11-29 18:15:50 +00:00
|
|
|
typedef _Rp* _Up;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-28 21:01:06 +00:00
|
|
|
protected:
|
2011-11-29 18:15:50 +00:00
|
|
|
_Up __value_;
|
2010-08-28 21:01:06 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
|
2010-08-28 21:01:06 +00:00
|
|
|
|
2023-12-18 19:01:33 +00:00
|
|
|
public:
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __arg);
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp& __arg);
|
2010-08-28 21:01:06 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _Rp& copy();
|
2010-08-28 21:01:06 +00:00
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
void __assoc_state<_Rp&>::__on_zero_shared() _NOEXCEPT {
|
2010-08-28 21:01:06 +00:00
|
|
|
delete this;
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
void __assoc_state<_Rp&>::set_value(_Rp& __arg) {
|
2010-08-28 21:01:06 +00:00
|
|
|
unique_lock<mutex> __lk(this->__mut_);
|
|
|
|
if (this->__has_value())
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::promise_already_satisfied);
|
2013-08-08 18:38:55 +00:00
|
|
|
__value_ = std::addressof(__arg);
|
2010-08-28 21:01:06 +00:00
|
|
|
this->__state_ |= base::__constructed | base::ready;
|
|
|
|
__cv_.notify_all();
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
void __assoc_state<_Rp&>::set_value_at_thread_exit(_Rp& __arg) {
|
2010-08-28 21:01:06 +00:00
|
|
|
unique_lock<mutex> __lk(this->__mut_);
|
|
|
|
if (this->__has_value())
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::promise_already_satisfied);
|
2013-08-08 18:38:55 +00:00
|
|
|
__value_ = std::addressof(__arg);
|
2010-08-28 21:01:06 +00:00
|
|
|
this->__state_ |= base::__constructed;
|
2010-10-14 19:18:04 +00:00
|
|
|
__thread_local_data()->__make_ready_at_thread_exit(this);
|
2010-08-28 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
_Rp& __assoc_state<_Rp&>::copy() {
|
2010-08-28 21:01:06 +00:00
|
|
|
unique_lock<mutex> __lk(this->__mut_);
|
2010-08-30 18:46:21 +00:00
|
|
|
this->__sub_wait(__lk);
|
2010-08-28 21:01:06 +00:00
|
|
|
if (this->__exception_ != nullptr)
|
[libc++] Add custom clang-tidy checks
Reviewed By: #libc, ldionne
Spies: jwakely, beanz, smeenai, cfe-commits, tschuett, avogelsgesang, Mordante, sstefan1, libcxx-commits, ldionne, mgorny, arichardson, miyuki
Differential Revision: https://reviews.llvm.org/D131963
2022-08-13 20:33:12 +00:00
|
|
|
std::rethrow_exception(this->__exception_);
|
2010-08-28 21:01:06 +00:00
|
|
|
return *__value_;
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Alloc>
|
2023-11-01 22:56:43 +00:00
|
|
|
class __assoc_state_alloc : public __assoc_state<_Rp> {
|
2011-11-29 18:15:50 +00:00
|
|
|
typedef __assoc_state<_Rp> base;
|
2010-08-27 20:10:19 +00:00
|
|
|
_Alloc __alloc_;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
|
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Alloc>
|
|
|
|
void __assoc_state_alloc<_Rp, _Alloc>::__on_zero_shared() _NOEXCEPT {
|
2010-08-27 20:10:19 +00:00
|
|
|
if (this->__state_ & base::__constructed)
|
2013-08-08 18:38:55 +00:00
|
|
|
reinterpret_cast<_Rp*>(std::addressof(this->__value_))->~_Rp();
|
2015-02-05 23:01:40 +00:00
|
|
|
typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
|
|
|
|
typedef allocator_traits<_Al> _ATraits;
|
2014-10-23 06:24:45 +00:00
|
|
|
typedef pointer_traits<typename _ATraits::pointer> _PTraits;
|
2015-02-05 23:01:40 +00:00
|
|
|
_Al __a(__alloc_);
|
2010-08-27 20:10:19 +00:00
|
|
|
this->~__assoc_state_alloc();
|
2014-10-23 06:24:45 +00:00
|
|
|
__a.deallocate(_PTraits::pointer_to(*this), 1);
|
2010-08-27 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Alloc>
|
2023-11-01 22:56:43 +00:00
|
|
|
class __assoc_state_alloc<_Rp&, _Alloc> : public __assoc_state<_Rp&> {
|
2011-11-29 18:15:50 +00:00
|
|
|
typedef __assoc_state<_Rp&> base;
|
2010-08-28 21:01:06 +00:00
|
|
|
_Alloc __alloc_;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
|
2010-08-28 21:01:06 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit __assoc_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
|
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Alloc>
|
|
|
|
void __assoc_state_alloc<_Rp&, _Alloc>::__on_zero_shared() _NOEXCEPT {
|
2015-02-05 23:01:40 +00:00
|
|
|
typedef typename __allocator_traits_rebind<_Alloc, __assoc_state_alloc>::type _Al;
|
|
|
|
typedef allocator_traits<_Al> _ATraits;
|
2014-10-23 06:24:45 +00:00
|
|
|
typedef pointer_traits<typename _ATraits::pointer> _PTraits;
|
2015-02-05 23:01:40 +00:00
|
|
|
_Al __a(__alloc_);
|
2010-08-28 21:01:06 +00:00
|
|
|
this->~__assoc_state_alloc();
|
2014-10-23 06:24:45 +00:00
|
|
|
__a.deallocate(_PTraits::pointer_to(*this), 1);
|
2010-08-28 21:01:06 +00:00
|
|
|
}
|
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
template <class _Alloc>
|
2023-11-01 22:56:43 +00:00
|
|
|
class __assoc_sub_state_alloc : public __assoc_sub_state {
|
2010-08-27 20:10:19 +00:00
|
|
|
typedef __assoc_sub_state base;
|
|
|
|
_Alloc __alloc_;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit __assoc_sub_state_alloc(const _Alloc& __a) : __alloc_(__a) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Alloc>
|
2011-05-28 14:41:13 +00:00
|
|
|
void __assoc_sub_state_alloc<_Alloc>::__on_zero_shared() _NOEXCEPT {
|
2015-02-05 23:01:40 +00:00
|
|
|
typedef typename __allocator_traits_rebind<_Alloc, __assoc_sub_state_alloc>::type _Al;
|
|
|
|
typedef allocator_traits<_Al> _ATraits;
|
2014-10-23 06:24:45 +00:00
|
|
|
typedef pointer_traits<typename _ATraits::pointer> _PTraits;
|
2015-02-05 23:01:40 +00:00
|
|
|
_Al __a(__alloc_);
|
2010-08-27 20:10:19 +00:00
|
|
|
this->~__assoc_sub_state_alloc();
|
2014-10-23 06:24:45 +00:00
|
|
|
__a.deallocate(_PTraits::pointer_to(*this), 1);
|
2010-08-27 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Fp>
|
2023-11-01 22:56:43 +00:00
|
|
|
class __deferred_assoc_state : public __assoc_state<_Rp> {
|
2011-11-29 18:15:50 +00:00
|
|
|
typedef __assoc_state<_Rp> base;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
_Fp __func_;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
|
|
|
public:
|
2011-11-29 18:15:50 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
|
2010-08-30 18:46:21 +00:00
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Fp>
|
|
|
|
inline __deferred_assoc_state<_Rp, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
|
2010-08-30 18:46:21 +00:00
|
|
|
this->__set_deferred();
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Fp>
|
|
|
|
void __deferred_assoc_state<_Rp, _Fp>::__execute() {
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
try {
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
this->set_value(__func_());
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
} catch (...) {
|
|
|
|
this->set_exception(current_exception());
|
|
|
|
}
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp>
|
2023-11-01 22:56:43 +00:00
|
|
|
class __deferred_assoc_state<void, _Fp> : public __assoc_sub_state {
|
2010-08-30 18:46:21 +00:00
|
|
|
typedef __assoc_sub_state base;
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
_Fp __func_;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
|
|
|
public:
|
2011-11-29 18:15:50 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit __deferred_assoc_state(_Fp&& __f);
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
|
2010-08-30 18:46:21 +00:00
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp>
|
|
|
|
inline __deferred_assoc_state<void, _Fp>::__deferred_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {
|
2010-08-30 18:46:21 +00:00
|
|
|
this->__set_deferred();
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp>
|
|
|
|
void __deferred_assoc_state<void, _Fp>::__execute() {
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
try {
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
__func_();
|
|
|
|
this->set_value();
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
} catch (...) {
|
|
|
|
this->set_exception(current_exception());
|
|
|
|
}
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Fp>
|
2023-11-01 22:56:43 +00:00
|
|
|
class __async_assoc_state : public __assoc_state<_Rp> {
|
2011-11-29 18:15:50 +00:00
|
|
|
typedef __assoc_state<_Rp> base;
|
2011-05-19 15:05:04 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
_Fp __func_;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __on_zero_shared() _NOEXCEPT;
|
2011-05-19 15:05:04 +00:00
|
|
|
|
|
|
|
public:
|
2011-11-29 18:15:50 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
|
2011-05-19 15:05:04 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __execute();
|
2011-05-19 15:05:04 +00:00
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Fp>
|
|
|
|
inline __async_assoc_state<_Rp, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
|
2011-05-19 15:05:04 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Fp>
|
|
|
|
void __async_assoc_state<_Rp, _Fp>::__execute() {
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2011-05-19 15:05:04 +00:00
|
|
|
try {
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2011-05-19 15:05:04 +00:00
|
|
|
this->set_value(__func_());
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2011-05-19 15:05:04 +00:00
|
|
|
} catch (...) {
|
|
|
|
this->set_exception(current_exception());
|
|
|
|
}
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2011-05-19 15:05:04 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Fp>
|
|
|
|
void __async_assoc_state<_Rp, _Fp>::__on_zero_shared() _NOEXCEPT {
|
2011-05-19 15:05:04 +00:00
|
|
|
this->wait();
|
|
|
|
base::__on_zero_shared();
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp>
|
2023-11-01 22:56:43 +00:00
|
|
|
class __async_assoc_state<void, _Fp> : public __assoc_sub_state {
|
2011-05-19 15:05:04 +00:00
|
|
|
typedef __assoc_sub_state base;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
_Fp __func_;
|
2011-05-19 15:05:04 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __on_zero_shared() _NOEXCEPT override;
|
2011-05-19 15:05:04 +00:00
|
|
|
|
|
|
|
public:
|
2011-11-29 18:15:50 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit __async_assoc_state(_Fp&& __f);
|
2011-05-19 15:05:04 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL void __execute() override;
|
2011-05-19 15:05:04 +00:00
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp>
|
|
|
|
inline __async_assoc_state<void, _Fp>::__async_assoc_state(_Fp&& __f) : __func_(std::forward<_Fp>(__f)) {}
|
2011-05-19 15:05:04 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp>
|
|
|
|
void __async_assoc_state<void, _Fp>::__execute() {
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2011-05-19 15:05:04 +00:00
|
|
|
try {
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2011-05-19 15:05:04 +00:00
|
|
|
__func_();
|
|
|
|
this->set_value();
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2011-05-19 15:05:04 +00:00
|
|
|
} catch (...) {
|
|
|
|
this->set_exception(current_exception());
|
|
|
|
}
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2011-05-19 15:05:04 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp>
|
|
|
|
void __async_assoc_state<void, _Fp>::__on_zero_shared() _NOEXCEPT {
|
2011-05-19 15:05:04 +00:00
|
|
|
this->wait();
|
|
|
|
base::__on_zero_shared();
|
|
|
|
}
|
|
|
|
|
2017-01-04 23:56:00 +00:00
|
|
|
template <class _Rp>
|
|
|
|
class _LIBCPP_TEMPLATE_VIS promise;
|
|
|
|
template <class _Rp>
|
|
|
|
class _LIBCPP_TEMPLATE_VIS shared_future;
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
// future
|
|
|
|
|
2017-01-04 23:56:00 +00:00
|
|
|
template <class _Rp>
|
|
|
|
class _LIBCPP_TEMPLATE_VIS future;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Fp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f);
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Fp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f);
|
2011-05-19 15:05:04 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2023-11-01 22:56:43 +00:00
|
|
|
class _LIBCPP_TEMPLATE_VIS future {
|
2011-11-29 18:15:50 +00:00
|
|
|
__assoc_state<_Rp>* __state_;
|
2010-08-27 20:10:19 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp>* __state);
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
template <class>
|
|
|
|
friend class promise;
|
2010-09-03 18:39:25 +00:00
|
|
|
template <class>
|
|
|
|
friend class shared_future;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _R1, class _Fp>
|
|
|
|
friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
|
|
|
|
template <class _R1, class _Fp>
|
|
|
|
friend future<_R1> __make_async_assoc_state(_Fp&& __f);
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
public:
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
|
2010-08-27 20:10:19 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
|
|
|
|
future(const future&) = delete;
|
|
|
|
future& operator=(const future&) = delete;
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
|
[libc++] Consistently replace `std::` qualification with `_VSTD::` or nothing. NFCI.
I used a lot of `git grep` to find places where `std::` was being used
outside of comments and assert-messages. There were three outcomes:
- Qualified function calls, e.g. `std::move` becomes `_VSTD::move`.
This is the most common case.
- Typenames that don't need qualification, e.g. `std::allocator` becomes `allocator`.
Leaving these as `_VSTD::allocator` would also be fine, but I decided
that removing the qualification is more consistent with existing practice.
- Names that specifically need un-versioned `std::` qualification,
or that I wasn't sure about. For example, I didn't touch any code in
<atomic>, <math.h>, <new>, or any ext/ or experimental/ headers;
and I didn't touch any instances of `std::type_info`.
In some deduction guides, we were accidentally using `class Alloc = typename std::allocator<T>`,
despite `std::allocator<T>`'s type-ness not being template-dependent.
Because `std::allocator` is a qualified name, this did parse as we intended;
but what we meant was simply `class Alloc = allocator<T>`.
Differential Revision: https://reviews.llvm.org/D92250
2020-11-27 16:02:06 +00:00
|
|
|
future(std::move(__rhs)).swap(*this);
|
2010-08-27 20:10:19 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI ~future();
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future<_Rp> share() _NOEXCEPT;
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
// retrieving the value
|
2010-09-22 14:16:26 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _Rp get();
|
2010-08-27 20:10:19 +00:00
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
// functions to check state
|
2010-09-22 14:16:26 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
|
|
|
|
template <class _Rep, class _Period>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
|
|
|
|
return __state_->wait_for(__rel_time);
|
|
|
|
}
|
|
|
|
template <class _Clock, class _Duration>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
|
|
|
|
return __state_->wait_until(__abs_time);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
future<_Rp>::future(__assoc_state<_Rp>* __state) : __state_(__state) {
|
2018-08-24 14:00:59 +00:00
|
|
|
__state_->__attach_future();
|
2010-08-27 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2010-08-30 18:46:21 +00:00
|
|
|
struct __release_shared_count {
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void operator()(__shared_count* __p) { __p->__release_shared(); }
|
2010-08-30 18:46:21 +00:00
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
future<_Rp>::~future() {
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_)
|
|
|
|
__state_->__release_shared();
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
_Rp future<_Rp>::get() {
|
2023-02-10 15:25:40 +00:00
|
|
|
unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
|
2011-11-29 18:15:50 +00:00
|
|
|
__assoc_state<_Rp>* __s = __state_;
|
2010-08-27 20:10:19 +00:00
|
|
|
__state_ = nullptr;
|
|
|
|
return __s->move();
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2023-11-01 22:56:43 +00:00
|
|
|
class _LIBCPP_TEMPLATE_VIS future<_Rp&> {
|
2011-11-29 18:15:50 +00:00
|
|
|
__assoc_state<_Rp&>* __state_;
|
2010-08-27 20:10:19 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
explicit _LIBCPP_HIDE_FROM_ABI future(__assoc_state<_Rp&>* __state);
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
template <class>
|
|
|
|
friend class promise;
|
2010-09-03 18:39:25 +00:00
|
|
|
template <class>
|
|
|
|
friend class shared_future;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _R1, class _Fp>
|
|
|
|
friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
|
|
|
|
template <class _R1, class _Fp>
|
|
|
|
friend future<_R1> __make_async_assoc_state(_Fp&& __f);
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
public:
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
|
2010-08-27 20:10:19 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
|
|
|
|
future(const future&) = delete;
|
|
|
|
future& operator=(const future&) = delete;
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
|
[libc++] Consistently replace `std::` qualification with `_VSTD::` or nothing. NFCI.
I used a lot of `git grep` to find places where `std::` was being used
outside of comments and assert-messages. There were three outcomes:
- Qualified function calls, e.g. `std::move` becomes `_VSTD::move`.
This is the most common case.
- Typenames that don't need qualification, e.g. `std::allocator` becomes `allocator`.
Leaving these as `_VSTD::allocator` would also be fine, but I decided
that removing the qualification is more consistent with existing practice.
- Names that specifically need un-versioned `std::` qualification,
or that I wasn't sure about. For example, I didn't touch any code in
<atomic>, <math.h>, <new>, or any ext/ or experimental/ headers;
and I didn't touch any instances of `std::type_info`.
In some deduction guides, we were accidentally using `class Alloc = typename std::allocator<T>`,
despite `std::allocator<T>`'s type-ness not being template-dependent.
Because `std::allocator` is a qualified name, this did parse as we intended;
but what we meant was simply `class Alloc = allocator<T>`.
Differential Revision: https://reviews.llvm.org/D92250
2020-11-27 16:02:06 +00:00
|
|
|
future(std::move(__rhs)).swap(*this);
|
2010-08-27 20:10:19 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI ~future();
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future<_Rp&> share() _NOEXCEPT;
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
// retrieving the value
|
2010-09-22 14:16:26 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _Rp& get();
|
2010-08-27 20:10:19 +00:00
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
// functions to check state
|
2010-09-22 14:16:26 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
|
|
|
|
template <class _Rep, class _Period>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
|
|
|
|
return __state_->wait_for(__rel_time);
|
|
|
|
}
|
|
|
|
template <class _Clock, class _Duration>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
|
|
|
|
return __state_->wait_until(__abs_time);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
future<_Rp&>::future(__assoc_state<_Rp&>* __state) : __state_(__state) {
|
2018-08-24 14:00:59 +00:00
|
|
|
__state_->__attach_future();
|
2010-08-27 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
future<_Rp&>::~future() {
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_)
|
|
|
|
__state_->__release_shared();
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
_Rp& future<_Rp&>::get() {
|
2023-02-10 15:25:40 +00:00
|
|
|
unique_ptr<__shared_count, __release_shared_count> __guard(__state_);
|
2011-11-29 18:15:50 +00:00
|
|
|
__assoc_state<_Rp&>* __s = __state_;
|
2010-08-27 20:10:19 +00:00
|
|
|
__state_ = nullptr;
|
|
|
|
return __s->copy();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2023-11-01 22:56:43 +00:00
|
|
|
class _LIBCPP_EXPORTED_FROM_ABI future<void> {
|
2010-08-27 20:10:19 +00:00
|
|
|
__assoc_sub_state* __state_;
|
|
|
|
|
|
|
|
explicit future(__assoc_sub_state* __state);
|
|
|
|
|
|
|
|
template <class>
|
|
|
|
friend class promise;
|
2010-09-03 18:39:25 +00:00
|
|
|
template <class>
|
|
|
|
friend class shared_future;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _R1, class _Fp>
|
|
|
|
friend future<_R1> __make_deferred_assoc_state(_Fp&& __f);
|
|
|
|
template <class _R1, class _Fp>
|
|
|
|
friend future<_R1> __make_async_assoc_state(_Fp&& __f);
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
public:
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI future() _NOEXCEPT : __state_(nullptr) {}
|
2010-08-27 20:10:19 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI future(future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
|
|
|
|
future(const future&) = delete;
|
|
|
|
future& operator=(const future&) = delete;
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI future& operator=(future&& __rhs) _NOEXCEPT {
|
[libc++] Consistently replace `std::` qualification with `_VSTD::` or nothing. NFCI.
I used a lot of `git grep` to find places where `std::` was being used
outside of comments and assert-messages. There were three outcomes:
- Qualified function calls, e.g. `std::move` becomes `_VSTD::move`.
This is the most common case.
- Typenames that don't need qualification, e.g. `std::allocator` becomes `allocator`.
Leaving these as `_VSTD::allocator` would also be fine, but I decided
that removing the qualification is more consistent with existing practice.
- Names that specifically need un-versioned `std::` qualification,
or that I wasn't sure about. For example, I didn't touch any code in
<atomic>, <math.h>, <new>, or any ext/ or experimental/ headers;
and I didn't touch any instances of `std::type_info`.
In some deduction guides, we were accidentally using `class Alloc = typename std::allocator<T>`,
despite `std::allocator<T>`'s type-ness not being template-dependent.
Because `std::allocator` is a qualified name, this did parse as we intended;
but what we meant was simply `class Alloc = allocator<T>`.
Differential Revision: https://reviews.llvm.org/D92250
2020-11-27 16:02:06 +00:00
|
|
|
future(std::move(__rhs)).swap(*this);
|
2010-08-27 20:10:19 +00:00
|
|
|
return *this;
|
|
|
|
}
|
2020-07-30 13:42:23 +00:00
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
~future();
|
2017-01-24 23:28:25 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future<void> share() _NOEXCEPT;
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
// retrieving the value
|
|
|
|
void get();
|
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void swap(future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
// functions to check state
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
|
|
|
|
template <class _Rep, class _Period>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
|
|
|
|
return __state_->wait_for(__rel_time);
|
|
|
|
}
|
|
|
|
template <class _Clock, class _Duration>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
|
|
|
|
return __state_->wait_until(__abs_time);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2012-07-21 17:46:55 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI void swap(future<_Rp>& __x, future<_Rp>& __y) _NOEXCEPT {
|
2010-09-03 18:39:25 +00:00
|
|
|
__x.swap(__y);
|
|
|
|
}
|
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
// promise<R>
|
|
|
|
|
2011-06-14 19:58:17 +00:00
|
|
|
template <class _Callable>
|
|
|
|
class packaged_task;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2023-11-01 22:56:43 +00:00
|
|
|
class _LIBCPP_TEMPLATE_VIS promise {
|
2011-11-29 18:15:50 +00:00
|
|
|
__assoc_state<_Rp>* __state_;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-30 18:46:21 +00:00
|
|
|
template <class>
|
|
|
|
friend class packaged_task;
|
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
public:
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI promise();
|
2010-08-27 20:10:19 +00:00
|
|
|
template <class _Alloc>
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Alloc& __a);
|
2010-08-27 20:10:19 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
|
|
|
|
promise(const promise& __rhs) = delete;
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI ~promise();
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
// assignment
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
|
[libc++] Consistently replace `std::` qualification with `_VSTD::` or nothing. NFCI.
I used a lot of `git grep` to find places where `std::` was being used
outside of comments and assert-messages. There were three outcomes:
- Qualified function calls, e.g. `std::move` becomes `_VSTD::move`.
This is the most common case.
- Typenames that don't need qualification, e.g. `std::allocator` becomes `allocator`.
Leaving these as `_VSTD::allocator` would also be fine, but I decided
that removing the qualification is more consistent with existing practice.
- Names that specifically need un-versioned `std::` qualification,
or that I wasn't sure about. For example, I didn't touch any code in
<atomic>, <math.h>, <new>, or any ext/ or experimental/ headers;
and I didn't touch any instances of `std::type_info`.
In some deduction guides, we were accidentally using `class Alloc = typename std::allocator<T>`,
despite `std::allocator<T>`'s type-ness not being template-dependent.
Because `std::allocator` is a qualified name, this did parse as we intended;
but what we meant was simply `class Alloc = allocator<T>`.
Differential Revision: https://reviews.llvm.org/D92250
2020-11-27 16:02:06 +00:00
|
|
|
promise(std::move(__rhs)).swap(*this);
|
2010-08-27 20:10:19 +00:00
|
|
|
return *this;
|
2023-12-18 19:01:33 +00:00
|
|
|
}
|
2010-08-27 20:10:19 +00:00
|
|
|
promise& operator=(const promise& __rhs) = delete;
|
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
|
2020-07-30 13:42:23 +00:00
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
// retrieving the result
|
2010-09-22 14:16:26 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI future<_Rp> get_future();
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
// setting the result
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void set_value(const _Rp& __r);
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void set_value(_Rp&& __r);
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
// setting the result with deferred notification
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(const _Rp& __r);
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&& __r);
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
|
2010-08-27 20:10:19 +00:00
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
promise<_Rp>::promise() : __state_(new __assoc_state<_Rp>) {}
|
2010-08-27 20:10:19 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2010-08-27 20:10:19 +00:00
|
|
|
template <class _Alloc>
|
2011-11-29 18:15:50 +00:00
|
|
|
promise<_Rp>::promise(allocator_arg_t, const _Alloc& __a0) {
|
2014-10-23 06:24:45 +00:00
|
|
|
typedef __assoc_state_alloc<_Rp, _Alloc> _State;
|
|
|
|
typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
|
2010-08-27 20:10:19 +00:00
|
|
|
typedef __allocator_destructor<_A2> _D2;
|
|
|
|
_A2 __a(__a0);
|
2014-10-23 06:24:45 +00:00
|
|
|
unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
|
2020-12-12 01:30:28 +00:00
|
|
|
::new ((void*)std::addressof(*__hold.get())) _State(__a0);
|
2014-10-23 06:24:45 +00:00
|
|
|
__state_ = std::addressof(*__hold.release());
|
2010-08-27 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
promise<_Rp>::~promise() {
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_) {
|
|
|
|
if (!__state_->__has_value() && __state_->use_count() > 1)
|
2023-09-26 16:54:16 +00:00
|
|
|
__state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
|
2010-08-27 20:10:19 +00:00
|
|
|
__state_->__release_shared();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
future<_Rp> promise<_Rp>::get_future() {
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2011-11-29 18:15:50 +00:00
|
|
|
return future<_Rp>(__state_);
|
2010-08-27 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
void promise<_Rp>::set_value(const _Rp& __r) {
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2010-08-27 20:10:19 +00:00
|
|
|
__state_->set_value(__r);
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
void promise<_Rp>::set_value(_Rp&& __r) {
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2011-06-30 21:18:19 +00:00
|
|
|
__state_->set_value(std::move(__r));
|
2010-08-27 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
void promise<_Rp>::set_exception(exception_ptr __p) {
|
2023-11-08 02:12:15 +00:00
|
|
|
_LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2010-08-27 20:10:19 +00:00
|
|
|
__state_->set_exception(__p);
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
void promise<_Rp>::set_value_at_thread_exit(const _Rp& __r) {
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2010-08-27 20:10:19 +00:00
|
|
|
__state_->set_value_at_thread_exit(__r);
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
void promise<_Rp>::set_value_at_thread_exit(_Rp&& __r) {
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2011-06-30 21:18:19 +00:00
|
|
|
__state_->set_value_at_thread_exit(std::move(__r));
|
2010-08-27 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
void promise<_Rp>::set_exception_at_thread_exit(exception_ptr __p) {
|
2023-11-08 02:12:15 +00:00
|
|
|
_LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2010-08-27 20:10:19 +00:00
|
|
|
__state_->set_exception_at_thread_exit(__p);
|
|
|
|
}
|
|
|
|
|
|
|
|
// promise<R&>
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2023-11-01 22:56:43 +00:00
|
|
|
class _LIBCPP_TEMPLATE_VIS promise<_Rp&> {
|
2011-11-29 18:15:50 +00:00
|
|
|
__assoc_state<_Rp&>* __state_;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
|
2010-08-30 18:46:21 +00:00
|
|
|
|
|
|
|
template <class>
|
|
|
|
friend class packaged_task;
|
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
public:
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI promise();
|
2010-08-27 20:10:19 +00:00
|
|
|
template <class _Allocator>
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI promise(allocator_arg_t, const _Allocator& __a);
|
2010-08-27 20:10:19 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
|
|
|
|
promise(const promise& __rhs) = delete;
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI ~promise();
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
// assignment
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
|
[libc++] Consistently replace `std::` qualification with `_VSTD::` or nothing. NFCI.
I used a lot of `git grep` to find places where `std::` was being used
outside of comments and assert-messages. There were three outcomes:
- Qualified function calls, e.g. `std::move` becomes `_VSTD::move`.
This is the most common case.
- Typenames that don't need qualification, e.g. `std::allocator` becomes `allocator`.
Leaving these as `_VSTD::allocator` would also be fine, but I decided
that removing the qualification is more consistent with existing practice.
- Names that specifically need un-versioned `std::` qualification,
or that I wasn't sure about. For example, I didn't touch any code in
<atomic>, <math.h>, <new>, or any ext/ or experimental/ headers;
and I didn't touch any instances of `std::type_info`.
In some deduction guides, we were accidentally using `class Alloc = typename std::allocator<T>`,
despite `std::allocator<T>`'s type-ness not being template-dependent.
Because `std::allocator` is a qualified name, this did parse as we intended;
but what we meant was simply `class Alloc = allocator<T>`.
Differential Revision: https://reviews.llvm.org/D92250
2020-11-27 16:02:06 +00:00
|
|
|
promise(std::move(__rhs)).swap(*this);
|
2010-08-27 20:10:19 +00:00
|
|
|
return *this;
|
2023-12-18 19:01:33 +00:00
|
|
|
}
|
2010-08-27 20:10:19 +00:00
|
|
|
promise& operator=(const promise& __rhs) = delete;
|
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
|
2020-07-30 13:42:23 +00:00
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
// retrieving the result
|
2010-09-22 14:16:26 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI future<_Rp&> get_future();
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
// setting the result
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void set_value(_Rp& __r);
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void set_exception(exception_ptr __p);
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
// setting the result with deferred notification
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void set_value_at_thread_exit(_Rp&);
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void set_exception_at_thread_exit(exception_ptr __p);
|
2010-08-27 20:10:19 +00:00
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
promise<_Rp&>::promise() : __state_(new __assoc_state<_Rp&>) {}
|
2010-08-27 20:10:19 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2010-08-27 20:10:19 +00:00
|
|
|
template <class _Alloc>
|
2011-11-29 18:15:50 +00:00
|
|
|
promise<_Rp&>::promise(allocator_arg_t, const _Alloc& __a0) {
|
2014-10-23 06:24:45 +00:00
|
|
|
typedef __assoc_state_alloc<_Rp&, _Alloc> _State;
|
|
|
|
typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
|
2010-08-27 20:10:19 +00:00
|
|
|
typedef __allocator_destructor<_A2> _D2;
|
|
|
|
_A2 __a(__a0);
|
2014-10-23 06:24:45 +00:00
|
|
|
unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
|
2020-12-12 01:30:28 +00:00
|
|
|
::new ((void*)std::addressof(*__hold.get())) _State(__a0);
|
2014-10-23 06:24:45 +00:00
|
|
|
__state_ = std::addressof(*__hold.release());
|
2010-08-27 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
promise<_Rp&>::~promise() {
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_) {
|
|
|
|
if (!__state_->__has_value() && __state_->use_count() > 1)
|
2023-09-26 16:54:16 +00:00
|
|
|
__state_->set_exception(make_exception_ptr(future_error(make_error_code(future_errc::broken_promise))));
|
2010-08-27 20:10:19 +00:00
|
|
|
__state_->__release_shared();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
future<_Rp&> promise<_Rp&>::get_future() {
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2011-11-29 18:15:50 +00:00
|
|
|
return future<_Rp&>(__state_);
|
2010-08-27 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
void promise<_Rp&>::set_value(_Rp& __r) {
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2010-08-27 20:10:19 +00:00
|
|
|
__state_->set_value(__r);
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
void promise<_Rp&>::set_exception(exception_ptr __p) {
|
2023-11-08 02:12:15 +00:00
|
|
|
_LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception: received nullptr");
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2010-08-27 20:10:19 +00:00
|
|
|
__state_->set_exception(__p);
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
void promise<_Rp&>::set_value_at_thread_exit(_Rp& __r) {
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2010-08-27 20:10:19 +00:00
|
|
|
__state_->set_value_at_thread_exit(__r);
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
void promise<_Rp&>::set_exception_at_thread_exit(exception_ptr __p) {
|
2023-11-08 02:12:15 +00:00
|
|
|
_LIBCPP_ASSERT_NON_NULL(__p != nullptr, "promise::set_exception_at_thread_exit: received nullptr");
|
2010-08-27 20:10:19 +00:00
|
|
|
if (__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2010-08-27 20:10:19 +00:00
|
|
|
__state_->set_exception_at_thread_exit(__p);
|
|
|
|
}
|
|
|
|
|
|
|
|
// promise<void>
|
|
|
|
|
|
|
|
template <>
|
2023-11-01 22:56:43 +00:00
|
|
|
class _LIBCPP_EXPORTED_FROM_ABI promise<void> {
|
2010-08-27 20:10:19 +00:00
|
|
|
__assoc_sub_state* __state_;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit promise(nullptr_t) _NOEXCEPT : __state_(nullptr) {}
|
2010-08-30 18:46:21 +00:00
|
|
|
|
|
|
|
template <class>
|
|
|
|
friend class packaged_task;
|
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
public:
|
|
|
|
promise();
|
|
|
|
template <class _Allocator>
|
2017-03-02 03:22:18 +00:00
|
|
|
_LIBCPP_METHOD_TEMPLATE_IMPLICIT_INSTANTIATION_VIS promise(allocator_arg_t, const _Allocator& __a);
|
2010-08-27 20:10:19 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI promise(promise&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) { __rhs.__state_ = nullptr; }
|
|
|
|
promise(const promise& __rhs) = delete;
|
|
|
|
~promise();
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-27 20:10:19 +00:00
|
|
|
// assignment
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI promise& operator=(promise&& __rhs) _NOEXCEPT {
|
[libc++] Consistently replace `std::` qualification with `_VSTD::` or nothing. NFCI.
I used a lot of `git grep` to find places where `std::` was being used
outside of comments and assert-messages. There were three outcomes:
- Qualified function calls, e.g. `std::move` becomes `_VSTD::move`.
This is the most common case.
- Typenames that don't need qualification, e.g. `std::allocator` becomes `allocator`.
Leaving these as `_VSTD::allocator` would also be fine, but I decided
that removing the qualification is more consistent with existing practice.
- Names that specifically need un-versioned `std::` qualification,
or that I wasn't sure about. For example, I didn't touch any code in
<atomic>, <math.h>, <new>, or any ext/ or experimental/ headers;
and I didn't touch any instances of `std::type_info`.
In some deduction guides, we were accidentally using `class Alloc = typename std::allocator<T>`,
despite `std::allocator<T>`'s type-ness not being template-dependent.
Because `std::allocator` is a qualified name, this did parse as we intended;
but what we meant was simply `class Alloc = allocator<T>`.
Differential Revision: https://reviews.llvm.org/D92250
2020-11-27 16:02:06 +00:00
|
|
|
promise(std::move(__rhs)).swap(*this);
|
2010-08-27 20:10:19 +00:00
|
|
|
return *this;
|
2023-12-18 19:01:33 +00:00
|
|
|
}
|
2010-08-27 20:10:19 +00:00
|
|
|
promise& operator=(const promise& __rhs) = delete;
|
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void swap(promise& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
|
2020-07-30 13:42:23 +00:00
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
// retrieving the result
|
|
|
|
future<void> get_future();
|
2010-08-27 20:10:19 +00:00
|
|
|
|
|
|
|
// setting the result
|
|
|
|
void set_value();
|
|
|
|
void set_exception(exception_ptr __p);
|
|
|
|
|
|
|
|
// setting the result with deferred notification
|
|
|
|
void set_value_at_thread_exit();
|
|
|
|
void set_exception_at_thread_exit(exception_ptr __p);
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class _Alloc>
|
|
|
|
promise<void>::promise(allocator_arg_t, const _Alloc& __a0) {
|
2014-10-23 06:24:45 +00:00
|
|
|
typedef __assoc_sub_state_alloc<_Alloc> _State;
|
|
|
|
typedef typename __allocator_traits_rebind<_Alloc, _State>::type _A2;
|
2010-08-27 20:10:19 +00:00
|
|
|
typedef __allocator_destructor<_A2> _D2;
|
|
|
|
_A2 __a(__a0);
|
2014-10-23 06:24:45 +00:00
|
|
|
unique_ptr<_State, _D2> __hold(__a.allocate(1), _D2(__a, 1));
|
2020-12-12 01:30:28 +00:00
|
|
|
::new ((void*)std::addressof(*__hold.get())) _State(__a0);
|
2014-10-23 06:24:45 +00:00
|
|
|
__state_ = std::addressof(*__hold.release());
|
2010-08-27 20:10:19 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2012-07-21 17:46:55 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI void swap(promise<_Rp>& __x, promise<_Rp>& __y) _NOEXCEPT {
|
2010-08-27 20:10:19 +00:00
|
|
|
__x.swap(__y);
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Alloc>
|
2017-01-04 23:56:00 +00:00
|
|
|
struct _LIBCPP_TEMPLATE_VIS uses_allocator<promise<_Rp>, _Alloc> : public true_type {};
|
2010-08-27 20:10:19 +00:00
|
|
|
|
2010-08-30 18:46:21 +00:00
|
|
|
// packaged_task
|
|
|
|
|
|
|
|
template <class _Fp>
|
|
|
|
class __packaged_task_base;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2021-03-24 22:31:58 +00:00
|
|
|
template <class _Rp, class... _ArgTypes>
|
2010-08-30 18:46:21 +00:00
|
|
|
class __packaged_task_base<_Rp(_ArgTypes...)> {
|
|
|
|
__packaged_task_base(const __packaged_task_base&);
|
|
|
|
__packaged_task_base& operator=(const __packaged_task_base&);
|
|
|
|
|
|
|
|
public:
|
|
|
|
_LIBCPP_HIDE_FROM_ABI __packaged_task_base() {}
|
2022-12-21 15:08:54 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL
|
2010-08-30 18:46:21 +00:00
|
|
|
virtual ~__packaged_task_base() {}
|
2012-07-21 17:46:55 +00:00
|
|
|
virtual void __move_to(__packaged_task_base*) _NOEXCEPT = 0;
|
2010-08-30 18:46:21 +00:00
|
|
|
virtual void destroy() = 0;
|
|
|
|
virtual void destroy_deallocate() = 0;
|
2011-11-29 18:15:50 +00:00
|
|
|
virtual _Rp operator()(_ArgTypes&&...) = 0;
|
2010-08-30 18:46:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
template <class _FD, class _Alloc, class _FB>
|
|
|
|
class __packaged_task_func;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-30 18:46:21 +00:00
|
|
|
template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
|
|
|
|
class __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)> : public __packaged_task_base<_Rp(_ArgTypes...)> {
|
2011-11-29 18:15:50 +00:00
|
|
|
__compressed_pair<_Fp, _Alloc> __f_;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
|
|
|
public:
|
2019-12-16 23:23:39 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(const _Fp& __f) : __f_(__f, __default_init_tag()) {}
|
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit __packaged_task_func(_Fp&& __f) : __f_(std::move(__f), __default_init_tag()) {}
|
2011-11-29 18:15:50 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI __packaged_task_func(const _Fp& __f, const _Alloc& __a) : __f_(__f, __a) {}
|
|
|
|
_LIBCPP_HIDE_FROM_ABI __packaged_task_func(_Fp&& __f, const _Alloc& __a) : __f_(std::move(__f), __a) {}
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void __move_to(__packaged_task_base<_Rp(_ArgTypes...)>*) _NOEXCEPT;
|
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy();
|
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual void destroy_deallocate();
|
|
|
|
_LIBCPP_HIDE_FROM_ABI_VIRTUAL virtual _Rp operator()(_ArgTypes&&... __args);
|
2010-08-30 18:46:21 +00:00
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
|
|
|
|
void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::__move_to(
|
2012-07-21 17:46:55 +00:00
|
|
|
__packaged_task_base<_Rp(_ArgTypes...)>* __p) _NOEXCEPT {
|
2020-12-12 01:30:28 +00:00
|
|
|
::new ((void*)__p) __packaged_task_func(std::move(__f_.first()), std::move(__f_.second()));
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
|
|
|
|
void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy() {
|
|
|
|
__f_.~__compressed_pair<_Fp, _Alloc>();
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
|
|
|
|
void __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::destroy_deallocate() {
|
2014-10-23 06:24:45 +00:00
|
|
|
typedef typename __allocator_traits_rebind<_Alloc, __packaged_task_func>::type _Ap;
|
|
|
|
typedef allocator_traits<_Ap> _ATraits;
|
|
|
|
typedef pointer_traits<typename _ATraits::pointer> _PTraits;
|
2011-11-29 18:15:50 +00:00
|
|
|
_Ap __a(__f_.second());
|
|
|
|
__f_.~__compressed_pair<_Fp, _Alloc>();
|
2014-10-23 06:24:45 +00:00
|
|
|
__a.deallocate(_PTraits::pointer_to(*this), 1);
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp, class _Alloc, class _Rp, class... _ArgTypes>
|
|
|
|
_Rp __packaged_task_func<_Fp, _Alloc, _Rp(_ArgTypes...)>::operator()(_ArgTypes&&... __arg) {
|
2020-12-16 00:32:29 +00:00
|
|
|
return std::__invoke(__f_.first(), std::forward<_ArgTypes>(__arg)...);
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2011-06-14 19:58:17 +00:00
|
|
|
template <class _Callable>
|
|
|
|
class __packaged_task_function;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class... _ArgTypes>
|
2023-11-01 22:56:43 +00:00
|
|
|
class __packaged_task_function<_Rp(_ArgTypes...)> {
|
2011-11-29 18:15:50 +00:00
|
|
|
typedef __packaged_task_base<_Rp(_ArgTypes...)> __base;
|
2020-08-06 18:32:33 +00:00
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_NO_CFI __base* __get_buf() { return (__base*)&__buf_; }
|
|
|
|
|
2022-12-11 01:10:31 +00:00
|
|
|
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
|
2013-01-21 17:26:55 +00:00
|
|
|
typename aligned_storage<3 * sizeof(void*)>::type __buf_;
|
2022-12-11 01:10:31 +00:00
|
|
|
_LIBCPP_SUPPRESS_DEPRECATED_POP
|
2010-08-30 18:46:21 +00:00
|
|
|
__base* __f_;
|
|
|
|
|
|
|
|
public:
|
2011-11-29 18:15:50 +00:00
|
|
|
typedef _Rp result_type;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
|
|
|
// construct/copy/destroy:
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI __packaged_task_function() _NOEXCEPT : __f_(nullptr) {}
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp>
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI __packaged_task_function(_Fp&& __f);
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp, class _Alloc>
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI __packaged_task_function(allocator_arg_t, const _Alloc& __a, _Fp&& __f);
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI __packaged_task_function(__packaged_task_function&&) _NOEXCEPT;
|
|
|
|
_LIBCPP_HIDE_FROM_ABI __packaged_task_function& operator=(__packaged_task_function&&) _NOEXCEPT;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
|
|
|
__packaged_task_function(const __packaged_task_function&) = delete;
|
|
|
|
__packaged_task_function& operator=(const __packaged_task_function&) = delete;
|
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI ~__packaged_task_function();
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void swap(__packaged_task_function&) _NOEXCEPT;
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _LIBCPP_HIDE_FROM_ABI _Rp operator()(_ArgTypes...) const;
|
2010-08-30 18:46:21 +00:00
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class... _ArgTypes>
|
2012-07-21 17:46:55 +00:00
|
|
|
__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(__packaged_task_function&& __f) _NOEXCEPT {
|
2010-08-30 18:46:21 +00:00
|
|
|
if (__f.__f_ == nullptr)
|
|
|
|
__f_ = nullptr;
|
2020-08-06 18:32:33 +00:00
|
|
|
else if (__f.__f_ == __f.__get_buf()) {
|
|
|
|
__f.__f_->__move_to(__get_buf());
|
2010-08-30 18:46:21 +00:00
|
|
|
__f_ = (__base*)&__buf_;
|
|
|
|
} else {
|
|
|
|
__f_ = __f.__f_;
|
|
|
|
__f.__f_ = nullptr;
|
2023-12-18 19:01:33 +00:00
|
|
|
}
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class... _ArgTypes>
|
|
|
|
template <class _Fp>
|
|
|
|
__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(_Fp&& __f) : __f_(nullptr) {
|
2023-03-27 16:22:23 +00:00
|
|
|
typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
|
2011-11-29 18:15:50 +00:00
|
|
|
typedef __packaged_task_func<_FR, allocator<_FR>, _Rp(_ArgTypes...)> _FF;
|
2010-08-30 18:46:21 +00:00
|
|
|
if (sizeof(_FF) <= sizeof(__buf_)) {
|
2020-12-12 01:30:28 +00:00
|
|
|
::new ((void*)&__buf_) _FF(std::forward<_Fp>(__f));
|
2010-08-30 18:46:21 +00:00
|
|
|
__f_ = (__base*)&__buf_;
|
|
|
|
} else {
|
2011-11-29 18:15:50 +00:00
|
|
|
typedef allocator<_FF> _Ap;
|
|
|
|
_Ap __a;
|
|
|
|
typedef __allocator_destructor<_Ap> _Dp;
|
|
|
|
unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
|
2020-12-12 01:30:28 +00:00
|
|
|
::new ((void*)__hold.get()) _FF(std::forward<_Fp>(__f), allocator<_FR>(__a));
|
2010-08-30 18:46:21 +00:00
|
|
|
__f_ = __hold.release();
|
2023-12-18 19:01:33 +00:00
|
|
|
}
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class... _ArgTypes>
|
|
|
|
template <class _Fp, class _Alloc>
|
|
|
|
__packaged_task_function<_Rp(_ArgTypes...)>::__packaged_task_function(allocator_arg_t, const _Alloc& __a0, _Fp&& __f)
|
2010-08-30 18:46:21 +00:00
|
|
|
: __f_(nullptr) {
|
2023-03-27 16:22:23 +00:00
|
|
|
typedef __libcpp_remove_reference_t<__decay_t<_Fp> > _FR;
|
2011-11-29 18:15:50 +00:00
|
|
|
typedef __packaged_task_func<_FR, _Alloc, _Rp(_ArgTypes...)> _FF;
|
2010-08-30 18:46:21 +00:00
|
|
|
if (sizeof(_FF) <= sizeof(__buf_)) {
|
|
|
|
__f_ = (__base*)&__buf_;
|
2020-12-12 01:30:28 +00:00
|
|
|
::new ((void*)__f_) _FF(std::forward<_Fp>(__f));
|
2010-08-30 18:46:21 +00:00
|
|
|
} else {
|
2014-10-23 06:24:45 +00:00
|
|
|
typedef typename __allocator_traits_rebind<_Alloc, _FF>::type _Ap;
|
2011-11-29 18:15:50 +00:00
|
|
|
_Ap __a(__a0);
|
|
|
|
typedef __allocator_destructor<_Ap> _Dp;
|
|
|
|
unique_ptr<__base, _Dp> __hold(__a.allocate(1), _Dp(__a, 1));
|
2020-12-12 01:30:28 +00:00
|
|
|
::new ((void*)std::addressof(*__hold.get())) _FF(std::forward<_Fp>(__f), _Alloc(__a));
|
2014-10-23 06:24:45 +00:00
|
|
|
__f_ = std::addressof(*__hold.release());
|
2023-12-18 19:01:33 +00:00
|
|
|
}
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class... _ArgTypes>
|
|
|
|
__packaged_task_function<_Rp(_ArgTypes...)>&
|
2012-07-21 17:46:55 +00:00
|
|
|
__packaged_task_function<_Rp(_ArgTypes...)>::operator=(__packaged_task_function&& __f) _NOEXCEPT {
|
2020-08-06 18:32:33 +00:00
|
|
|
if (__f_ == __get_buf())
|
2010-08-30 18:46:21 +00:00
|
|
|
__f_->destroy();
|
|
|
|
else if (__f_)
|
|
|
|
__f_->destroy_deallocate();
|
|
|
|
__f_ = nullptr;
|
|
|
|
if (__f.__f_ == nullptr)
|
|
|
|
__f_ = nullptr;
|
2020-08-06 18:32:33 +00:00
|
|
|
else if (__f.__f_ == __f.__get_buf()) {
|
|
|
|
__f.__f_->__move_to(__get_buf());
|
|
|
|
__f_ = __get_buf();
|
2010-08-30 18:46:21 +00:00
|
|
|
} else {
|
|
|
|
__f_ = __f.__f_;
|
|
|
|
__f.__f_ = nullptr;
|
|
|
|
}
|
2012-10-13 02:03:45 +00:00
|
|
|
return *this;
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class... _ArgTypes>
|
|
|
|
__packaged_task_function<_Rp(_ArgTypes...)>::~__packaged_task_function() {
|
2020-08-06 18:32:33 +00:00
|
|
|
if (__f_ == __get_buf())
|
2010-08-30 18:46:21 +00:00
|
|
|
__f_->destroy();
|
|
|
|
else if (__f_)
|
|
|
|
__f_->destroy_deallocate();
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class... _ArgTypes>
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_NO_CFI void __packaged_task_function<_Rp(_ArgTypes...)>::swap(__packaged_task_function& __f) _NOEXCEPT {
|
2010-08-30 18:46:21 +00:00
|
|
|
if (__f_ == (__base*)&__buf_ && __f.__f_ == (__base*)&__f.__buf_) {
|
2022-12-11 01:10:31 +00:00
|
|
|
_LIBCPP_SUPPRESS_DEPRECATED_PUSH
|
2010-08-30 18:46:21 +00:00
|
|
|
typename aligned_storage<sizeof(__buf_)>::type __tempbuf;
|
2022-12-11 01:10:31 +00:00
|
|
|
_LIBCPP_SUPPRESS_DEPRECATED_POP
|
2010-08-30 18:46:21 +00:00
|
|
|
__base* __t = (__base*)&__tempbuf;
|
|
|
|
__f_->__move_to(__t);
|
|
|
|
__f_->destroy();
|
|
|
|
__f_ = nullptr;
|
|
|
|
__f.__f_->__move_to((__base*)&__buf_);
|
|
|
|
__f.__f_->destroy();
|
|
|
|
__f.__f_ = nullptr;
|
|
|
|
__f_ = (__base*)&__buf_;
|
|
|
|
__t->__move_to((__base*)&__f.__buf_);
|
|
|
|
__t->destroy();
|
|
|
|
__f.__f_ = (__base*)&__f.__buf_;
|
|
|
|
} else if (__f_ == (__base*)&__buf_) {
|
|
|
|
__f_->__move_to((__base*)&__f.__buf_);
|
|
|
|
__f_->destroy();
|
|
|
|
__f_ = __f.__f_;
|
|
|
|
__f.__f_ = (__base*)&__f.__buf_;
|
|
|
|
} else if (__f.__f_ == (__base*)&__f.__buf_) {
|
|
|
|
__f.__f_->__move_to((__base*)&__buf_);
|
|
|
|
__f.__f_->destroy();
|
|
|
|
__f.__f_ = __f_;
|
|
|
|
__f_ = (__base*)&__buf_;
|
|
|
|
} else
|
2011-06-30 21:18:19 +00:00
|
|
|
std::swap(__f_, __f.__f_);
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class... _ArgTypes>
|
|
|
|
inline _Rp __packaged_task_function<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __arg) const {
|
2011-06-30 21:18:19 +00:00
|
|
|
return (*__f_)(std::forward<_ArgTypes>(__arg)...);
|
2023-12-18 19:01:33 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class... _ArgTypes>
|
2023-11-01 22:56:43 +00:00
|
|
|
class _LIBCPP_TEMPLATE_VIS packaged_task<_Rp(_ArgTypes...)> {
|
2010-08-30 18:46:21 +00:00
|
|
|
public:
|
2016-06-01 21:05:53 +00:00
|
|
|
typedef _Rp result_type; // extension
|
2010-08-30 18:46:21 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
__packaged_task_function<result_type(_ArgTypes...)> __f_;
|
|
|
|
promise<result_type> __p_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// construction and destruction
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
|
2022-09-05 22:33:34 +00:00
|
|
|
template <class _Fp, class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
|
2011-11-29 18:15:50 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
|
2022-09-05 22:33:34 +00:00
|
|
|
template <class _Fp, class _Allocator, class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
|
2017-11-27 20:47:54 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
|
2023-03-06 20:43:26 +00:00
|
|
|
: __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
|
2010-08-30 18:46:21 +00:00
|
|
|
// ~packaged_task() = default;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-30 18:46:21 +00:00
|
|
|
// no copy
|
2012-07-21 19:34:12 +00:00
|
|
|
packaged_task(const packaged_task&) = delete;
|
|
|
|
packaged_task& operator=(const packaged_task&) = delete;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-30 18:46:21 +00:00
|
|
|
// move support
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
|
2011-06-30 21:18:19 +00:00
|
|
|
: __f_(std::move(__other.__f_)),
|
|
|
|
__p_(std::move(__other.__p_)) {}
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
|
2011-06-30 21:18:19 +00:00
|
|
|
__f_ = std::move(__other.__f_);
|
|
|
|
__p_ = std::move(__other.__p_);
|
2010-08-27 20:10:19 +00:00
|
|
|
return *this;
|
2023-12-18 19:01:33 +00:00
|
|
|
}
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
|
2010-08-30 18:46:21 +00:00
|
|
|
__f_.swap(__other.__f_);
|
|
|
|
__p_.swap(__other.__p_);
|
2023-12-18 19:01:33 +00:00
|
|
|
}
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2012-07-21 19:34:12 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
|
2010-08-30 18:46:21 +00:00
|
|
|
|
|
|
|
// result retrieval
|
2010-09-22 14:16:26 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
|
2010-08-30 18:46:21 +00:00
|
|
|
|
|
|
|
// execution
|
2010-09-22 14:16:26 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
|
2010-08-30 18:46:21 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
|
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void reset();
|
2010-08-30 18:46:21 +00:00
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class... _ArgTypes>
|
|
|
|
void packaged_task<_Rp(_ArgTypes...)>::operator()(_ArgTypes... __args) {
|
2010-08-30 18:46:21 +00:00
|
|
|
if (__p_.__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2010-08-30 18:46:21 +00:00
|
|
|
if (__p_.__state_->__has_value())
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::promise_already_satisfied);
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
try {
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2011-06-30 21:18:19 +00:00
|
|
|
__p_.set_value(__f_(std::forward<_ArgTypes>(__args)...));
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
} catch (...) {
|
|
|
|
__p_.set_exception(current_exception());
|
|
|
|
}
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class... _ArgTypes>
|
|
|
|
void packaged_task<_Rp(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
|
2010-08-30 18:46:21 +00:00
|
|
|
if (__p_.__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2010-08-30 18:46:21 +00:00
|
|
|
if (__p_.__state_->__has_value())
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::promise_already_satisfied);
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
try {
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2011-06-30 21:18:19 +00:00
|
|
|
__p_.set_value_at_thread_exit(__f_(std::forward<_ArgTypes>(__args)...));
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
} catch (...) {
|
|
|
|
__p_.set_exception_at_thread_exit(current_exception());
|
|
|
|
}
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class... _ArgTypes>
|
|
|
|
void packaged_task<_Rp(_ArgTypes...)>::reset() {
|
2010-11-30 20:23:32 +00:00
|
|
|
if (!valid())
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2010-08-30 18:46:21 +00:00
|
|
|
__p_ = promise<result_type>();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class... _ArgTypes>
|
2023-11-01 22:56:43 +00:00
|
|
|
class _LIBCPP_TEMPLATE_VIS packaged_task<void(_ArgTypes...)> {
|
2010-08-30 18:46:21 +00:00
|
|
|
public:
|
2016-06-01 21:05:53 +00:00
|
|
|
typedef void result_type; // extension
|
2010-08-30 18:46:21 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
__packaged_task_function<result_type(_ArgTypes...)> __f_;
|
|
|
|
promise<result_type> __p_;
|
|
|
|
|
|
|
|
public:
|
|
|
|
// construction and destruction
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI packaged_task() _NOEXCEPT : __p_(nullptr) {}
|
2022-09-05 22:33:34 +00:00
|
|
|
template <class _Fp, class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
|
2011-11-29 18:15:50 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit packaged_task(_Fp&& __f) : __f_(std::forward<_Fp>(__f)) {}
|
2022-09-05 22:33:34 +00:00
|
|
|
template <class _Fp, class _Allocator, class = __enable_if_t<!is_same<__remove_cvref_t<_Fp>, packaged_task>::value> >
|
2017-11-27 20:47:54 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI packaged_task(allocator_arg_t, const _Allocator& __a, _Fp&& __f)
|
2023-03-06 20:43:26 +00:00
|
|
|
: __f_(allocator_arg_t(), __a, std::forward<_Fp>(__f)), __p_(allocator_arg_t(), __a) {}
|
2010-08-30 18:46:21 +00:00
|
|
|
// ~packaged_task() = default;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-30 18:46:21 +00:00
|
|
|
// no copy
|
2012-07-21 19:34:12 +00:00
|
|
|
packaged_task(const packaged_task&) = delete;
|
|
|
|
packaged_task& operator=(const packaged_task&) = delete;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2010-08-30 18:46:21 +00:00
|
|
|
// move support
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI packaged_task(packaged_task&& __other) _NOEXCEPT
|
2011-06-30 21:18:19 +00:00
|
|
|
: __f_(std::move(__other.__f_)),
|
|
|
|
__p_(std::move(__other.__p_)) {}
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI packaged_task& operator=(packaged_task&& __other) _NOEXCEPT {
|
2011-06-30 21:18:19 +00:00
|
|
|
__f_ = std::move(__other.__f_);
|
|
|
|
__p_ = std::move(__other.__p_);
|
2010-08-27 20:10:19 +00:00
|
|
|
return *this;
|
2023-12-18 19:01:33 +00:00
|
|
|
}
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void swap(packaged_task& __other) _NOEXCEPT {
|
2010-08-30 18:46:21 +00:00
|
|
|
__f_.swap(__other.__f_);
|
|
|
|
__p_.swap(__other.__p_);
|
2023-12-18 19:01:33 +00:00
|
|
|
}
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __p_.__state_ != nullptr; }
|
2010-08-30 18:46:21 +00:00
|
|
|
|
|
|
|
// result retrieval
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future<result_type> get_future() { return __p_.get_future(); }
|
|
|
|
|
|
|
|
// execution
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void operator()(_ArgTypes... __args);
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void make_ready_at_thread_exit(_ArgTypes... __args);
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void reset();
|
2010-08-30 18:46:21 +00:00
|
|
|
};
|
|
|
|
|
2022-10-01 19:25:13 +00:00
|
|
|
#if _LIBCPP_STD_VER >= 17
|
|
|
|
|
|
|
|
template <class _Rp, class... _Args>
|
|
|
|
packaged_task(_Rp (*)(_Args...)) -> packaged_task<_Rp(_Args...)>;
|
|
|
|
|
|
|
|
template <class _Fp, class _Stripped = typename __strip_signature<decltype(&_Fp::operator())>::type>
|
|
|
|
packaged_task(_Fp) -> packaged_task<_Stripped>;
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2010-08-30 18:46:21 +00:00
|
|
|
template <class... _ArgTypes>
|
|
|
|
void packaged_task<void(_ArgTypes...)>::operator()(_ArgTypes... __args) {
|
|
|
|
if (__p_.__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2010-08-30 18:46:21 +00:00
|
|
|
if (__p_.__state_->__has_value())
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::promise_already_satisfied);
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
try {
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2011-06-30 21:18:19 +00:00
|
|
|
__f_(std::forward<_ArgTypes>(__args)...);
|
2010-08-30 18:46:21 +00:00
|
|
|
__p_.set_value();
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
} catch (...) {
|
|
|
|
__p_.set_exception(current_exception());
|
|
|
|
}
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class... _ArgTypes>
|
|
|
|
void packaged_task<void(_ArgTypes...)>::make_ready_at_thread_exit(_ArgTypes... __args) {
|
|
|
|
if (__p_.__state_ == nullptr)
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2010-08-30 18:46:21 +00:00
|
|
|
if (__p_.__state_->__has_value())
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::promise_already_satisfied);
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
try {
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2011-06-30 21:18:19 +00:00
|
|
|
__f_(std::forward<_ArgTypes>(__args)...);
|
2010-08-30 18:46:21 +00:00
|
|
|
__p_.set_value_at_thread_exit();
|
2023-02-02 10:47:01 +00:00
|
|
|
#ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
} catch (...) {
|
|
|
|
__p_.set_exception_at_thread_exit(current_exception());
|
|
|
|
}
|
2023-02-02 10:47:01 +00:00
|
|
|
#endif // _LIBCPP_HAS_NO_EXCEPTIONS
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <class... _ArgTypes>
|
|
|
|
void packaged_task<void(_ArgTypes...)>::reset() {
|
2010-11-30 20:23:32 +00:00
|
|
|
if (!valid())
|
2015-10-02 21:25:15 +00:00
|
|
|
__throw_future_error(future_errc::no_state);
|
2010-08-30 18:46:21 +00:00
|
|
|
__p_ = promise<result_type>();
|
|
|
|
}
|
|
|
|
|
2021-03-24 22:31:58 +00:00
|
|
|
template <class _Rp, class... _ArgTypes>
|
2010-08-30 18:46:21 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI void
|
2021-03-24 22:31:58 +00:00
|
|
|
swap(packaged_task<_Rp(_ArgTypes...)>& __x, packaged_task<_Rp(_ArgTypes...)>& __y) _NOEXCEPT {
|
2010-08-30 18:46:21 +00:00
|
|
|
__x.swap(__y);
|
|
|
|
}
|
|
|
|
|
2017-11-27 20:47:54 +00:00
|
|
|
template <class _Callable, class _Alloc>
|
|
|
|
struct _LIBCPP_TEMPLATE_VIS uses_allocator<packaged_task<_Callable>, _Alloc> : public true_type {};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Fp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_deferred_assoc_state(_Fp&& __f) {
|
|
|
|
unique_ptr<__deferred_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
|
|
|
|
new __deferred_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
|
|
|
|
return future<_Rp>(__h.get());
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp, class _Fp>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future<_Rp> __make_async_assoc_state(_Fp&& __f) {
|
|
|
|
unique_ptr<__async_assoc_state<_Rp, _Fp>, __release_shared_count> __h(
|
|
|
|
new __async_assoc_state<_Rp, _Fp>(std::forward<_Fp>(__f)));
|
|
|
|
std::thread(&__async_assoc_state<_Rp, _Fp>::__execute, __h.get()).detach();
|
|
|
|
return future<_Rp>(__h.get());
|
2011-05-19 15:05:04 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 14:00:53 +00:00
|
|
|
#ifndef _LIBCPP_CXX03_LANG
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp, class... _Args>
|
2019-12-10 23:00:42 +00:00
|
|
|
class _LIBCPP_HIDDEN __async_func {
|
2011-11-29 18:15:50 +00:00
|
|
|
tuple<_Fp, _Args...> __f_;
|
2011-05-19 15:05:04 +00:00
|
|
|
|
|
|
|
public:
|
2011-11-29 18:15:50 +00:00
|
|
|
typedef typename __invoke_of<_Fp, _Args...>::type _Rp;
|
2011-05-19 15:05:04 +00:00
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI explicit __async_func(_Fp&& __f, _Args&&... __args)
|
2011-06-30 21:18:19 +00:00
|
|
|
: __f_(std::move(__f), std::move(__args)...) {}
|
2011-05-19 15:05:04 +00:00
|
|
|
|
2011-06-30 21:18:19 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI __async_func(__async_func&& __f) : __f_(std::move(__f.__f_)) {}
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _Rp operator()() {
|
2011-05-19 15:05:04 +00:00
|
|
|
typedef typename __make_tuple_indices<1 + sizeof...(_Args), 1>::type _Index;
|
|
|
|
return __execute(_Index());
|
2023-12-18 19:01:33 +00:00
|
|
|
}
|
2011-05-19 15:05:04 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
template <size_t... _Indices>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI _Rp __execute(__tuple_indices<_Indices...>) {
|
2020-12-16 00:32:29 +00:00
|
|
|
return std::__invoke(std::move(std::get<0>(__f_)), std::move(std::get<_Indices>(__f_))...);
|
2011-05-19 15:05:04 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2013-11-03 22:06:53 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI bool __does_policy_contain(launch __policy, launch __value) {
|
2013-11-03 15:43:35 +00:00
|
|
|
return (int(__policy) & int(__value)) != 0;
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp, class... _Args>
|
2022-08-13 11:23:16 +00:00
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 _LIBCPP_HIDE_FROM_ABI
|
2023-03-27 16:22:23 +00:00
|
|
|
future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
|
2011-11-29 18:15:50 +00:00
|
|
|
async(launch __policy, _Fp&& __f, _Args&&... __args) {
|
2023-03-27 16:22:23 +00:00
|
|
|
typedef __async_func<__decay_t<_Fp>, __decay_t<_Args>...> _BF;
|
2011-11-29 18:15:50 +00:00
|
|
|
typedef typename _BF::_Rp _Rp;
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2023-02-02 10:47:01 +00:00
|
|
|
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2013-11-03 15:43:35 +00:00
|
|
|
try {
|
|
|
|
# endif
|
|
|
|
if (__does_policy_contain(__policy, launch::async))
|
2021-12-14 00:21:38 +00:00
|
|
|
return std::__make_async_assoc_state<_Rp>(
|
|
|
|
_BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
|
2023-02-02 10:47:01 +00:00
|
|
|
# ifndef _LIBCPP_HAS_NO_EXCEPTIONS
|
2013-11-03 15:43:35 +00:00
|
|
|
} catch (...) {
|
|
|
|
if (__policy == launch::async)
|
|
|
|
throw;
|
2023-12-18 19:01:33 +00:00
|
|
|
}
|
2013-11-03 15:43:35 +00:00
|
|
|
# endif
|
2023-12-18 19:01:33 +00:00
|
|
|
|
2013-11-03 15:43:35 +00:00
|
|
|
if (__does_policy_contain(__policy, launch::deferred))
|
2021-12-14 00:21:38 +00:00
|
|
|
return std::__make_deferred_assoc_state<_Rp>(
|
|
|
|
_BF(_LIBCPP_AUTO_CAST(std::forward<_Fp>(__f)), _LIBCPP_AUTO_CAST(std::forward<_Args>(__args))...));
|
2013-11-03 15:43:35 +00:00
|
|
|
return future<_Rp>{};
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Fp, class... _Args>
|
2017-11-23 01:25:03 +00:00
|
|
|
_LIBCPP_NODISCARD_AFTER_CXX17 inline _LIBCPP_HIDE_FROM_ABI
|
2023-03-27 16:22:23 +00:00
|
|
|
future<typename __invoke_of<__decay_t<_Fp>, __decay_t<_Args>...>::type>
|
2011-11-29 18:15:50 +00:00
|
|
|
async(_Fp&& __f, _Args&&... __args) {
|
|
|
|
return std::async(launch::any, std::forward<_Fp>(__f), std::forward<_Args>(__args)...);
|
2010-08-30 18:46:21 +00:00
|
|
|
}
|
|
|
|
|
2020-07-30 14:00:53 +00:00
|
|
|
#endif // C++03
|
2010-08-30 18:46:21 +00:00
|
|
|
|
2010-09-03 21:46:37 +00:00
|
|
|
// shared_future
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2017-01-04 23:56:00 +00:00
|
|
|
class _LIBCPP_TEMPLATE_VIS shared_future {
|
2011-11-29 18:15:50 +00:00
|
|
|
__assoc_state<_Rp>* __state_;
|
2010-09-03 18:39:25 +00:00
|
|
|
|
|
|
|
public:
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
|
2016-11-14 19:58:05 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
|
2010-09-03 18:39:25 +00:00
|
|
|
if (__state_)
|
|
|
|
__state_->__add_shared();
|
|
|
|
}
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
|
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
|
2010-09-03 18:39:25 +00:00
|
|
|
__rhs.__state_ = nullptr;
|
|
|
|
}
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI ~shared_future();
|
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs) _NOEXCEPT;
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
|
[libc++] Consistently replace `std::` qualification with `_VSTD::` or nothing. NFCI.
I used a lot of `git grep` to find places where `std::` was being used
outside of comments and assert-messages. There were three outcomes:
- Qualified function calls, e.g. `std::move` becomes `_VSTD::move`.
This is the most common case.
- Typenames that don't need qualification, e.g. `std::allocator` becomes `allocator`.
Leaving these as `_VSTD::allocator` would also be fine, but I decided
that removing the qualification is more consistent with existing practice.
- Names that specifically need un-versioned `std::` qualification,
or that I wasn't sure about. For example, I didn't touch any code in
<atomic>, <math.h>, <new>, or any ext/ or experimental/ headers;
and I didn't touch any instances of `std::type_info`.
In some deduction guides, we were accidentally using `class Alloc = typename std::allocator<T>`,
despite `std::allocator<T>`'s type-ness not being template-dependent.
Because `std::allocator` is a qualified name, this did parse as we intended;
but what we meant was simply `class Alloc = allocator<T>`.
Differential Revision: https://reviews.llvm.org/D92250
2020-11-27 16:02:06 +00:00
|
|
|
shared_future(std::move(__rhs)).swap(*this);
|
2010-09-03 18:39:25 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieving the value
|
2011-11-29 18:15:50 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI const _Rp& get() const { return __state_->copy(); }
|
2010-09-03 18:39:25 +00:00
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
|
2010-09-03 18:39:25 +00:00
|
|
|
|
|
|
|
// functions to check state
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
|
2010-09-03 18:39:25 +00:00
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
|
|
|
|
template <class _Rep, class _Period>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
|
|
|
|
return __state_->wait_for(__rel_time);
|
|
|
|
}
|
|
|
|
template <class _Clock, class _Duration>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
|
|
|
|
return __state_->wait_until(__abs_time);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
shared_future<_Rp>::~shared_future() {
|
2010-09-03 18:39:25 +00:00
|
|
|
if (__state_)
|
|
|
|
__state_->__release_shared();
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2016-11-14 19:58:05 +00:00
|
|
|
shared_future<_Rp>& shared_future<_Rp>::operator=(const shared_future& __rhs) _NOEXCEPT {
|
2010-09-03 18:39:25 +00:00
|
|
|
if (__rhs.__state_)
|
|
|
|
__rhs.__state_->__add_shared();
|
|
|
|
if (__state_)
|
|
|
|
__state_->__release_shared();
|
|
|
|
__state_ = __rhs.__state_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2017-01-04 23:56:00 +00:00
|
|
|
class _LIBCPP_TEMPLATE_VIS shared_future<_Rp&> {
|
2011-11-29 18:15:50 +00:00
|
|
|
__assoc_state<_Rp&>* __state_;
|
2010-09-03 18:39:25 +00:00
|
|
|
|
|
|
|
public:
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
|
2010-09-03 18:39:25 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
|
|
|
|
if (__state_)
|
|
|
|
__state_->__add_shared();
|
|
|
|
}
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future(future<_Rp&>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
|
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
|
2010-09-03 18:39:25 +00:00
|
|
|
__rhs.__state_ = nullptr;
|
|
|
|
}
|
2023-01-23 09:27:14 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI ~shared_future();
|
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future& operator=(const shared_future& __rhs);
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
|
[libc++] Consistently replace `std::` qualification with `_VSTD::` or nothing. NFCI.
I used a lot of `git grep` to find places where `std::` was being used
outside of comments and assert-messages. There were three outcomes:
- Qualified function calls, e.g. `std::move` becomes `_VSTD::move`.
This is the most common case.
- Typenames that don't need qualification, e.g. `std::allocator` becomes `allocator`.
Leaving these as `_VSTD::allocator` would also be fine, but I decided
that removing the qualification is more consistent with existing practice.
- Names that specifically need un-versioned `std::` qualification,
or that I wasn't sure about. For example, I didn't touch any code in
<atomic>, <math.h>, <new>, or any ext/ or experimental/ headers;
and I didn't touch any instances of `std::type_info`.
In some deduction guides, we were accidentally using `class Alloc = typename std::allocator<T>`,
despite `std::allocator<T>`'s type-ness not being template-dependent.
Because `std::allocator` is a qualified name, this did parse as we intended;
but what we meant was simply `class Alloc = allocator<T>`.
Differential Revision: https://reviews.llvm.org/D92250
2020-11-27 16:02:06 +00:00
|
|
|
shared_future(std::move(__rhs)).swap(*this);
|
2010-09-03 18:39:25 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieving the value
|
2011-11-29 18:15:50 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI _Rp& get() const { return __state_->copy(); }
|
2010-09-03 18:39:25 +00:00
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
|
2010-09-03 18:39:25 +00:00
|
|
|
|
|
|
|
// functions to check state
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
|
2010-09-03 18:39:25 +00:00
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
|
|
|
|
template <class _Rep, class _Period>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
|
|
|
|
return __state_->wait_for(__rel_time);
|
|
|
|
}
|
|
|
|
template <class _Clock, class _Duration>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
|
|
|
|
return __state_->wait_until(__abs_time);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
shared_future<_Rp&>::~shared_future() {
|
2010-09-03 18:39:25 +00:00
|
|
|
if (__state_)
|
|
|
|
__state_->__release_shared();
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
|
|
|
shared_future<_Rp&>& shared_future<_Rp&>::operator=(const shared_future& __rhs) {
|
2010-09-03 18:39:25 +00:00
|
|
|
if (__rhs.__state_)
|
|
|
|
__rhs.__state_->__add_shared();
|
|
|
|
if (__state_)
|
|
|
|
__state_->__release_shared();
|
|
|
|
__state_ = __rhs.__state_;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <>
|
2023-11-01 22:56:43 +00:00
|
|
|
class _LIBCPP_EXPORTED_FROM_ABI shared_future<void> {
|
2010-09-03 18:39:25 +00:00
|
|
|
__assoc_sub_state* __state_;
|
|
|
|
|
|
|
|
public:
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future() _NOEXCEPT : __state_(nullptr) {}
|
2010-09-03 18:39:25 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future(const shared_future& __rhs) : __state_(__rhs.__state_) {
|
|
|
|
if (__state_)
|
|
|
|
__state_->__add_shared();
|
|
|
|
}
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future(future<void>&& __f) _NOEXCEPT : __state_(__f.__state_) { __f.__state_ = nullptr; }
|
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future(shared_future&& __rhs) _NOEXCEPT : __state_(__rhs.__state_) {
|
2010-09-03 18:39:25 +00:00
|
|
|
__rhs.__state_ = nullptr;
|
|
|
|
}
|
|
|
|
~shared_future();
|
|
|
|
shared_future& operator=(const shared_future& __rhs);
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI shared_future& operator=(shared_future&& __rhs) _NOEXCEPT {
|
[libc++] Consistently replace `std::` qualification with `_VSTD::` or nothing. NFCI.
I used a lot of `git grep` to find places where `std::` was being used
outside of comments and assert-messages. There were three outcomes:
- Qualified function calls, e.g. `std::move` becomes `_VSTD::move`.
This is the most common case.
- Typenames that don't need qualification, e.g. `std::allocator` becomes `allocator`.
Leaving these as `_VSTD::allocator` would also be fine, but I decided
that removing the qualification is more consistent with existing practice.
- Names that specifically need un-versioned `std::` qualification,
or that I wasn't sure about. For example, I didn't touch any code in
<atomic>, <math.h>, <new>, or any ext/ or experimental/ headers;
and I didn't touch any instances of `std::type_info`.
In some deduction guides, we were accidentally using `class Alloc = typename std::allocator<T>`,
despite `std::allocator<T>`'s type-ness not being template-dependent.
Because `std::allocator` is a qualified name, this did parse as we intended;
but what we meant was simply `class Alloc = allocator<T>`.
Differential Revision: https://reviews.llvm.org/D92250
2020-11-27 16:02:06 +00:00
|
|
|
shared_future(std::move(__rhs)).swap(*this);
|
2010-09-03 18:39:25 +00:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
// retrieving the value
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void get() const { __state_->copy(); }
|
|
|
|
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI void swap(shared_future& __rhs) _NOEXCEPT { std::swap(__state_, __rhs.__state_); }
|
2010-09-03 18:39:25 +00:00
|
|
|
|
|
|
|
// functions to check state
|
2012-07-21 17:46:55 +00:00
|
|
|
_LIBCPP_HIDE_FROM_ABI bool valid() const _NOEXCEPT { return __state_ != nullptr; }
|
2010-09-03 18:39:25 +00:00
|
|
|
|
|
|
|
_LIBCPP_HIDE_FROM_ABI void wait() const { __state_->wait(); }
|
|
|
|
template <class _Rep, class _Period>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future_status wait_for(const chrono::duration<_Rep, _Period>& __rel_time) const {
|
|
|
|
return __state_->wait_for(__rel_time);
|
|
|
|
}
|
|
|
|
template <class _Clock, class _Duration>
|
|
|
|
_LIBCPP_HIDE_FROM_ABI future_status wait_until(const chrono::time_point<_Clock, _Duration>& __abs_time) const {
|
|
|
|
return __state_->wait_until(__abs_time);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2012-07-21 17:46:55 +00:00
|
|
|
inline _LIBCPP_HIDE_FROM_ABI void swap(shared_future<_Rp>& __x, shared_future<_Rp>& __y) _NOEXCEPT {
|
2010-09-03 18:39:25 +00:00
|
|
|
__x.swap(__y);
|
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2017-01-24 23:28:25 +00:00
|
|
|
inline shared_future<_Rp> future<_Rp>::share() _NOEXCEPT {
|
2011-11-29 18:15:50 +00:00
|
|
|
return shared_future<_Rp>(std::move(*this));
|
2010-09-03 21:46:37 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 18:15:50 +00:00
|
|
|
template <class _Rp>
|
2017-01-24 23:28:25 +00:00
|
|
|
inline shared_future<_Rp&> future<_Rp&>::share() _NOEXCEPT {
|
2011-11-29 18:15:50 +00:00
|
|
|
return shared_future<_Rp&>(std::move(*this));
|
2010-09-03 21:46:37 +00:00
|
|
|
}
|
|
|
|
|
2011-06-30 21:18:19 +00:00
|
|
|
inline shared_future<void> future<void>::share() _NOEXCEPT { return shared_future<void>(std::move(*this)); }
|
2010-09-03 21:46:37 +00:00
|
|
|
|
2010-05-11 19:42:16 +00:00
|
|
|
_LIBCPP_END_NAMESPACE_STD
|
|
|
|
|
[🍒][libc++] Fix missing and incorrect push/pop macros (#79204) (#79497)
We recently noticed that the unwrap_iter.h file was pushing macros, but
it was pushing them again instead of popping them at the end of the
file. This led to libc++ basically swallowing any custom definition of
these macros in user code:
#define min HELLO
#include <algorithm>
// min is not HELLO anymore, it's not defined
While investigating this issue, I noticed that our push/pop pragmas were
actually entirely wrong too. Indeed, instead of pushing macros like
`move`, we'd push `move(int, int)` in the pragma, which is not a valid
macro name. As a result, we would not actually push macros like `move`
-- instead we'd simply undefine them. This led to the following code not
working:
#define move HELLO
#include <algorithm>
// move is not HELLO anymore
Fixing the pragma push/pop incantations led to a cascade of issues
because we use identifiers like `move` in a large number of places, and
all of these headers would now need to do the push/pop dance.
This patch fixes all these issues. First, it adds a check that we don't
swallow important names like min, max, move or refresh as explained
above. This is done by augmenting the existing
system_reserved_names.gen.py test to also check that the macros are what
we expect after including each header.
Second, it fixes the push/pop pragmas to work properly and adds missing
pragmas to all the files I could detect a failure in via the newly added
test.
rdar://121365472
(cherry picked from commit 7b4622514d232ce5f7110dd8b20d90e81127c467)
2024-02-02 01:51:34 +00:00
|
|
|
_LIBCPP_POP_MACROS
|
|
|
|
|
2022-09-02 15:53:28 +00:00
|
|
|
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 17
|
2022-09-19 15:37:35 +00:00
|
|
|
# include <chrono>
|
2022-09-02 15:53:28 +00:00
|
|
|
#endif
|
|
|
|
|
2023-01-31 18:23:30 +00:00
|
|
|
#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
|
|
|
|
# include <atomic>
|
2023-01-08 15:47:53 +00:00
|
|
|
# include <cstdlib>
|
2023-03-01 19:49:22 +00:00
|
|
|
# include <exception>
|
2023-10-29 17:31:37 +00:00
|
|
|
# include <iosfwd>
|
2023-04-08 15:29:31 +00:00
|
|
|
# include <system_error>
|
2023-01-31 18:23:30 +00:00
|
|
|
#endif
|
|
|
|
|
2021-04-20 16:03:32 +00:00
|
|
|
#endif // _LIBCPP_FUTURE
|