source: mainline/uspace/lib/cpp/include/__bits/thread/packaged_task.hpp@ 09170ab8

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 09170ab8 was 4d68584, checked in by Jaroslav Jindrak <dzejrou@…>, 6 years ago

cpp: add stub exception support

  • Property mode set to 100644
File size: 6.3 KB
Line 
1/*
2 * Copyright (c) 2019 Jaroslav Jindrak
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef LIBCPP_BITS_THREAD_PACKAGED_TASK
30#define LIBCPP_BITS_THREAD_PACKAGED_TASK
31
32#include <__bits/exception.hpp>
33#include <__bits/functional/function.hpp>
34#include <__bits/thread/future.hpp>
35#include <__bits/thread/future_common.hpp>
36#include <__bits/thread/shared_state.hpp>
37#include <type_traits>
38#include <utility>
39
40namespace std
41{
42 /**
43 * 30.6.9, class template packaged_task:
44 */
45
46 template<class>
47 class packaged_task; // undefined
48
49 template<class R, class... Args>
50 class packaged_task<R(Args...)>
51 {
52 public:
53 packaged_task() noexcept
54 : func_{}, state_{}
55 { /* DUMMY BODY */ }
56
57 template<
58 class F, enable_if_t<
59 !is_same_v<
60 decay_t<F>, packaged_task<R(Args...)>
61 >, int
62 > = 0
63 >
64 explicit packaged_task(F&& f)
65 : func_{forward<F>(f)}, state_{new aux::shared_state<R>{}}
66 { /* DUMMY BODY */ }
67
68 template<
69 class F, class Allocator, enable_if_t<
70 is_same_v<
71 decay_t<F>, packaged_task<R(Args...)>
72 >, int
73 > = 0
74 >
75 explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f)
76 : packaged_task{forward<F>(f)}
77 {
78 // TODO: use the allocator
79 }
80
81 ~packaged_task()
82 {
83 if (state_)
84 {
85 if (!state_->is_set())
86 {
87 state_->set_exception(make_exception_ptr(
88 future_error{make_error_code(future_errc::broken_promise)}
89 ));
90 state_->mark_set(true);
91 }
92
93 if (state_->decrement())
94 {
95 state_->destroy();
96 delete state_;
97 state_ = nullptr;
98 }
99 }
100 }
101
102 packaged_task(const packaged_task&) = delete;
103 packaged_task& operator=(const packaged_task&) = delete;
104
105 packaged_task(packaged_task&& rhs)
106 : func_{move(rhs.func_)}, state_{move(rhs.state_)}
107 { /* DUMMY BODY */ }
108
109 packaged_task& operator=(packaged_task&& rhs)
110 {
111 if (state_)
112 {
113 if (state_->decrement())
114 {
115 state_->destroy();
116 delete state_;
117 state_ = nullptr;
118 }
119 }
120
121 func_ = move(rhs.func_);
122 state_ = move(rhs.state_);
123
124 return *this;
125 }
126
127 void swap(packaged_task& other) noexcept
128 {
129 std::swap(func_, other.func_);
130 std::swap(state_, other.state_);
131 }
132
133 bool valid() const noexcept
134 {
135 return state_ != nullptr;
136 }
137
138 future<R> get_future()
139 {
140 if (!state_)
141 throw future_error{make_error_code(future_errc::no_state)};
142
143 state_->increment();
144
145 return future<R>{state_};
146 }
147
148 /**
149 * Note: This is how the signature is in the standard,
150 * should be investigated and verified.
151 */
152 void operator()(Args... args)
153 {
154 if (!state_)
155 throw future_error{make_error_code(future_errc::no_state)};
156 if (state_->is_set())
157 {
158 throw future_error{
159 make_error_code(future_errc::promise_already_satisfied)
160 };
161 }
162
163 try
164 {
165 state_->set_value(invoke(func_, args...));
166 }
167 catch(const exception& __exception)
168 {
169 state_->set_exception(make_exception_ptr(__exception));
170 }
171 }
172
173 void make_ready_at_thread_exit(Args...)
174 {
175 // TODO: implement
176 }
177
178 void reset()
179 {
180 if (!state_)
181 throw future_error{make_error_code(future_errc::no_state)};
182
183 *this = packaged_task{move(func_)};
184 }
185
186 private:
187 function<R(Args...)> func_;
188
189 aux::shared_state<R>* state_;
190 };
191
192 template<class R, class... Args>
193 void swap(packaged_task<R(Args...)>& lhs, packaged_task<R(Args...)>& rhs) noexcept
194 {
195 lhs.swap(rhs);
196 };
197
198 template<class R, class Alloc>
199 struct uses_allocator<packaged_task<R>, Alloc>: true_type
200 { /* DUMMY BODY */ };
201}
202
203#endif
Note: See TracBrowser for help on using the repository browser.