From: Yedidya Feldblum <yfeldblum@fb.com>
Date: Mon, 30 Nov 2015 23:29:32 +0000 (-0800)
Subject: Avoid deprecated Singleton<T>::get() in folly/futures
X-Git-Tag: deprecate-dynamic-initializer~222
X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=d3a08687ca79f566693a41520124f8ba92ca185a;p=folly.git

Avoid deprecated Singleton<T>::get() in folly/futures

Summary: [Folly] Avoid deprecated `Singleton<T>::get()` in `folly/futures`.

Changes `getTimekeeperSingleton()` to return a `shared_ptr<Timekeeper>`, and patches its call-sites.

Additionally, have the call-sites keep the singleton instance alive for the duration that it is being directly used.

Reviewed By: ddrcoder

Differential Revision: D2702361

fb-gh-sync-id: 82b72ee514dc4f2a7f7522af8b2e92b34df063d6
---

diff --git a/folly/futures/Future-inl.h b/folly/futures/Future-inl.h
index 6fb85a33..3a919ff4 100644
--- a/folly/futures/Future-inl.h
+++ b/folly/futures/Future-inl.h
@@ -33,7 +33,7 @@ namespace folly {
 class Timekeeper;
 
 namespace detail {
-  Timekeeper* getTimekeeperSingleton();
+  std::shared_ptr<Timekeeper> getTimekeeperSingleton();
 }
 
 template <class T>
@@ -889,8 +889,10 @@ Future<T> Future<T>::within(Duration dur, E e, Timekeeper* tk) {
     std::atomic<bool> token {false};
   };
 
+  std::shared_ptr<Timekeeper> tks;
   if (!tk) {
-    tk = folly::detail::getTimekeeperSingleton();
+    tks = folly::detail::getTimekeeperSingleton();
+    tk = DCHECK_NOTNULL(tks.get());
   }
 
   auto ctx = std::make_shared<Context>(std::move(e));
diff --git a/folly/futures/Future.cpp b/folly/futures/Future.cpp
index bbab4003..7cc623e3 100644
--- a/folly/futures/Future.cpp
+++ b/folly/futures/Future.cpp
@@ -32,8 +32,10 @@ template class Future<double>;
 namespace folly { namespace futures {
 
 Future<Unit> sleep(Duration dur, Timekeeper* tk) {
+  std::shared_ptr<Timekeeper> tks;
   if (LIKELY(!tk)) {
-    tk = folly::detail::getTimekeeperSingleton();
+    tks = folly::detail::getTimekeeperSingleton();
+    tk = DCHECK_NOTNULL(tks.get());
   }
   return tk->after(dur);
 }
diff --git a/folly/futures/ThreadWheelTimekeeper.cpp b/folly/futures/ThreadWheelTimekeeper.cpp
index 4179e99f..37ddf08d 100644
--- a/folly/futures/ThreadWheelTimekeeper.cpp
+++ b/folly/futures/ThreadWheelTimekeeper.cpp
@@ -138,8 +138,8 @@ Future<Unit> ThreadWheelTimekeeper::after(Duration dur) {
 
 namespace detail {
 
-Timekeeper* getTimekeeperSingleton() {
-  return timekeeperSingleton_.get();
+std::shared_ptr<Timekeeper> getTimekeeperSingleton() {
+  return timekeeperSingleton_.try_get();
 }
 
 } // detail
diff --git a/folly/futures/test/TimekeeperTest.cpp b/folly/futures/test/TimekeeperTest.cpp
index eddf2e81..316d48bf 100644
--- a/folly/futures/test/TimekeeperTest.cpp
+++ b/folly/futures/test/TimekeeperTest.cpp
@@ -37,7 +37,7 @@ struct TimekeeperFixture : public testing::Test {
     timeLord_(folly::detail::getTimekeeperSingleton())
   {}
 
-  Timekeeper* timeLord_;
+  std::shared_ptr<Timekeeper> timeLord_;
 };
 
 TEST_F(TimekeeperFixture, after) {