Commit 19fb62bd authored by Andrii Grynenko's avatar Andrii Grynenko Committed by facebook-github-bot-0

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
parent f0a9aafc
...@@ -486,7 +486,8 @@ class Singleton { ...@@ -486,7 +486,8 @@ class Singleton {
// singleton, you can try to do so with a weak_ptr. Avoid this when // 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 // possible but the inability to lock the weak pointer can be a
// signal that the vault has been destroyed. // signal that the vault has been destroyed.
static std::weak_ptr<T> get_weak() { static std::weak_ptr<T>
get_weak() __attribute__ ((__deprecated__("Replaced by try_get"))) {
return getEntry().get_weak(); return getEntry().get_weak();
} }
......
...@@ -391,10 +391,10 @@ TEST(Singleton, SingletonCreationError) { ...@@ -391,10 +391,10 @@ TEST(Singleton, SingletonCreationError) {
SingletonCreationError<ErrorConstructor> error_once_singleton; SingletonCreationError<ErrorConstructor> error_once_singleton;
// first time should error out // 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 // second time it'll work fine
error_once_singleton.get_weak().lock(); error_once_singleton.try_get();
SUCCEED(); SUCCEED();
} }
...@@ -409,7 +409,7 @@ TEST(Singleton, SingletonConcurrencyStress) { ...@@ -409,7 +409,7 @@ TEST(Singleton, SingletonConcurrencyStress) {
std::vector<std::thread> ts; std::vector<std::thread> ts;
for (size_t i = 0; i < 100; ++i) { for (size_t i = 0; i < 100; ++i) {
ts.emplace_back([&]() { ts.emplace_back([&]() {
slowpoke_singleton.get_weak().lock(); slowpoke_singleton.try_get();
}); });
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment