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
This commit is contained in:
Huffers 2017-03-27 17:09:18 +01:00 committed by Tianqi Chen
parent 14fba01b5a
commit d45cf240a9
5 changed files with 7 additions and 98 deletions

@ -1 +1 @@
Subproject commit 2b75a0ce6f191ad0fcb5319039b41e990968542a
Subproject commit b5bec5481df86e8e6728d8bd80a61d87ef3b2cd5

View File

@ -4,6 +4,7 @@
#include <xgboost/learner.h>
#include <xgboost/c_api.h>
#include <xgboost/logging.h>
#include <dmlc/thread_local.h>
#include <rabit/rabit.h>
#include <cstdio>
#include <vector>
@ -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<XGBAPIThreadLocalEntry> XGBAPIThreadLocalStore;
typedef dmlc::ThreadLocalStore<XGBAPIThreadLocalEntry> XGBAPIThreadLocalStore;
int XGDMatrixCreateFromFile(const char *fname,
int silent,

View File

@ -3,14 +3,14 @@
* \file c_api_error.cc
* \brief C error handling
*/
#include <dmlc/thread_local.h>
#include "./c_api_error.h"
#include "../common/thread_local.h"
struct XGBAPIErrorEntry {
std::string last_error;
};
typedef xgboost::common::ThreadLocalStore<XGBAPIErrorEntry> XGBAPIErrorStore;
typedef dmlc::ThreadLocalStore<XGBAPIErrorEntry> XGBAPIErrorStore;
const char *XGBGetLastError() {
return XGBAPIErrorStore::Get()->last_error.c_str();

View File

@ -3,8 +3,8 @@
* \file common.cc
* \brief Enable all kinds of global variables in common.
*/
#include <dmlc/thread_local.h>
#include "./random.h"
#include "./thread_local.h"
namespace xgboost {
namespace common {
@ -14,7 +14,7 @@ struct RandomThreadLocalEntry {
GlobalRandomEngine engine;
};
typedef ThreadLocalStore<RandomThreadLocalEntry> RandomThreadLocalStore;
typedef dmlc::ThreadLocalStore<RandomThreadLocalEntry> RandomThreadLocalStore;
GlobalRandomEngine& GlobalRandom() {
return RandomThreadLocalStore::Get()->engine;

View File

@ -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 <dmlc/base.h>
#if DMLC_ENABLE_STD_THREAD
#include <mutex>
#endif
#include <memory>
#include <vector>
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<typename T>
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<T> *Singleton() {
static ThreadLocalStore<T> 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<std::mutex> 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<T*> data_;
};
} // namespace common
} // namespace xgboost
#endif // XGBOOST_COMMON_THREAD_LOCAL_H_