|
| 1 | +/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | + you may not use this file except in compliance with the License. |
| 5 | + You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | + Unless required by applicable law or agreed to in writing, software |
| 10 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | + See the License for the specific language governing permissions and |
| 13 | + limitations under the License. */ |
| 14 | + |
| 15 | +#pragma once |
| 16 | +#include <memory> |
| 17 | +#include <thread> |
| 18 | + |
| 19 | +namespace paddle { |
| 20 | +namespace framework { |
| 21 | +namespace details { |
| 22 | + |
| 23 | +// Change it to thread safe flags if needed. |
| 24 | +class ThreadUnsafeOwnershipFlags { |
| 25 | + public: |
| 26 | + ThreadUnsafeOwnershipFlags(bool flag) : flag_(flag) {} |
| 27 | + |
| 28 | + ThreadUnsafeOwnershipFlags(const ThreadUnsafeOwnershipFlags& other) = delete; |
| 29 | + ThreadUnsafeOwnershipFlags& operator=( |
| 30 | + const ThreadUnsafeOwnershipFlags& other) = delete; |
| 31 | + ThreadUnsafeOwnershipFlags(ThreadUnsafeOwnershipFlags&& other) = default; |
| 32 | + |
| 33 | + void SetOwnership(bool flag) { flag_ = flag; } |
| 34 | + |
| 35 | + // Invoke the callback if it is not owned. |
| 36 | + template <typename Callback> |
| 37 | + void AcquireOwnershipOnce(Callback acquire) { |
| 38 | + if (!flag_) { |
| 39 | + acquire(); |
| 40 | + flag_ = true; |
| 41 | + } |
| 42 | + } |
| 43 | + |
| 44 | + private: |
| 45 | + bool flag_; |
| 46 | +}; |
| 47 | + |
| 48 | +// Copy-On-Write pointer. |
| 49 | +// It will hold a T* pointer, and only copy once when `MutableData` is invoked. |
| 50 | +// |
| 51 | +// The template parameter OwnershipFlags should have: |
| 52 | +// * a constructor takes a bool. True if own. |
| 53 | +// * SetOwnership(bool flag). |
| 54 | +// * AcquireOwnershipOnce(Callback). It will invoke the callback if it is not |
| 55 | +// owned. |
| 56 | +// |
| 57 | +// https://en.wikipedia.org/wiki/Copy-on-write |
| 58 | +template <typename T, typename OwnershipFlags = ThreadUnsafeOwnershipFlags> |
| 59 | +class COWPtr { |
| 60 | + public: |
| 61 | + // Ctor from raw pointer. |
| 62 | + explicit COWPtr(T* ptr) : payload_(ptr), ownership_{true} {} |
| 63 | + |
| 64 | + // Move methods. Steal ownership from origin |
| 65 | + COWPtr(COWPtr&& other) |
| 66 | + : payload_(other.payload_), ownership_{std::move(other.ownership_)} {} |
| 67 | + COWPtr& operator=(COWPtr&& origin) = default; |
| 68 | + |
| 69 | + // Copy methods. Not own payload |
| 70 | + COWPtr(const COWPtr& other) : payload_(other.payload_), ownership_{false} {} |
| 71 | + COWPtr& operator=(const COWPtr& other) { |
| 72 | + payload_ = other.payload_; |
| 73 | + ownership_.SetOwnership(false); |
| 74 | + return *this; |
| 75 | + } |
| 76 | + |
| 77 | + // Access read only data. |
| 78 | + const T& Data() const { return *payload_; } |
| 79 | + |
| 80 | + // Access mutable data. If the data is not owned, the data will be copied |
| 81 | + // before. |
| 82 | + T* MutableData() { |
| 83 | + ownership_.AcquireOwnershipOnce( |
| 84 | + [this] { payload_.reset(new T(*payload_)); }); |
| 85 | + return payload_.get(); |
| 86 | + } |
| 87 | + |
| 88 | + private: |
| 89 | + // Actual data pointer. |
| 90 | + std::shared_ptr<T> payload_; |
| 91 | + |
| 92 | + // Ownership flag. |
| 93 | + OwnershipFlags ownership_; |
| 94 | +}; |
| 95 | + |
| 96 | +} // namespace details |
| 97 | +} // namespace framework |
| 98 | +} // namespace paddle |
0 commit comments