From d45cf240a96836b00dd122d828384b9527cbd185 Mon Sep 17 00:00:00 2001 From: Huffers Date: Mon, 27 Mar 2017 17:09:18 +0100 Subject: [PATCH] Remove xgboost's thread_local and switch to dmlc::ThreadLocalStore (#2121) * Remove xgboost's own version of thread_local and switch to dmlc::ThreadLocalStore (#2109) * Update dmlc-core --- dmlc-core | 2 +- src/c_api/c_api.cc | 4 +- src/c_api/c_api_error.cc | 4 +- src/common/common.cc | 4 +- src/common/thread_local.h | 91 --------------------------------------- 5 files changed, 7 insertions(+), 98 deletions(-) delete mode 100644 src/common/thread_local.h diff --git a/dmlc-core b/dmlc-core index 2b75a0ce6..b5bec5481 160000 --- a/dmlc-core +++ b/dmlc-core @@ -1 +1 @@ -Subproject commit 2b75a0ce6f191ad0fcb5319039b41e990968542a +Subproject commit b5bec5481df86e8e6728d8bd80a61d87ef3b2cd5 diff --git a/src/c_api/c_api.cc b/src/c_api/c_api.cc index 1621c8b0f..c1474dbdb 100644 --- a/src/c_api/c_api.cc +++ b/src/c_api/c_api.cc @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include @@ -13,7 +14,6 @@ #include "./c_api_error.h" #include "../data/simple_csr_source.h" -#include "../common/thread_local.h" #include "../common/math.h" #include "../common/io.h" #include "../common/group_data.h" @@ -197,7 +197,7 @@ struct XGBAPIThreadLocalEntry { }; // define the threadlocal store. -typedef xgboost::common::ThreadLocalStore XGBAPIThreadLocalStore; +typedef dmlc::ThreadLocalStore XGBAPIThreadLocalStore; int XGDMatrixCreateFromFile(const char *fname, int silent, diff --git a/src/c_api/c_api_error.cc b/src/c_api/c_api_error.cc index e1949e560..19cd75af6 100644 --- a/src/c_api/c_api_error.cc +++ b/src/c_api/c_api_error.cc @@ -3,14 +3,14 @@ * \file c_api_error.cc * \brief C error handling */ +#include #include "./c_api_error.h" -#include "../common/thread_local.h" struct XGBAPIErrorEntry { std::string last_error; }; -typedef xgboost::common::ThreadLocalStore XGBAPIErrorStore; +typedef dmlc::ThreadLocalStore XGBAPIErrorStore; const char *XGBGetLastError() { return XGBAPIErrorStore::Get()->last_error.c_str(); diff --git a/src/common/common.cc b/src/common/common.cc index 43a23853e..f53ff752f 100644 --- a/src/common/common.cc +++ b/src/common/common.cc @@ -3,8 +3,8 @@ * \file common.cc * \brief Enable all kinds of global variables in common. */ +#include #include "./random.h" -#include "./thread_local.h" namespace xgboost { namespace common { @@ -14,7 +14,7 @@ struct RandomThreadLocalEntry { GlobalRandomEngine engine; }; -typedef ThreadLocalStore RandomThreadLocalStore; +typedef dmlc::ThreadLocalStore RandomThreadLocalStore; GlobalRandomEngine& GlobalRandom() { return RandomThreadLocalStore::Get()->engine; diff --git a/src/common/thread_local.h b/src/common/thread_local.h deleted file mode 100644 index c996152fc..000000000 --- a/src/common/thread_local.h +++ /dev/null @@ -1,91 +0,0 @@ -/*! - * Copyright (c) 2015 by Contributors - * \file thread_local.h - * \brief Common utility for thread local storage. - */ -#ifndef XGBOOST_COMMON_THREAD_LOCAL_H_ -#define XGBOOST_COMMON_THREAD_LOCAL_H_ - -#include - -#if DMLC_ENABLE_STD_THREAD -#include -#endif - -#include -#include - -namespace xgboost { -namespace common { - -// macro hanlding for threadlocal variables -#ifdef __sun - #define MX_TREAD_LOCAL -#elif defined(__GNUC__) - #define MX_TREAD_LOCAL __thread -#elif __STDC_VERSION__ >= 201112L - #define MX_TREAD_LOCAL _Thread_local -#elif defined(_MSC_VER) - #define MX_TREAD_LOCAL __declspec(thread) -#endif - -#ifndef MX_TREAD_LOCAL -#message("Warning: Threadlocal is not enabled"); -#endif - -/*! - * \brief A threadlocal store to store threadlocal variables. - * Will return a thread local singleton of type T - * \tparam T the type we like to store - */ -template -class ThreadLocalStore { - public: - /*! \return get a thread local singleton */ - static T* Get() { - static MX_TREAD_LOCAL T* ptr = nullptr; - if (ptr == nullptr) { - ptr = new T(); - Singleton()->RegisterDelete(ptr); - } - return ptr; - } - - private: - /*! \brief constructor */ - ThreadLocalStore() {} - /*! \brief destructor */ - ~ThreadLocalStore() { - for (size_t i = 0; i < data_.size(); ++i) { - delete data_[i]; - } - } - /*! \return singleton of the store */ - static ThreadLocalStore *Singleton() { - static ThreadLocalStore inst; - return &inst; - } - /*! - * \brief register str for internal deletion - * \param str the string pointer - */ - void RegisterDelete(T *str) { -#if DMLC_ENABLE_STD_THREAD - std::unique_lock lock(mutex_); - data_.push_back(str); - lock.unlock(); -#else - data_.push_back(str); -#endif - } - -#if DMLC_ENABLE_STD_THREAD - /*! \brief internal mutex */ - std::mutex mutex_; -#endif - /*!\brief internal data */ - std::vector data_; -}; -} // namespace common -} // namespace xgboost -#endif // XGBOOST_COMMON_THREAD_LOCAL_H_