| 159 | |
| 160 | template <typename T> |
| 161 | Future<Owned<T>> Shared<T>::own() |
| 162 | { |
| 163 | // If two threads simultaneously access this object and at least one |
| 164 | // of them is a write, the behavior is undefined. This is similar to |
| 165 | // boost::shared_ptr. For more details, please refer to the boost |
| 166 | // shared_ptr document (section "Thread Safety"). |
| 167 | if (data == nullptr) { |
| 168 | return Owned<T>(nullptr); |
| 169 | } |
| 170 | |
| 171 | bool false_value = false; |
| 172 | if (!data->owned.compare_exchange_strong(false_value, true)) { |
| 173 | return Failure("Ownership has already been transferred"); |
| 174 | } |
| 175 | |
| 176 | Future<Owned<T>> future = data->promise.future(); |
| 177 | data.reset(); |
| 178 | return future; |
| 179 | } |
| 180 | |
| 181 | |
| 182 | template <typename T> |