aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/core/lib/core/refcount.cc
diff options
context:
space:
mode:
Diffstat (limited to 'tensorflow/core/lib/core/refcount.cc')
-rw-r--r--tensorflow/core/lib/core/refcount.cc35
1 files changed, 35 insertions, 0 deletions
diff --git a/tensorflow/core/lib/core/refcount.cc b/tensorflow/core/lib/core/refcount.cc
new file mode 100644
index 0000000000..3ed8c58eb8
--- /dev/null
+++ b/tensorflow/core/lib/core/refcount.cc
@@ -0,0 +1,35 @@
+#include "tensorflow/core/lib/core/refcount.h"
+#include "tensorflow/core/platform/logging.h"
+
+namespace tensorflow {
+namespace core {
+
+RefCounted::RefCounted() : ref_(1) {}
+
+RefCounted::~RefCounted() { DCHECK_EQ(ref_.load(), 0); }
+
+void RefCounted::Ref() const {
+ DCHECK_GE(ref_.load(), 1);
+ ref_.fetch_add(1, std::memory_order_relaxed);
+}
+
+bool RefCounted::Unref() const {
+ DCHECK_GT(ref_.load(), 0);
+ // If ref_==1, this object is owned only by the caller. Bypass a locked op
+ // in that case.
+ if (ref_.load(std::memory_order_acquire) == 1 || ref_.fetch_sub(1) == 1) {
+ // Make DCHECK in ~RefCounted happy
+ DCHECK((ref_.store(0), true));
+ delete this;
+ return true;
+ } else {
+ return false;
+ }
+}
+
+bool RefCounted::RefCountIsOne() const {
+ return (ref_.load(std::memory_order_acquire) == 1);
+}
+
+} // namespace core
+} // namespace tensorflow