You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kudu.apache.org by mp...@apache.org on 2016/04/02 00:12:31 UTC

incubator-kudu git commit: Make ScopedCleanup cancelable

Repository: incubator-kudu
Updated Branches:
  refs/heads/master 2079c81f2 -> c6e709f6e


Make ScopedCleanup cancelable

Change-Id: I3240bfbb877a3e74c40815a9266af26f69e0ccc3
Reviewed-on: http://gerrit.cloudera.org:8080/2698
Tested-by: Kudu Jenkins
Reviewed-by: Adar Dembo <ad...@cloudera.com>


Project: http://git-wip-us.apache.org/repos/asf/incubator-kudu/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-kudu/commit/c6e709f6
Tree: http://git-wip-us.apache.org/repos/asf/incubator-kudu/tree/c6e709f6
Diff: http://git-wip-us.apache.org/repos/asf/incubator-kudu/diff/c6e709f6

Branch: refs/heads/master
Commit: c6e709f6e7def678ce8d5ea40a36b952c8cb7ad9
Parents: 2079c81
Author: Mike Percy <mp...@apache.org>
Authored: Thu Mar 31 16:49:58 2016 -0700
Committer: Mike Percy <mp...@apache.org>
Committed: Fri Apr 1 22:11:53 2016 +0000

----------------------------------------------------------------------
 src/kudu/util/scoped_cleanup-test.cc | 13 ++++++++++++-
 src/kudu/util/scoped_cleanup.h       | 14 ++++++++++++--
 2 files changed, 24 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/c6e709f6/src/kudu/util/scoped_cleanup-test.cc
----------------------------------------------------------------------
diff --git a/src/kudu/util/scoped_cleanup-test.cc b/src/kudu/util/scoped_cleanup-test.cc
index 790df39..fad50a0 100644
--- a/src/kudu/util/scoped_cleanup-test.cc
+++ b/src/kudu/util/scoped_cleanup-test.cc
@@ -28,7 +28,18 @@ TEST(ScopedCleanup, TestCleanup) {
     auto cleanup = MakeScopedCleanup([&] () { var = saved; });
     var = 42;
   }
-  ASSERT_EQ(var, 0);
+  ASSERT_EQ(0, var);
+}
+
+TEST(ScopedCleanup, TestCancelCleanup) {
+  int var = 0;
+  {
+    auto saved = var;
+    auto cleanup = MakeScopedCleanup([&] () { var = saved; });
+    var = 42;
+    cleanup.cancel();
+  }
+  ASSERT_EQ(42, var);
 }
 
 } // namespace kudu

http://git-wip-us.apache.org/repos/asf/incubator-kudu/blob/c6e709f6/src/kudu/util/scoped_cleanup.h
----------------------------------------------------------------------
diff --git a/src/kudu/util/scoped_cleanup.h b/src/kudu/util/scoped_cleanup.h
index a74422c..58aadad 100644
--- a/src/kudu/util/scoped_cleanup.h
+++ b/src/kudu/util/scoped_cleanup.h
@@ -26,9 +26,19 @@ namespace kudu {
 template<typename F>
 class ScopedCleanup {
  public:
-  explicit ScopedCleanup(F f) : f_(std::move(f)) {}
-  ~ScopedCleanup() { f_(); }
+  explicit ScopedCleanup(F f)
+      : cancelled_(false),
+        f_(std::move(f)) {
+  }
+  ~ScopedCleanup() {
+    if (!cancelled_) {
+      f_();
+    }
+  }
+  void cancel() { cancelled_ = true; }
+
  private:
+  bool cancelled_;
   F f_;
 };