`thrust::future<T>`:
* Add unit tests.
* Add `future_errc` and friends.
* Replace `assert`s in the CUDA backend's future with exceptions.
* Add missing includes to `<thrust/future.h>`.
Bug
Bug
ADD
testing/future.cu
+#include <thrust/detail/config.h>
+
+#if THRUST_CPP_DIALECT >= 2011
+
+#include <unittest/unittest.h>
+
+#include <thrust/future.h>
+
+template <typename T>
+void test_future_default_construction()
+{
+ thrust::future<T> f0;
+ thrust::future<T, decltype(thrust::device)> f1;
+ thrust::future<T, decltype(thrust::cuda::par)> f2;
+ thrust::future<T, decltype(thrust::device), T*> f3;
+ thrust::future<T, decltype(thrust::cuda::par), T*> f4;
+
+ ASSERT_EQUAL(false, f0.valid());
+ ASSERT_EQUAL(false, f1.valid());
+ ASSERT_EQUAL(false, f2.valid());
+ ASSERT_EQUAL(false, f3.valid());
+ ASSERT_EQUAL(false, f4.valid());
+};
+DECLARE_GENERIC_UNITTEST(test_future_default_construction);
+
+#endif // THRUST_CPP_DIALECT >= 2011
+

ADD
thrust/detail/future_error.h
+/*
+ * Copyright 2008-2013 NVIDIA Corporation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/// \file thrust/detail/future_error.h
+/// \brief \c thrust::future error handling types and codes.
+
+#pragma once
+
+#include <thrust/detail/config.h>
+#include <thrust/detail/cpp11_required.h>
+
+#if THRUST_CPP_DIALECT >= 2011
+
+#include <thrust/detail/type_traits.h>
+#include <thrust/system/error_code.h>
+
+THRUST_BEGIN_NS
+
+enum class future_errc
+{
+ unknown_future_error
+, no_state
+, last_future_error
+};
+
+/// \return <tt>error_code(static_cast<int>(e), future_category())</tt>
+inline error_code make_error_code(future_errc e);
+
+/// \return <tt>error_condition(static_cast<int>(e), future_category())</tt>.
+inline error_condition make_error_condition(future_errc e);
+
+struct future_error_category : error_category
+{
+ future_error_category() = default;
+
+ virtual char const* name() const
+ {
+ return "future";
+ }
+
+ virtual std::string message(int ev) const
+ {
+ switch (static_cast<future_errc>(ev))
+ {
+ case future_errc::no_state:
+ {
+ return "no_state: an operation has been performed on a moved-from or "
+ "default constructed future object";
+ }
+ default:
+ {
+ return "unknown_future_error: an unknown error with a future object "
+ "has occurred";
+ }
+ };
+ }
+
+ virtual error_condition default_error_condition(int ev) const
+ {
+ if (future_errc::last_future_error > static_cast<future_errc>(ev))
+ return make_error_condition(static_cast<future_errc>(ev));
+
+ return system_category().default_error_condition(ev);
+ }
+};
+
+/// Obtains a reference to the static error category object for the errors
+/// related to futures and promises. The object is required to override the
+/// virtual function error_category::name() to return a pointer to the string
+/// "future". It is used to identify error codes provided in the exceptions of
+/// type future_error.
+inline error_category const& future_category()
+{
+ static const future_error_category result;
+ return result;
+}
+
+/// Specialization of \p is_error_code_enum for \p future_errc.
+template<> struct is_error_code_enum<future_errc> : true_type {};
+
+/// \return <tt>error_code(static_cast<int>(e), future_category())</tt>
+inline error_code make_error_code(future_errc e)
+{
+ return error_code(static_cast<int>(e), future_category());
+}
+
+/// \return <tt>error_condition(static_cast<int>(e), future_category())</tt>.
+inline error_condition make_error_condition(future_errc e)
+{
+ return error_condition(static_cast<int>(e), future_category());
+}
+
+THRUST_END_NS
+
+#endif // THRUST_CPP_DIALECT >= 2011

MODIFY
thrust/future.h
thrust/future.h
#if THRUST_CPP_DIALECT >= 2011
+#include <thrust/execution_policy.h>
+
+// #include the device system's pointer.h header.
+#define __THRUST_DEVICE_SYSTEM_POINTER_HEADER <__THRUST_DEVICE_SYSTEM_ROOT/pointer.h>
+ #include __THRUST_DEVICE_SYSTEM_POINTER_HEADER
+#undef __THRUST_DEVICE_SYSTEM_POINTER_HEADER
+
+//// #include the host system's pointer.h header.
+//#define __THRUST_HOST_SYSTEM_POINTER_HEADER <__THRUST_HOST_SYSTEM_ROOT/pointer.h>
+// #include __THRUST_HOST_SYSTEM_POINTER_HEADER
+//#undef __THRUST_HOST_SYSTEM_POINTER_HEADER
+
THRUST_BEGIN_NS
// Fallback.
@@ -76,15 +88,15 @@ template <
THRUST_END_NS
-// #include the host system's execution_policy header
-//#define __THRUST_HOST_SYSTEM_EXECUTION_POLICY_HEADER <__THRUST_HOST_SYSTEM_ROOT/future.h>
-//#include __THRUST_HOST_SYSTEM_EXECUTION_POLICY_HEADER
-//#undef __THRUST_HOST_SYSTEM_EXECUTION_POLICY_HEADER
+// #include the device system's future.h header.
+#define __THRUST_DEVICE_SYSTEM_FUTURE_HEADER <__THRUST_DEVICE_SYSTEM_ROOT/future.h>
+ #include __THRUST_DEVICE_SYSTEM_FUTURE_HEADER
+#undef __THRUST_DEVICE_SYSTEM_FUTURE_HEADER
-// #include the device system's execution_policy.h header
-#define __THRUST_DEVICE_SYSTEM_EXECUTION_POLICY_HEADER <__THRUST_DEVICE_SYSTEM_ROOT/future.h>
-#include __THRUST_DEVICE_SYSTEM_EXECUTION_POLICY_HEADER
-#undef __THRUST_DEVICE_SYSTEM_EXECUTION_POLICY_HEADER
+//// #include the host system's future.h header.
+//#define __THRUST_HOST_SYSTEM_FUTURE_HEADER <__THRUST_HOST_SYSTEM_ROOT/future.h>
+// #include __THRUST_HOST_SYSTEM_FUTURE_HEADER
+//#undef __THRUST_HOST_SYSTEM_FUTURE_HEADER
#endif // THRUST_CPP_DIALECT >= 2011

MODIFY
thrust/system/cuda/detail/future.inl
thrust/system/cuda/detail/future.inl
#include <thrust/allocate_unique.h>
#include <thrust/detail/static_assert.h>
#include <thrust/detail/execute_with_dependencies.h>
+#include <thrust/detail/future_error.h>
#include <thrust/system/cuda/memory.h>
#include <thrust/system/cuda/future.h>
#include <thrust/system/cuda/detail/util.h>
@@ -654,7 +655,9 @@ public:
__host__
detail::unique_stream& stream()
{
- assert(true == valid());
+ if (!valid())
+ throw thrust::system_error(future_errc::no_state, future_category());
+
return async_value_->stream();
}
@@ -751,7 +754,9 @@ public:
__host__
detail::unique_stream& stream()
{
- assert(true == valid());
+ if (!valid())
+ throw thrust::system_error(future_errc::no_state, future_category());
+
return async_value_->stream();
}

`thrust::future<T>`:

* Add unit tests.
* Add `future_errc` and friends.
* Replace `assert`s in the CUDA backend's future with exceptions.
* Add missing includes to `<thrust/future.h>`.

Bug 2379510
Bug 2463967