cow_ptr.h 2.9 KB
Newer Older
1
/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
Y
Yang Yu 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16

   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 <memory>
17
#include <thread>  // NOLINT
Y
Yang Yu 已提交
18 19 20 21 22

namespace paddle {
namespace framework {
namespace details {

23 24
// Change it to thread safe flags if needed.
class ThreadUnsafeOwnershipFlags {
Y
Yang Yu 已提交
25
 public:
26
  explicit ThreadUnsafeOwnershipFlags(bool flag) : flag_(flag) {}
Y
Yang Yu 已提交
27

28 29 30 31
  ThreadUnsafeOwnershipFlags(const ThreadUnsafeOwnershipFlags& other) = delete;
  ThreadUnsafeOwnershipFlags& operator=(
      const ThreadUnsafeOwnershipFlags& other) = delete;
  ThreadUnsafeOwnershipFlags(ThreadUnsafeOwnershipFlags&& other) = default;
Y
Yang Yu 已提交
32

33 34 35 36 37 38 39 40
  void SetOwnership(bool flag) { flag_ = flag; }

  // Invoke the callback if it is not owned.
  template <typename Callback>
  void AcquireOwnershipOnce(Callback acquire) {
    if (!flag_) {
      acquire();
      flag_ = true;
Y
Yang Yu 已提交
41 42 43
    }
  }

44 45 46
 private:
  bool flag_;
};
Y
Yang Yu 已提交
47

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
// Copy-On-Write pointer.
// It will hold a T* pointer, and only copy once when `MutableData` is invoked.
//
// The template parameter OwnershipFlags should have:
//   * a constructor takes a bool. True if own.
//   * SetOwnership(bool flag).
//   * AcquireOwnershipOnce(Callback). It will invoke the callback if it is not
//     owned.
//
// https://en.wikipedia.org/wiki/Copy-on-write
template <typename T, typename OwnershipFlags = ThreadUnsafeOwnershipFlags>
class COWPtr {
 public:
  // Ctor from raw pointer.
  explicit COWPtr(T* ptr) : payload_(ptr), ownership_{true} {}
Y
Yang Yu 已提交
63

64 65 66 67
  // Move methods. Steal ownership from origin
  COWPtr(COWPtr&& other)
      : payload_(other.payload_), ownership_{std::move(other.ownership_)} {}
  COWPtr& operator=(COWPtr&& origin) = default;
Y
Yang Yu 已提交
68

69 70 71 72 73 74
  // Copy methods. Not own payload
  COWPtr(const COWPtr& other) : payload_(other.payload_), ownership_{false} {}
  COWPtr& operator=(const COWPtr& other) {
    payload_ = other.payload_;
    ownership_.SetOwnership(false);
    return *this;
C
chengduoZH 已提交
75
  }
76 77 78 79 80 81 82 83 84 85

  // Access read only data.
  const T& Data() const { return *payload_; }

  // Access mutable data. If the data is not owned, the data will be copied
  // before.
  T* MutableData() {
    ownership_.AcquireOwnershipOnce(
        [this] { payload_.reset(new T(*payload_)); });
    return payload_.get();
Y
Yang Yu 已提交
86
  }
87 88 89 90 91 92 93

 private:
  // Actual data pointer.
  std::shared_ptr<T> payload_;

  // Ownership flag.
  OwnershipFlags ownership_;
Y
Yang Yu 已提交
94
};
95

Y
Yang Yu 已提交
96 97 98
}  // namespace details
}  // namespace framework
}  // namespace paddle