cow_ptr.h 2.9 KB
Newer Older
Y
Yang Yu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.

   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>
#include <thread>

namespace paddle {
namespace framework {
namespace details {

// Change it to thread safe flags if needed.
class ThreadUnsafeOwnershipFlags {
 public:
  ThreadUnsafeOwnershipFlags(bool flag) : flag_(flag) {}

Y
Yang Yu 已提交
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

  void SetOwnership(bool flag) { flag_ = flag; }

Y
Yang Yu 已提交
35
  // Invoke the callback if it is not owned.
Y
Yang Yu 已提交
36 37 38 39 40 41 42 43 44 45 46 47
  template <typename Callback>
  void AcquireOwnershipOnce(Callback acquire) {
    if (!flag_) {
      acquire();
      flag_ = true;
    }
  }

 private:
  bool flag_;
};

Y
Yang Yu 已提交
48
// Copy-On-Write pointer.
Y
Yang Yu 已提交
49 50 51 52 53 54 55
// 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.
Y
Yang Yu 已提交
56 57
//
// https://en.wikipedia.org/wiki/Copy-on-write
Y
Yang Yu 已提交
58 59 60 61 62 63 64
template <typename T, typename OwnershipFlags = ThreadUnsafeOwnershipFlags>
class COWPtr {
 public:
  // Ctor from raw pointer.
  explicit COWPtr(T* ptr) : payload_(ptr), ownership_{true} {}

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

  // Copy methods. Not own payload
Y
Yang Yu 已提交
70 71 72
  COWPtr(const COWPtr& other) : payload_(other.payload_), ownership_{false} {}
  COWPtr& operator=(const COWPtr& other) {
    payload_ = other.payload_;
Y
Yang Yu 已提交
73 74 75 76
    ownership_.SetOwnership(false);
    return *this;
  }

Y
Yang Yu 已提交
77
  // Access read only data.
Y
Yang Yu 已提交
78 79
  const T& Data() const { return *payload_; }

Y
Yang Yu 已提交
80 81
  // Access mutable data. If the data is not owned, the data will be copied
  // before.
Y
Yang Yu 已提交
82 83 84 85 86 87 88
  T* MutableData() {
    ownership_.AcquireOwnershipOnce(
        [this] { payload_.reset(new T(*payload_)); });
    return payload_.get();
  }

 private:
Y
Yang Yu 已提交
89
  // Actual data pointer.
Y
Yang Yu 已提交
90
  std::shared_ptr<T> payload_;
Y
Yang Yu 已提交
91 92

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

}  // namespace details
}  // namespace framework
}  // namespace paddle