source: mainline/uspace/lib/cpp/include/__bits/thread/packaged_task.hpp@ 5ea9dd2

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

cpp: add allocator support

  • Property mode set to 100644
File size: 6.5 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/memory/allocator_traits.hpp>
35#include <__bits/thread/future.hpp>
36#include <__bits/thread/future_common.hpp>
37#include <__bits/thread/shared_state.hpp>
38#include <type_traits>
39#include <utility>
40
41namespace std
42{
43 /**
44 * 30.6.9, class template packaged_task:
45 */
46
47 template<class>
48 class packaged_task; // undefined
49
50 template<class R, class... Args>
51 class packaged_task<R(Args...)>
52 {
53 public:
54 packaged_task() noexcept
55 : func_{}, state_{}
56 { /* DUMMY BODY */ }
57
58 template<
59 class F, enable_if_t<
60 !is_same_v<
61 decay_t<F>, packaged_task<R(Args...)>
62 >, int
63 > = 0
64 >
65 explicit packaged_task(F&& f)
66 : func_{forward<F>(f)}, state_{new aux::shared_state<R>{}}
67 { /* DUMMY BODY */ }
68
69 template<
70 class F, class Allocator, enable_if_t<
71 is_same_v<
72 decay_t<F>, packaged_task<R(Args...)>
73 >, int
74 > = 0
75 >
76 explicit packaged_task(allocator_arg_t, const Allocator& a, F&& f)
77 : func_{forward<F>(f)}, state_{}
78 {
79 auto rebound = allocator_traits<Allocator>::rebind<
80 aux::shared_state<R>
81 >{a};
82
83 state_ = rebound.allocate(1);
84 rebound.construct(state_);
85 }
86
87 ~packaged_task()
88 {
89 if (state_)
90 {
91 if (!state_->is_set())
92 {
93 state_->set_exception(make_exception_ptr(
94 future_error{make_error_code(future_errc::broken_promise)}
95 ));
96 state_->mark_set(true);
97 }
98
99 if (state_->decrement())
100 {
101 state_->destroy();
102 delete state_;
103 state_ = nullptr;
104 }
105 }
106 }
107
108 packaged_task(const packaged_task&) = delete;
109 packaged_task& operator=(const packaged_task&) = delete;
110
111 packaged_task(packaged_task&& rhs)
112 : func_{move(rhs.func_)}, state_{move(rhs.state_)}
113 { /* DUMMY BODY */ }
114
115 packaged_task& operator=(packaged_task&& rhs)
116 {
117 if (state_)
118 {
119 if (state_->decrement())
120 {
121 state_->destroy();
122 delete state_;
123 state_ = nullptr;
124 }
125 }
126
127 func_ = move(rhs.func_);
128 state_ = move(rhs.state_);
129
130 return *this;
131 }
132
133 void swap(packaged_task& other) noexcept
134 {
135 std::swap(func_, other.func_);
136 std::swap(state_, other.state_);
137 }
138
139 bool valid() const noexcept
140 {
141 return state_ != nullptr;
142 }
143
144 future<R> get_future()
145 {
146 if (!state_)
147 throw future_error{make_error_code(future_errc::no_state)};
148
149 state_->increment();
150
151 return future<R>{state_};
152 }
153
154 /**
155 * Note: This is how the signature is in the standard,
156 * should be investigated and verified.
157 */
158 void operator()(Args... args)
159 {
160 if (!state_)
161 throw future_error{make_error_code(future_errc::no_state)};
162 if (state_->is_set())
163 {
164 throw future_error{
165 make_error_code(future_errc::promise_already_satisfied)
166 };
167 }
168
169 try
170 {
171 state_->set_value(invoke(func_, args...));
172 }
173 catch(const exception& __exception)
174 {
175 state_->set_exception(make_exception_ptr(__exception));
176 }
177 }
178
179 void make_ready_at_thread_exit(Args...)
180 {
181 // TODO: implement
182 }
183
184 void reset()
185 {
186 if (!state_)
187 throw future_error{make_error_code(future_errc::no_state)};
188
189 *this = packaged_task{move(func_)};
190 }
191
192 private:
193 function<R(Args...)> func_;
194
195 aux::shared_state<R>* state_;
196 };
197
198 template<class R, class... Args>
199 void swap(packaged_task<R(Args...)>& lhs, packaged_task<R(Args...)>& rhs) noexcept
200 {
201 lhs.swap(rhs);
202 };
203
204 template<class R, class Alloc>
205 struct uses_allocator<packaged_task<R>, Alloc>: true_type
206 { /* DUMMY BODY */ };
207}
208
209#endif
Note: See TracBrowser for help on using the repository browser.