From: Andrii Grynenko Date: Thu, 26 Nov 2015 20:33:16 +0000 (-0800) Subject: Deprecate get_weak() X-Git-Tag: deprecate-dynamic-initializer~223 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=19fb62bd3fa370bcecd3fac46910718e3871aec6;p=folly.git Deprecate get_weak() Summary: get_weak() is always used with .lock(), but try_get() is actually more performant than get_weak().lock(). Using get_weak() to store a weak_ptr and keep locking is not safe in fork scenarios. Reviewed By: yfeldblum Differential Revision: D2694223 fb-gh-sync-id: 908d44293ffd9b3782152d43e28d5de172d1654a --- diff --git a/folly/Singleton.h b/folly/Singleton.h index 94867c02..af8dacdb 100644 --- a/folly/Singleton.h +++ b/folly/Singleton.h @@ -486,7 +486,8 @@ class Singleton { // singleton, you can try to do so with a weak_ptr. Avoid this when // possible but the inability to lock the weak pointer can be a // signal that the vault has been destroyed. - static std::weak_ptr get_weak() { + static std::weak_ptr + get_weak() __attribute__ ((__deprecated__("Replaced by try_get"))) { return getEntry().get_weak(); } diff --git a/folly/test/SingletonTest.cpp b/folly/test/SingletonTest.cpp index 575e136f..89423d45 100644 --- a/folly/test/SingletonTest.cpp +++ b/folly/test/SingletonTest.cpp @@ -391,10 +391,10 @@ TEST(Singleton, SingletonCreationError) { SingletonCreationError error_once_singleton; // first time should error out - EXPECT_THROW(error_once_singleton.get_weak().lock(), std::runtime_error); + EXPECT_THROW(error_once_singleton.try_get(), std::runtime_error); // second time it'll work fine - error_once_singleton.get_weak().lock(); + error_once_singleton.try_get(); SUCCEED(); } @@ -409,7 +409,7 @@ TEST(Singleton, SingletonConcurrencyStress) { std::vector ts; for (size_t i = 0; i < 100; ++i) { ts.emplace_back([&]() { - slowpoke_singleton.get_weak().lock(); + slowpoke_singleton.try_get(); }); }