HBASE-15823 Use call once for user util
Project: http://git-wip-us.apache.org/repos/asf/hbase/repo Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/20f43d2f Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/20f43d2f Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/20f43d2f Branch: refs/heads/HBASE-14850 Commit: 20f43d2f519d686f5497d3a28d6decd6e908febe Parents: 2508b03 Author: Elliott Clark <ecl...@apache.org> Authored: Fri May 13 13:07:03 2016 -0700 Committer: Elliott Clark <ecl...@apache.org> Committed: Tue May 17 09:05:19 2016 -0700 ---------------------------------------------------------------------- hbase-native-client/utils/user-util.cc | 13 ++----------- hbase-native-client/utils/user-util.h | 6 ++---- 2 files changed, 4 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/hbase/blob/20f43d2f/hbase-native-client/utils/user-util.cc ---------------------------------------------------------------------- diff --git a/hbase-native-client/utils/user-util.cc b/hbase-native-client/utils/user-util.cc index 3d963b3..9e170e0 100644 --- a/hbase-native-client/utils/user-util.cc +++ b/hbase-native-client/utils/user-util.cc @@ -27,22 +27,14 @@ using namespace hbase; using namespace std; -UserUtil::UserUtil() : init_{false}, user_name_{"drwho"}, m_() {} +UserUtil::UserUtil() : once_flag_{}, user_name_{"drwho"} {} string UserUtil::user_name() { - if (!init_) { - compute_user_name(); - } + std::call_once(once_flag_, [this]() { compute_user_name(); }); return user_name_; } void UserUtil::compute_user_name() { - lock_guard<mutex> lock(m_); - - if (init_) { - return; - } - // According to the man page of getpwuid // this should never be free'd // @@ -52,6 +44,5 @@ void UserUtil::compute_user_name() { // make sure that we got something. if (passwd && passwd->pw_name) { user_name_ = string{passwd->pw_name}; - init_ = true; } } http://git-wip-us.apache.org/repos/asf/hbase/blob/20f43d2f/hbase-native-client/utils/user-util.h ---------------------------------------------------------------------- diff --git a/hbase-native-client/utils/user-util.h b/hbase-native-client/utils/user-util.h index 0b4cc73..fdfc0c8 100644 --- a/hbase-native-client/utils/user-util.h +++ b/hbase-native-client/utils/user-util.h @@ -19,8 +19,7 @@ #pragma once -#include <atomic> -#include <memory> +#include <string> #include <mutex> namespace hbase { @@ -49,8 +48,7 @@ private: * Compute the username. This will block. */ void compute_user_name(); - std::atomic<bool> init_; + std::once_flag once_flag_; std::string user_name_; - std::mutex m_; }; } // namespace hbase