// Copyright (c) Facebook, Inc. and its affiliates. // // 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. #pragma once #include #include namespace yarpl { template struct AtomicReference { folly::Synchronized, std::mutex> ref; AtomicReference() = default; AtomicReference(std::shared_ptr&& r) { *(ref.lock()) = std::move(r); } }; template std::shared_ptr atomic_load(AtomicReference* ar) { return *(ar->ref.lock()); } template std::shared_ptr atomic_exchange( AtomicReference* ar, std::shared_ptr r) { auto refptr = ar->ref.lock(); auto old = std::move(*refptr); *refptr = std::move(r); return old; } template std::shared_ptr atomic_exchange(AtomicReference* ar, std::nullptr_t) { return atomic_exchange(ar, std::shared_ptr()); } template void atomic_store(AtomicReference* ar, std::shared_ptr r) { *ar->ref.lock() = std::move(r); } class enable_get_ref : public std::enable_shared_from_this { private: virtual void dummy_internal_get_ref() {} protected: // materialize a reference to 'this', but a type even further derived from // Derived, because C++ doesn't have covariant return types on methods template std::shared_ptr ref_from_this(As* ptr) { // at runtime, ensure that the most derived class can indeed be // converted into an 'as' (void)ptr; // silence 'unused parameter' errors in Release builds return std::static_pointer_cast(this->shared_from_this()); } template std::shared_ptr ref_from_this(As const* ptr) const { // at runtime, ensure that the most derived class can indeed be // converted into an 'as' (void)ptr; // silence 'unused parameter' errors in Release builds return std::static_pointer_cast(this->shared_from_this()); } public: virtual ~enable_get_ref() = default; }; } /* namespace yarpl */